1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Albert221\Validation; |
4
|
|
|
|
5
|
|
|
use Albert221\Validation\Rule\RuleInterface; |
6
|
|
|
|
7
|
|
|
class Validator |
8
|
|
|
{ |
9
|
|
|
protected $rules = []; |
10
|
|
|
protected $fields = []; |
11
|
|
|
protected $secretFields = []; |
12
|
|
|
protected $errors = []; |
13
|
|
|
|
14
|
|
|
protected $validated = false; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Adds field with its value to be validated later. |
18
|
|
|
* |
19
|
|
|
* @param string $name |
20
|
|
|
* @param mixed $value |
21
|
|
|
* @param bool $secret Should this be hidden when fetching fields? |
22
|
|
|
*/ |
23
|
8 |
|
public function addField($name, $value, $secret = false) |
24
|
|
|
{ |
25
|
8 |
|
if (! is_string($name)) { |
26
|
1 |
|
throw new \InvalidArgumentException( |
27
|
1 |
|
sprintf('Field name should be of string type, %s given.', gettype($name)) |
28
|
1 |
|
); |
29
|
|
|
} |
30
|
|
|
|
31
|
7 |
|
if ($this->issetField($name)) { |
32
|
1 |
|
throw new \InvalidArgumentException(sprintf('Field \'%s\' has been already added.', $name)); |
33
|
|
|
} |
34
|
|
|
|
35
|
7 |
|
$this->fields[$name] = $value; |
36
|
|
|
|
37
|
7 |
|
if ($secret) { |
38
|
1 |
|
$this->secretFields[] = $name; |
39
|
1 |
|
} |
40
|
7 |
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Returns if field is already added. |
44
|
|
|
* |
45
|
|
|
* @param string $name |
46
|
|
|
* @return bool |
47
|
|
|
*/ |
48
|
8 |
|
public function issetField($name) |
49
|
|
|
{ |
50
|
8 |
|
return isset($this->fields[$name]); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Adds validation rule to field with passed name. |
55
|
|
|
* |
56
|
|
|
* @param string $fieldName |
57
|
|
|
* @param RuleInterface $rule |
58
|
|
|
* @return RuleInterface |
59
|
|
|
*/ |
60
|
5 |
|
public function addRule($fieldName, RuleInterface $rule) |
61
|
|
|
{ |
62
|
5 |
|
if (! $this->issetField($fieldName)) { |
63
|
1 |
|
throw new \InvalidArgumentException( |
64
|
1 |
|
sprintf('Field \'%s\' does not exist and hence you cannot add rule to it', $fieldName) |
65
|
1 |
|
); |
66
|
|
|
} |
67
|
|
|
|
68
|
4 |
|
$this->rules[$fieldName][] = $rule; |
69
|
|
|
|
70
|
4 |
|
return $rule; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Returns count of validation errors. |
75
|
|
|
* |
76
|
|
|
* @return int |
77
|
|
|
*/ |
78
|
3 |
|
public function getErrorsCount() |
79
|
|
|
{ |
80
|
|
|
// Count recursively items of $errors array and subtract count of items in $errors to get subarray items count. |
81
|
3 |
|
$keysCount = count($this->getErrors()); |
82
|
3 |
|
$count = count($this->getErrors(), COUNT_RECURSIVE); |
83
|
|
|
|
84
|
3 |
|
return $count - $keysCount; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Returns associative array of field name => error messages pairs. |
89
|
|
|
* |
90
|
|
|
* @return array |
91
|
|
|
*/ |
92
|
4 |
|
public function getErrors() |
93
|
|
|
{ |
94
|
4 |
|
return $this->errors; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Returns added fields. Handy for flashing old input. |
99
|
|
|
* |
100
|
|
|
* @return array |
101
|
|
|
*/ |
102
|
2 |
|
public function getFields() |
103
|
|
|
{ |
104
|
2 |
|
$fields = $this->fields; |
105
|
|
|
|
106
|
2 |
|
foreach ($this->secretFields as $secret) { |
107
|
1 |
|
unset($fields[$secret]); |
108
|
2 |
|
} |
109
|
|
|
|
110
|
2 |
|
return $fields; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Validates fields using validation rules |
115
|
|
|
* |
116
|
|
|
* @return $this |
117
|
|
|
*/ |
118
|
5 |
|
public function validate() |
119
|
|
|
{ |
120
|
5 |
|
if ($this->validated) { |
121
|
1 |
|
return $this; |
122
|
|
|
} |
123
|
|
|
|
124
|
5 |
|
foreach ($this->fields as $field => $value) { |
125
|
|
|
/** @var RuleInterface $rule */ |
126
|
3 |
|
foreach ($this->rules[$field] as $rule) { |
127
|
3 |
|
if ($rule->test($value)) { |
128
|
1 |
|
continue; |
129
|
|
|
} |
130
|
|
|
|
131
|
3 |
|
$this->errors[$field][] = $rule->getMessage(); |
132
|
3 |
|
} |
133
|
5 |
|
} |
134
|
|
|
|
135
|
5 |
|
$this->validated = true; |
136
|
|
|
|
137
|
5 |
|
return $this; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Clone validator with fields and rules but without validation errors. |
142
|
|
|
*/ |
143
|
1 |
|
public function __clone() |
144
|
|
|
{ |
145
|
1 |
|
$this->validated = false; |
146
|
1 |
|
$this->errors = []; |
147
|
1 |
|
} |
148
|
|
|
} |
149
|
|
|
|