-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathModel.php
More file actions
141 lines (125 loc) · 4.25 KB
/
Model.php
File metadata and controls
141 lines (125 loc) · 4.25 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<?php
/**
* User: TheCodeholic
* Date: 7/8/2020
* Time: 9:16 AM
*/
namespace thecodeholic\phpmvc;
/**
* Class Model
*
* @author Zura Sekhniashvili <zurasekhniashvili@gmail.com>
* @package thecodeholic\phpmvc
*/
class Model
{
const RULE_REQUIRED = 'required';
const RULE_EMAIL = 'email';
const RULE_MIN = 'min';
const RULE_MAX = 'max';
const RULE_MATCH = 'match';
const RULE_UNIQUE = 'unique';
public array $errors = [];
public function loadData($data)
{
foreach ($data as $key => $value) {
if (property_exists($this, $key)) {
$this->{$key} = $value;
}
}
}
public function attributes()
{
return [];
}
public function labels()
{
return [];
}
public function getLabel($attribute)
{
return $this->labels()[$attribute] ?? $attribute;
}
public function rules()
{
return [];
}
public function validate()
{
foreach ($this->rules() as $attribute => $rules) {
$value = $this->{$attribute};
foreach ($rules as $rule) {
$ruleName = $rule;
if (!is_string($rule)) {
$ruleName = $rule[0];
}
if ($ruleName === self::RULE_REQUIRED && !$value) {
$this->addErrorByRule($attribute, self::RULE_REQUIRED);
}
if ($ruleName === self::RULE_EMAIL && !filter_var($value, FILTER_VALIDATE_EMAIL)) {
$this->addErrorByRule($attribute, self::RULE_EMAIL);
}
if ($ruleName === self::RULE_MIN && strlen($value) < $rule['min']) {
$this->addErrorByRule($attribute, self::RULE_MIN, ['min' => $rule['min']]);
}
if ($ruleName === self::RULE_MAX && strlen($value) > $rule['max']) {
$this->addErrorByRule($attribute, self::RULE_MAX);
}
if ($ruleName === self::RULE_MATCH && $value !== $this->{$rule['match']}) {
$this->addErrorByRule($attribute, self::RULE_MATCH, ['match' => $rule['match']]);
}
if ($ruleName === self::RULE_UNIQUE) {
$className = $rule['class'];
$uniqueAttr = $rule['attribute'] ?? $attribute;
$tableName = $className::tableName();
$db = Application::$app->db;
$statement = $db->prepare("SELECT * FROM $tableName WHERE $uniqueAttr = :$uniqueAttr");
$statement->bindValue(":$uniqueAttr", $value);
$statement->execute();
$record = $statement->fetchObject();
if ($record) {
$this->addErrorByRule($attribute, self::RULE_UNIQUE);
}
}
}
}
return empty($this->errors);
}
public function errorMessages()
{
return [
self::RULE_REQUIRED => 'This field is required',
self::RULE_EMAIL => 'This field must be valid email address',
self::RULE_MIN => 'Min length of this field must be {min}',
self::RULE_MAX => 'Max length of this field must be {max}',
self::RULE_MATCH => 'This field must be the same as {match}',
self::RULE_UNIQUE => 'Record with with this {field} already exists',
];
}
public function errorMessage($rule)
{
return $this->errorMessages()[$rule];
}
protected function addErrorByRule(string $attribute, string $rule, $params = [])
{
$params['field'] ??= $attribute;
$errorMessage = $this->errorMessage($rule);
foreach ($params as $key => $value) {
$errorMessage = str_replace("{{$key}}", $value, $errorMessage);
}
$this->errors[$attribute][] = $errorMessage;
}
public function addError(string $attribute, string $message)
{
$this->errors[$attribute][] = $message;
}
public function hasError($attribute)
{
return $this->errors[$attribute] ?? false;
}
public function getFirstError($attribute)
{
$errors = $this->errors[$attribute] ?? [];
return $errors[0] ?? '';
}
}