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 | * |
||
121 | * @return $this |
||
122 | 42 | */ |
|
123 | public function setMaxDepth($maxDepth) |
||
129 | |||
130 | /** |
||
131 | * Register a custom format validation extension. |
||
132 | * |
||
133 | * @param string $format |
||
134 | * @param FormatExtension $extension |
||
135 | 4 | */ |
|
136 | public function registerFormatExtension($format, FormatExtension $extension) |
||
140 | |||
141 | /** |
||
142 | * @internal |
||
143 | * @return string |
||
144 | */ |
||
145 | 42 | public function getPointer() |
|
149 | 42 | ||
150 | /** |
||
151 | * Create a new sub-validator. |
||
152 | * |
||
153 | * @param mixed $data |
||
154 | * @param object $schema |
||
155 | * @param string $pointer |
||
156 | * |
||
157 | 42 | * @return Validator |
|
158 | */ |
||
159 | 42 | public function makeSubSchemaValidator($data, $schema, $pointer) |
|
170 | 76 | ||
171 | /** |
||
172 | * Validate the data and collect the errors. |
||
173 | */ |
||
174 | private function validate() |
||
189 | |||
190 | /** |
||
191 | * Keep track of how many levels deep we have validated. |
||
192 | * This is to prevent a really deeply nested JSON |
||
193 | 42 | * structure from causing the validator to continue |
|
194 | * validating for an incredibly long time. |
||
195 | 42 | * |
|
196 | 42 | * @throws \League\JsonGuard\Exceptions\MaximumDepthExceededException |
|
197 | 42 | */ |
|
198 | 42 | private function checkDepth() |
|
204 | |||
205 | 76 | /** |
|
206 | * Validate the data using the given rule and parameter. |
||
207 | 76 | * |
|
208 | 28 | * @param string $rule |
|
209 | * @param mixed $parameter |
||
210 | * |
||
211 | 76 | * @return null|ValidationError|ValidationError[] |
|
212 | */ |
||
213 | 76 | private function validateRule($rule, $parameter) |
|
227 | |||
228 | /** |
||
229 | 76 | * Invoke the given constraint and return the validation errors. |
|
230 | * |
||
231 | 76 | * @param \League\JsonGuard\Constraints\Constraint $constraint |
|
232 | 4 | * @param mixed $parameter |
|
233 | * |
||
234 | 76 | * @return \League\JsonGuard\ValidationError|\League\JsonGuard\ValidationError[]|null |
|
235 | */ |
||
236 | private function invokeConstraint(Constraint $constraint, $parameter) |
||
250 | 4 | ||
251 | /** |
||
252 | * Determine if a rule has a custom format extension registered. |
||
253 | 74 | * |
|
254 | * @param string $rule |
||
255 | 74 | * @param mixed $parameter |
|
256 | * |
||
257 | * @return bool |
||
258 | */ |
||
259 | private function isCustomFormatExtension($rule, $parameter) |
||
263 | |||
264 | /** |
||
265 | * Call a custom format extension to validate the data. |
||
266 | 74 | * |
|
267 | * @param string $format |
||
268 | 74 | * |
|
269 | 62 | * @return ValidationError|null |
|
270 | 48 | */ |
|
271 | 20 | private function validateCustomFormat($format) |
|
278 | |||
279 | /** |
||
280 | * Merge the errors with our error collection. |
||
281 | * |
||
282 | * @param ValidationError[]|ValidationError|null $errors |
||
283 | */ |
||
284 | private function mergeErrors($errors) |
||
298 | } |
||
299 |