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 = 10; |
||
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 | 72 | public function __construct($data, $schema, $ruleSet = null) |
|
86 | |||
87 | /** |
||
88 | * @return boolean |
||
89 | */ |
||
90 | 72 | public function fails() |
|
91 | { |
||
92 | 72 | return !$this->passes(); |
|
93 | } |
||
94 | |||
95 | /** |
||
96 | * @return boolean |
||
97 | */ |
||
98 | 72 | public function passes() |
|
102 | |||
103 | /** |
||
104 | * Get a collection of errors. |
||
105 | * |
||
106 | * @return ValidationError[] |
||
107 | */ |
||
108 | 72 | 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 | 38 | public function setMaxDepth($maxDepth) |
|
128 | |||
129 | /** |
||
130 | * Register a custom format validation extension. |
||
131 | * |
||
132 | * @param string $format |
||
133 | * @param FormatExtension $extension |
||
134 | */ |
||
135 | 2 | public function registerFormatExtension($format, FormatExtension $extension) |
|
139 | |||
140 | /** |
||
141 | * @internal |
||
142 | * @param int $depth |
||
143 | * @return $this |
||
144 | */ |
||
145 | 38 | public function setDepth($depth) |
|
151 | |||
152 | /** |
||
153 | * @internal |
||
154 | * @return string |
||
155 | */ |
||
156 | 72 | public function getPointer() |
|
160 | |||
161 | /** |
||
162 | * @internal |
||
163 | * @param string $pointer |
||
164 | * @return $this |
||
165 | */ |
||
166 | 38 | public function setPointer($pointer) |
|
172 | |||
173 | /** |
||
174 | * Create a new sub-validator. |
||
175 | * |
||
176 | * @param mixed $data |
||
177 | * @param object $schema |
||
178 | * @param string $pointer |
||
179 | * @return Validator |
||
180 | */ |
||
181 | 38 | public function makeSubSchemaValidator($data, $schema, $pointer) |
|
188 | |||
189 | /** |
||
190 | * Validate the data and collect the errors. |
||
191 | */ |
||
192 | 72 | private function validate() |
|
207 | |||
208 | /** |
||
209 | * Keep track of how many levels deep we have validated. |
||
210 | * This is to prevent a really deeply nested JSON |
||
211 | * structure from causing the validator to continue |
||
212 | * validating for an incredibly long time. |
||
213 | * |
||
214 | * @throws \League\JsonGuard\Exceptions\MaximumDepthExceededException |
||
215 | */ |
||
216 | 72 | private function checkDepth() |
|
222 | |||
223 | /** |
||
224 | * Validate the data using the given rule and parameter. |
||
225 | * |
||
226 | * @param string $rule |
||
227 | * @param mixed $parameter |
||
228 | * @return null|ValidationError|ValidationError[] |
||
229 | */ |
||
230 | 72 | private function validateRule($rule, $parameter) |
|
244 | |||
245 | /** |
||
246 | * Invoke the given constraint and return the validation errors. |
||
247 | * |
||
248 | * @param \League\JsonGuard\Constraints\Constraint $constraint |
||
249 | * @param mixed $parameter |
||
250 | * |
||
251 | * @return \League\JsonGuard\ValidationError|\League\JsonGuard\ValidationError[]|null |
||
252 | */ |
||
253 | 70 | private function invokeConstraint(Constraint $constraint, $parameter) |
|
267 | |||
268 | /** |
||
269 | * Determine if a rule has a custom format extension registered. |
||
270 | * |
||
271 | * @param string $rule |
||
272 | * @param mixed $parameter |
||
273 | * |
||
274 | * @return bool |
||
275 | */ |
||
276 | 72 | private function isCustomFormatExtension($rule, $parameter) |
|
280 | |||
281 | /** |
||
282 | * Call a custom format extension to validate the data. |
||
283 | * |
||
284 | * @param string $format |
||
285 | * |
||
286 | * @return ValidationError|null |
||
287 | */ |
||
288 | 2 | private function validateCustomFormat($format) |
|
295 | |||
296 | /** |
||
297 | * Merge the errors with our error collection. |
||
298 | * |
||
299 | * @param ValidationError[]|ValidationError|null $errors |
||
300 | */ |
||
301 | 70 | private function mergeErrors($errors) |
|
314 | } |
||
315 |