|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Bluz Framework Component |
|
4
|
|
|
* |
|
5
|
|
|
* @copyright Bluz PHP Team |
|
6
|
|
|
* @link https://github.com/bluzphp/framework |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
declare(strict_types=1); |
|
10
|
|
|
|
|
11
|
|
|
namespace Bluz\Validator; |
|
12
|
|
|
|
|
13
|
|
|
use Bluz\Validator\Exception\ValidatorException; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Validator Builder |
|
17
|
|
|
* |
|
18
|
|
|
* @package Bluz\Validator |
|
19
|
|
|
* @author Anton Shevchuk |
|
20
|
|
|
*/ |
|
21
|
|
|
class ValidatorForm |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* Stack of validators |
|
25
|
|
|
* |
|
26
|
|
|
* ['foo'] => ValidatorChain |
|
27
|
|
|
* ['bar'] => ValidatorChain |
|
28
|
|
|
* |
|
29
|
|
|
* @var ValidatorChain[] |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $validators = []; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* list of validation errors |
|
35
|
|
|
* |
|
36
|
|
|
* ['foo'] => "some field error" |
|
37
|
|
|
* ['bar'] => "some field error" |
|
38
|
|
|
* |
|
39
|
|
|
* @var array |
|
40
|
|
|
*/ |
|
41
|
|
|
protected $errors = []; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Add chain to form |
|
45
|
|
|
* |
|
46
|
|
|
* @param string $name |
|
47
|
|
|
* |
|
48
|
|
|
* @return ValidatorChain |
|
49
|
|
|
*/ |
|
50
|
5 |
|
public function add($name) : ValidatorChain |
|
51
|
|
|
{ |
|
52
|
5 |
|
$this->validators[$name] = $this->validators[$name] ?? Validator::create(); |
|
53
|
5 |
|
return $this->validators[$name]; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Validate chain of rules |
|
58
|
|
|
* |
|
59
|
|
|
* @param array $input |
|
60
|
|
|
* |
|
61
|
|
|
* @return bool |
|
62
|
|
|
*/ |
|
63
|
5 |
|
public function validate($input) : bool |
|
64
|
|
|
{ |
|
65
|
5 |
|
$this->resetErrors(); |
|
66
|
|
|
|
|
67
|
|
|
// run chains |
|
68
|
5 |
|
foreach ($this->validators as $key => $validators) { |
|
69
|
5 |
|
$this->validateItem($key, $input[$key] ?? null); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
// strict rules |
|
|
|
|
|
|
73
|
|
|
// $missed = array_keys(array_diff_key($input, $this->validators)); |
|
74
|
|
|
// foreach ($missed as $key) { |
|
75
|
|
|
// $this->setError($key, 'This field is not defined'); |
|
76
|
|
|
// } |
|
77
|
|
|
|
|
78
|
5 |
|
return !$this->hasErrors(); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Validate chain of rules for single item |
|
83
|
|
|
* |
|
84
|
|
|
* @param string $key |
|
85
|
|
|
* @param mixed $value |
|
86
|
|
|
* |
|
87
|
|
|
* @return bool |
|
88
|
|
|
*/ |
|
89
|
5 |
|
protected function validateItem($key, $value): bool |
|
90
|
|
|
{ |
|
91
|
|
|
// run validators chain |
|
92
|
5 |
|
$result = $this->validators[$key]->validate($value); |
|
93
|
|
|
|
|
94
|
5 |
|
if (!$result) { |
|
95
|
3 |
|
$this->setError($key, $this->validators[$key]->getError()); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
5 |
|
return $result; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Assert |
|
103
|
|
|
* |
|
104
|
|
|
* @param mixed $input |
|
105
|
|
|
* |
|
106
|
|
|
* @return bool |
|
107
|
|
|
* @throws ValidatorException |
|
108
|
|
|
*/ |
|
109
|
5 |
|
public function assert($input) |
|
110
|
|
|
{ |
|
111
|
5 |
|
if (!$this->validate($input)) { |
|
112
|
3 |
|
$exception = new ValidatorException(); |
|
113
|
3 |
|
$exception->setErrors($this->getErrors()); |
|
114
|
3 |
|
throw $exception; |
|
115
|
|
|
} |
|
116
|
2 |
|
return true; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* Add Error by field name |
|
121
|
|
|
* |
|
122
|
|
|
* @param string $name |
|
123
|
|
|
* @param string $message |
|
124
|
|
|
* |
|
125
|
|
|
* @return void |
|
126
|
|
|
*/ |
|
127
|
3 |
|
protected function setError($name, $message) |
|
128
|
|
|
{ |
|
129
|
3 |
|
$this->errors[$name] = $message; |
|
130
|
3 |
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* Reset errors |
|
134
|
|
|
* |
|
135
|
|
|
* @return void |
|
136
|
|
|
*/ |
|
137
|
5 |
|
protected function resetErrors() |
|
138
|
|
|
{ |
|
139
|
5 |
|
$this->errors = []; |
|
140
|
5 |
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* Get errors |
|
144
|
|
|
* |
|
145
|
|
|
* @return array |
|
146
|
|
|
*/ |
|
147
|
3 |
|
public function getErrors() : array |
|
148
|
|
|
{ |
|
149
|
3 |
|
return $this->errors; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* Has errors? |
|
154
|
|
|
* |
|
155
|
|
|
* @return bool |
|
156
|
|
|
*/ |
|
157
|
5 |
|
public function hasErrors() : bool |
|
158
|
|
|
{ |
|
159
|
5 |
|
return (bool)count($this->errors); |
|
160
|
|
|
} |
|
161
|
|
|
} |
|
162
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.