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