1 | <?php |
||
8 | class Validator implements SubSchemaValidatorFactory |
||
9 | { |
||
10 | /** |
||
11 | * @var array |
||
12 | */ |
||
13 | private $errors = []; |
||
14 | |||
15 | /** |
||
16 | * @var mixed |
||
17 | */ |
||
18 | private $data; |
||
19 | |||
20 | /** |
||
21 | * @var object |
||
22 | */ |
||
23 | private $schema; |
||
24 | |||
25 | /** |
||
26 | * @var string |
||
27 | */ |
||
28 | private $pointer = ''; |
||
29 | |||
30 | /** |
||
31 | * The maximum depth the validator should recurse into $data |
||
32 | * before throwing an exception. |
||
33 | * |
||
34 | * @var int |
||
35 | */ |
||
36 | private $maxDepth = 50; |
||
37 | |||
38 | /** |
||
39 | * The depth the validator has reached in the data. |
||
40 | * |
||
41 | * @var int |
||
42 | */ |
||
43 | private $depth = 0; |
||
44 | |||
45 | /** |
||
46 | * @var \League\JsonGuard\FormatExtension[] |
||
47 | */ |
||
48 | private $formatExtensions = []; |
||
49 | |||
50 | /** |
||
51 | * @var \League\JsonGuard\RuleSet |
||
52 | */ |
||
53 | private $ruleSet; |
||
54 | |||
55 | /** |
||
56 | * @var bool |
||
57 | */ |
||
58 | private $hasValidated; |
||
59 | |||
60 | /** |
||
61 | * @param mixed $data |
||
62 | * @param object $schema |
||
63 | * @param RuleSet|null $ruleSet |
||
64 | */ |
||
65 | 236 | public function __construct($data, $schema, RuleSet $ruleSet = null) |
|
82 | |||
83 | /** |
||
84 | * @return boolean |
||
85 | */ |
||
86 | 142 | public function fails() |
|
90 | |||
91 | /** |
||
92 | * @return boolean |
||
93 | */ |
||
94 | 142 | public function passes() |
|
98 | |||
99 | /** |
||
100 | * Get a collection of errors. |
||
101 | * |
||
102 | * @return ValidationError[] |
||
103 | */ |
||
104 | 210 | public function errors() |
|
110 | |||
111 | /** |
||
112 | * Set the maximum allowed depth data will be validated until. |
||
113 | * If the data exceeds the stack depth an exception is thrown. |
||
114 | * |
||
115 | * @param int $maxDepth |
||
116 | * |
||
117 | * @return $this |
||
118 | */ |
||
119 | 6 | public function setMaxDepth($maxDepth) |
|
125 | |||
126 | /** |
||
127 | * Register a custom format validation extension. |
||
128 | * |
||
129 | * @param string $format |
||
130 | * @param FormatExtension $extension |
||
131 | */ |
||
132 | 4 | public function registerFormatExtension($format, FormatExtension $extension) |
|
136 | |||
137 | /** |
||
138 | * @return string |
||
139 | */ |
||
140 | 234 | public function getPointer() |
|
144 | |||
145 | /** |
||
146 | * @return mixed |
||
147 | */ |
||
148 | public function getData() |
||
152 | |||
153 | /** |
||
154 | * @return object |
||
155 | */ |
||
156 | 58 | public function getSchema() |
|
160 | |||
161 | /** |
||
162 | * Create a new sub-validator. |
||
163 | * |
||
164 | * @param mixed $data |
||
165 | * @param object $schema |
||
166 | * @param string|null $pointer |
||
167 | * |
||
168 | * @return Validator |
||
169 | */ |
||
170 | 84 | public function makeSubSchemaValidator($data, $schema, $pointer = null) |
|
181 | |||
182 | /** |
||
183 | * Validate the data and collect the errors. |
||
184 | */ |
||
185 | 210 | private function validate() |
|
200 | |||
201 | /** |
||
202 | * Keep track of how many levels deep we have validated. |
||
203 | * This is to prevent a really deeply nested JSON |
||
204 | * structure from causing the validator to continue |
||
205 | * validating for an incredibly long time. |
||
206 | * |
||
207 | * @throws \League\JsonGuard\Exceptions\MaximumDepthExceededException |
||
208 | */ |
||
209 | 210 | private function checkDepth() |
|
215 | |||
216 | /** |
||
217 | * Validate the data using the given rule and parameter. |
||
218 | * |
||
219 | * @param string $rule |
||
220 | * @param mixed $parameter |
||
221 | * |
||
222 | * @return null|ValidationError|ValidationError[] |
||
223 | */ |
||
224 | 210 | private function validateRule($rule, $parameter) |
|
238 | |||
239 | /** |
||
240 | * Determine if a rule has a custom format extension registered. |
||
241 | * |
||
242 | * @param string $rule |
||
243 | * @param mixed $parameter |
||
244 | * |
||
245 | * @return bool |
||
246 | */ |
||
247 | 210 | private function isCustomFormatExtension($rule, $parameter) |
|
253 | |||
254 | /** |
||
255 | * Call a custom format extension to validate the data. |
||
256 | * |
||
257 | * @param string $format |
||
258 | * |
||
259 | * @return ValidationError|null |
||
260 | */ |
||
261 | 4 | private function validateCustomFormat($format) |
|
268 | |||
269 | /** |
||
270 | * Merge the errors with our error collection. |
||
271 | * |
||
272 | * @param ValidationError[]|ValidationError|null $errors |
||
273 | */ |
||
274 | 140 | private function mergeErrors($errors) |
|
283 | } |
||
284 |