1 | <?php |
||
13 | class Validator implements SubSchemaValidatorFactory |
||
14 | { |
||
15 | /** |
||
16 | * @var array |
||
17 | */ |
||
18 | private $errors = []; |
||
19 | |||
20 | /** |
||
21 | * @var mixed |
||
22 | */ |
||
23 | private $data; |
||
24 | |||
25 | /** |
||
26 | * @var object |
||
27 | */ |
||
28 | private $schema; |
||
29 | |||
30 | /** |
||
31 | * @var string |
||
32 | */ |
||
33 | private $pointer = ''; |
||
34 | |||
35 | /** |
||
36 | * The maximum depth the validator should recurse into $data |
||
37 | * before throwing an exception. |
||
38 | * |
||
39 | * @var int |
||
40 | */ |
||
41 | private $maxDepth = 50; |
||
42 | |||
43 | /** |
||
44 | * The depth the validator has reached in the data. |
||
45 | * |
||
46 | * @var int |
||
47 | */ |
||
48 | private $depth = 0; |
||
49 | |||
50 | /** |
||
51 | * @var \League\JsonGuard\FormatExtension[] |
||
52 | */ |
||
53 | private $formatExtensions = []; |
||
54 | |||
55 | /** |
||
56 | * @var \League\JsonGuard\RuleSet |
||
57 | */ |
||
58 | private $ruleSet; |
||
59 | |||
60 | /** |
||
61 | * @var bool |
||
62 | */ |
||
63 | private $hasValidated; |
||
64 | |||
65 | /** |
||
66 | * @param mixed $data |
||
67 | * @param object $schema |
||
68 | * @param RuleSet|null $ruleSet |
||
69 | */ |
||
70 | 78 | public function __construct($data, $schema, $ruleSet = null) |
|
86 | |||
87 | /** |
||
88 | * @return boolean |
||
89 | */ |
||
90 | 76 | public function fails() |
|
94 | |||
95 | /** |
||
96 | * @return boolean |
||
97 | */ |
||
98 | 76 | public function passes() |
|
102 | |||
103 | /** |
||
104 | * Get a collection of errors. |
||
105 | * |
||
106 | * @return ValidationError[] |
||
107 | */ |
||
108 | 76 | public function errors() |
|
114 | |||
115 | /** |
||
116 | * Set the maximum allowed depth data will be validated until. |
||
117 | * If the data exceeds the stack depth an exception is thrown. |
||
118 | * |
||
119 | * @param int $maxDepth |
||
120 | * @return $this |
||
121 | */ |
||
122 | 42 | public function setMaxDepth($maxDepth) |
|
128 | |||
129 | /** |
||
130 | * Register a custom format validation extension. |
||
131 | * |
||
132 | * @param string $format |
||
133 | * @param FormatExtension $extension |
||
134 | */ |
||
135 | 4 | public function registerFormatExtension($format, FormatExtension $extension) |
|
139 | |||
140 | /** |
||
141 | * @internal |
||
142 | * @param FormatExtension[] $formatExtensions |
||
143 | * @return $this |
||
144 | */ |
||
145 | 42 | public function setFormatExtensions(array $formatExtensions) |
|
151 | |||
152 | /** |
||
153 | * @internal |
||
154 | * @param int $depth |
||
155 | * @return $this |
||
156 | */ |
||
157 | 42 | public function setDepth($depth) |
|
163 | |||
164 | /** |
||
165 | * @internal |
||
166 | * @return string |
||
167 | */ |
||
168 | 76 | public function getPointer() |
|
172 | |||
173 | /** |
||
174 | * @internal |
||
175 | * @param string $pointer |
||
176 | * @return $this |
||
177 | */ |
||
178 | 42 | public function setPointer($pointer) |
|
184 | |||
185 | /** |
||
186 | * Create a new sub-validator. |
||
187 | * |
||
188 | * @param mixed $data |
||
189 | * @param object $schema |
||
190 | * @param string $pointer |
||
191 | * @return Validator |
||
192 | */ |
||
193 | 42 | public function makeSubSchemaValidator($data, $schema, $pointer) |
|
201 | |||
202 | /** |
||
203 | * Validate the data and collect the errors. |
||
204 | */ |
||
205 | 76 | private function validate() |
|
220 | |||
221 | /** |
||
222 | * Keep track of how many levels deep we have validated. |
||
223 | * This is to prevent a really deeply nested JSON |
||
224 | * structure from causing the validator to continue |
||
225 | * validating for an incredibly long time. |
||
226 | * |
||
227 | * @throws \League\JsonGuard\Exceptions\MaximumDepthExceededException |
||
228 | */ |
||
229 | 76 | private function checkDepth() |
|
235 | |||
236 | /** |
||
237 | * Validate the data using the given rule and parameter. |
||
238 | * |
||
239 | * @param string $rule |
||
240 | * @param mixed $parameter |
||
241 | * @return null|ValidationError|ValidationError[] |
||
242 | */ |
||
243 | 76 | private function validateRule($rule, $parameter) |
|
257 | |||
258 | /** |
||
259 | * Invoke the given constraint and return the validation errors. |
||
260 | * |
||
261 | * @param \League\JsonGuard\Constraints\Constraint $constraint |
||
262 | * @param mixed $parameter |
||
263 | * |
||
264 | * @return \League\JsonGuard\ValidationError|\League\JsonGuard\ValidationError[]|null |
||
265 | */ |
||
266 | 74 | private function invokeConstraint(Constraint $constraint, $parameter) |
|
280 | |||
281 | /** |
||
282 | * Determine if a rule has a custom format extension registered. |
||
283 | * |
||
284 | * @param string $rule |
||
285 | * @param mixed $parameter |
||
286 | * |
||
287 | * @return bool |
||
288 | */ |
||
289 | 76 | private function isCustomFormatExtension($rule, $parameter) |
|
293 | |||
294 | /** |
||
295 | * Call a custom format extension to validate the data. |
||
296 | * |
||
297 | * @param string $format |
||
298 | * |
||
299 | * @return ValidationError|null |
||
300 | */ |
||
301 | 4 | private function validateCustomFormat($format) |
|
308 | |||
309 | /** |
||
310 | * Merge the errors with our error collection. |
||
311 | * |
||
312 | * @param ValidationError[]|ValidationError|null $errors |
||
313 | */ |
||
314 | 74 | private function mergeErrors($errors) |
|
327 | } |
||
328 |