-
Notifications
You must be signed in to change notification settings - Fork 0
/
Model.php
96 lines (93 loc) · 3.88 KB
/
Model.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
<?php
namespace app\core;
abstract class Model
{
public const RULE_REQUIRED = 'required';
public const RULE_Email = 'email';
public const RULE_MAX_LENGHT = 'maxLenght';
public const RULE_MIN_LENGHT = 'minLenght';
public const RULE_MATCH = 'match';
public const RULE_UNIQUE = 'unique';
public array $errors = [];
public function loadData($data)
{
foreach ($data as $key => $value) {
if (property_exists($this, $key)) {
$this->{$key} = $value;
} else {
return false;
}
}
}
abstract public function Rules();
public function Validate(): bool
{
foreach ($this->Rules() as $attribute => $rules) {
$attributeValue = $this->{$attribute};
foreach ($rules as $rule) {
$ruleName = $rule;
if (!is_string($ruleName)) {
$ruleName = $rule[0];
}
//checking if Attribute value exists
if ($ruleName === self::RULE_REQUIRED && !isset($attributeValue)) {
$this->newRuleError($attribute, self::RULE_REQUIRED);
}
if ($ruleName === self::RULE_Email && !filter_var($attributeValue, FILTER_VALIDATE_EMAIL)) {
$this->newRuleError($attribute, self::RULE_Email);
}
if ($ruleName === self::RULE_MAX_LENGHT && strlen($attributeValue) > $rule["max"]) {
$this->newRuleError($attribute, self::RULE_MAX_LENGHT, $rule);
}
if ($ruleName === self::RULE_MIN_LENGHT && strlen($attributeValue) < $rule["min"]) {
$this->newRuleError($attribute, self::RULE_MIN_LENGHT, $rule);
}
if ($ruleName === self::RULE_MATCH && $attributeValue != $this->{$rule["match"]}) {
$this->newRuleError($attribute, self::RULE_MATCH, $rule);
}
if ($ruleName === self::RULE_UNIQUE){
$className = $rule["className"];
$uniqueAttr = $rule["attribute"] ?? $attribute;
$tableName = $className::tableName();
$SQL = "SELECT * FROM $tableName WHERE $attribute = :attr";
$statement = Application::$app->db->pdo->prepare($SQL);
$statement->bindValue(":attr", $attributeValue);
$statement->execute();
$record = $statement->fetchObject();
if ($record){
$this->newRuleError($attribute, self::RULE_UNIQUE, ["field" => $attribute]);
}
}
}
}
return empty($this->errors);
}
public function addError(string $attributeName, $message){
$this->errors[$attributeName][] = $message;
}
private function newRuleError(string $attributeName, string $rule, $params = [])
{
$message = $this->errorMessages()[$rule] ?? "";
foreach ($params as $key => $value) {
$message = str_replace("{{$key}}", $value, $message);
}
$this->errors[$attributeName][] = $message;
}
public function errorMessages(): array
{
return [
self::RULE_REQUIRED => "This field is required",
self::RULE_Email => "This field must be a valid email adress",
self::RULE_MAX_LENGHT => "Max length of this field must be {max}",
self::RULE_MIN_LENGHT => "Min length of this field must be {min}",
self::RULE_MATCH => "This field must be the same as {match}",
self::RULE_UNIQUE => "Record with this {field} already exits",
];
}
public function haseError($attribute){
return $this->errors[$attribute] ?? false;
}
public function getErrorMessages($attribute){
return $this->errors[$attribute];
}
}