1 | <?php |
||
12 | class Validator |
||
13 | { |
||
14 | /** @var FailureCollection */ |
||
15 | protected $failures; |
||
16 | |||
17 | /** @var ValidateSpec[] */ |
||
18 | protected $validateSpecs = []; |
||
19 | |||
20 | /** @var SanitizeSpec[] */ |
||
21 | protected $sanitizeSpecs = []; |
||
22 | |||
23 | /** |
||
24 | * Fields to skip during validation. |
||
25 | * |
||
26 | * @var array |
||
27 | */ |
||
28 | protected $skip = []; |
||
29 | |||
30 | /** |
||
31 | * Messages to use for a field. |
||
32 | * |
||
33 | * Index is the fieldName, value is the message. |
||
34 | * |
||
35 | * @var array |
||
36 | */ |
||
37 | protected $filedMessages = []; |
||
38 | |||
39 | /** |
||
40 | * Validator constructor. |
||
41 | * |
||
42 | * @param FailureCollection $failureCollection |
||
43 | */ |
||
44 | 54 | public function __construct( |
|
50 | |||
51 | /** |
||
52 | * Asserts the validator, throws a ValidationFailureException if anything fails |
||
53 | * |
||
54 | * @param $subject |
||
55 | * |
||
56 | * @throws ValidationFailureException |
||
57 | * |
||
58 | * @return bool |
||
59 | */ |
||
60 | public function __invoke(&$subject): bool |
||
64 | |||
65 | /** |
||
66 | * Hook function that can be implemented in an extended custom validator class. |
||
67 | * |
||
68 | * If a custom class extends Validator, this method is the hook to provide some default rule configuration. |
||
69 | */ |
||
70 | 54 | protected function init(): void |
|
74 | |||
75 | /** |
||
76 | * Returns the collection of validation failures. |
||
77 | * |
||
78 | * @return FailureCollection |
||
79 | */ |
||
80 | 21 | public function getFailures(): FailureCollection |
|
84 | |||
85 | /** |
||
86 | * @param string $fieldName |
||
87 | * @param string $message |
||
88 | * |
||
89 | * @return Validator |
||
90 | */ |
||
91 | 3 | public function setFieldMessage(string $fieldName, string $message): self |
|
97 | |||
98 | /** |
||
99 | * Configure the validator to validate the given $field, with the given $rule. |
||
100 | * |
||
101 | * @param string $field |
||
102 | * |
||
103 | * @return ValidateSpec |
||
104 | */ |
||
105 | 39 | public function validate(string $field): ValidateSpec |
|
112 | |||
113 | /** |
||
114 | * Configure the validator to sanitize the given $field, with the given $rule. |
||
115 | * |
||
116 | * @param string $field |
||
117 | * @param string $ruleName |
||
|
|||
118 | * @param array $args |
||
119 | * |
||
120 | * @return SanitizeSpec |
||
121 | */ |
||
122 | 18 | public function sanitize(string $field): SanitizeSpec |
|
129 | |||
130 | /** |
||
131 | * Applies the validator to the subject and throws an exception upon failure |
||
132 | * |
||
133 | * @param $subject |
||
134 | * |
||
135 | * @throws ValidationFailureException |
||
136 | * |
||
137 | * @return bool |
||
138 | */ |
||
139 | public function assert(&$subject) |
||
152 | |||
153 | /** |
||
154 | * Applies the configured validate and sanitize rules to the given $subject. |
||
155 | * |
||
156 | * @param array|object $subject |
||
157 | * |
||
158 | * @return bool |
||
159 | */ |
||
160 | 51 | public function apply(&$subject): bool |
|
168 | |||
169 | /** |
||
170 | * Handles applying all rules to an array. |
||
171 | * |
||
172 | * The array gets type casted to an object then passed to `$this->applyToObject`. because this is all done by |
||
173 | * reference, we can type cast it back to an array _after_ the `applyToObject()` call and return the $subject with |
||
174 | * any values that many have been alerted by sanitize rules. |
||
175 | * |
||
176 | * @param array $subject |
||
177 | * |
||
178 | * @return bool |
||
179 | */ |
||
180 | 6 | protected function applyToArray(array &$subject): bool |
|
188 | |||
189 | /** |
||
190 | * Applies all sanitize and validate specs to a given object. |
||
191 | * |
||
192 | * Sanitize specs run first |
||
193 | * |
||
194 | * @param $subject |
||
195 | * |
||
196 | * @return bool |
||
197 | */ |
||
198 | 51 | protected function applyToObject($subject): bool |
|
219 | |||
220 | /** |
||
221 | * Applies a given spec to the subject. |
||
222 | * |
||
223 | * If the spec returns false, this will log the error. |
||
224 | * |
||
225 | * @param object $subject |
||
226 | * @param AbstractSpec $spec |
||
227 | * |
||
228 | * @return bool |
||
229 | */ |
||
230 | 51 | protected function applySpec($subject, AbstractSpec $spec): bool |
|
248 | |||
249 | /** |
||
250 | * Handles failing a spec. |
||
251 | * |
||
252 | * If the spec is set to a HardFailure, add its field to the skip list. |
||
253 | * |
||
254 | * @param AbstractSpec $spec |
||
255 | */ |
||
256 | 24 | protected function failSpec(AbstractSpec $spec): void |
|
270 | } |
||
271 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.