Passed
Push — master ( b5c1c2...fed690 )
by Todd
29s queued 10s
created

Schema::setValidationFactory()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 4
ccs 4
cts 4
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @author Todd Burry <[email protected]>
4
 * @copyright 2009-2018 Vanilla Forums Inc.
5
 * @license MIT
6
 */
7
8
namespace Garden\Schema;
9
10
/**
11
 * A class for defining and validating data schemas.
12
 */
13
class Schema implements \JsonSerializable, \ArrayAccess {
14
    /**
15
     * Trigger a notice when extraneous properties are encountered during validation.
16
     */
17
    const VALIDATE_EXTRA_PROPERTY_NOTICE = 0x1;
18
19
    /**
20
     * Throw a ValidationException when extraneous properties are encountered during validation.
21
     */
22
    const VALIDATE_EXTRA_PROPERTY_EXCEPTION = 0x2;
23
24
    /**
25
     * @var array All the known types.
26
     *
27
     * If this is ever given some sort of public access then remove the static.
28
     */
29
    private static $types = [
30
        'array' => ['a'],
31
        'object' => ['o'],
32
        'integer' => ['i', 'int'],
33
        'string' => ['s', 'str'],
34
        'number' => ['f', 'float'],
35
        'boolean' => ['b', 'bool'],
36
37
        // Psuedo-types
38
        'timestamp' => ['ts'], // type: integer, format: timestamp
39
        'datetime' => ['dt'], // type: string, format: date-time
40
        'null' => ['n'], // Adds nullable: true
41
    ];
42
43
    /**
44
     * @var string The regular expression to strictly determine if a string is a date.
45
     */
46
    private static $DATE_REGEX = '`^\d{4}-\d{2}-\d{2}([ T]\d{2}:\d{2}(:\d{2})?)?`i';
47
48
    private $schema = [];
49
50
    /**
51
     * @var int A bitwise combination of the various **Schema::FLAG_*** constants.
52
     */
53
    private $flags = 0;
54
55
    /**
56
     * @var array An array of callbacks that will filter data in the schema.
57
     */
58
    private $filters = [];
59
60
    /**
61
     * @var array An array of callbacks that will custom validate the schema.
62
     */
63
    private $validators = [];
64
65
    /**
66
     * @var string|Validation The name of the class or an instance that will be cloned.
67
     * @deprecated
68
     */
69
    private $validationClass = Validation::class;
70
71
    /**
72
     * @var callable A callback is used to create validation objects.
73
     */
74
    private $validationFactory;
75
76
    /**
77
     * @var callable
78
     */
79
    private $refLookup;
80
81
    /// Methods ///
82
83
    /**
84
     * Initialize an instance of a new {@link Schema} class.
85
     *
86
     * @param array $schema The array schema to validate against.
87
     */
88 264
    public function __construct(array $schema = []) {
89 264
        $this->schema = $schema;
90
        $this->refLookup = function (string $name) {
0 ignored issues
show
Unused Code introduced by
The parameter $name is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

90
        $this->refLookup = function (/** @scrutinizer ignore-unused */ string $name) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
91
            return null;
92
        };
93
        $this->validationFactory = function () {
94 206
            return new Validation();
95
        };
96 264
    }
97
98
    /**
99
     * Grab the schema's current description.
100
     *
101
     * @return string
102
     */
103 1
    public function getDescription(): string {
104 1
        return $this->schema['description'] ?? '';
105
    }
106
107
    /**
108
     * Set the description for the schema.
109
     *
110
     * @param string $description The new description.
111
     * @return $this
112
     */
113 1
    public function setDescription(string $description) {
114 1
        $this->schema['description'] = $description;
115 1
        return $this;
116
    }
117
118
    /**
119
     * Get the schema's title.
120
     *
121
     * @return string Returns the title.
122
     */
123
    public function getTitle(): string {
124
        return $this->schema['title'] ?? '';
125
    }
126
127
    /**
128
     * Set the schema's title.
129
     *
130
     * @param string $title The new title.
131
     */
132
    public function setTitle(string $title) {
133
        $this->schema['title'] = $title;
134
    }
135
136
    /**
137
     * Get a schema field.
138
     *
139
     * @param string|array $path The JSON schema path of the field with parts separated by dots.
140
     * @param mixed $default The value to return if the field isn't found.
141
     * @return mixed Returns the field value or `$default`.
142
     */
143 10
    public function getField($path, $default = null) {
144 10
        if (is_string($path)) {
145 10
            if (strpos($path, '.') !== false && strpos($path, '/') === false) {
146 1
                trigger_error('Field selectors must be separated by "/" instead of "."', E_USER_DEPRECATED);
147 1
                $path = explode('.', $path);
148
            } else {
149 9
                $path = explode('/', $path);
150
            }
151
        }
152
153 10
        $value = $this->schema;
154 10
        foreach ($path as $i => $subKey) {
155 10
            if (is_array($value) && isset($value[$subKey])) {
156 10
                $value = $value[$subKey];
157 1
            } elseif ($value instanceof Schema) {
158 1
                return $value->getField(array_slice($path, $i), $default);
159
            } else {
160 10
                return $default;
161
            }
162
        }
163 10
        return $value;
164
    }
165
166
    /**
167
     * Set a schema field.
168
     *
169
     * @param string|array $path The JSON schema path of the field with parts separated by slashes.
170
     * @param mixed $value The new value.
171
     * @return $this
172
     */
173 4
    public function setField($path, $value) {
174 4
        if (is_string($path)) {
175 4
            if (strpos($path, '.') !== false && strpos($path, '/') === false) {
176 1
                trigger_error('Field selectors must be separated by "/" instead of "."', E_USER_DEPRECATED);
177 1
                $path = explode('.', $path);
178
            } else {
179 3
                $path = explode('/', $path);
180
            }
181
        }
182
183 4
        $selection = &$this->schema;
184 4
        foreach ($path as $i => $subSelector) {
185 4
            if (is_array($selection)) {
186 4
                if (!isset($selection[$subSelector])) {
187 4
                    $selection[$subSelector] = [];
188
                }
189 1
            } elseif ($selection instanceof Schema) {
190 1
                $selection->setField(array_slice($path, $i), $value);
191 1
                return $this;
192
            } else {
193
                $selection = [$subSelector => []];
194
            }
195 4
            $selection = &$selection[$subSelector];
196
        }
197
198 4
        $selection = $value;
199 4
        return $this;
200
    }
201
202
    /**
203
     * Get the ID for the schema.
204
     *
205
     * @return string
206
     */
207 3
    public function getID(): string {
208 3
        return isset($this->schema['id']) ? $this->schema['id'] : '';
209
    }
210
211
    /**
212
     * Set the ID for the schema.
213
     *
214
     * @param string $id The new ID.
215
     * @throws \InvalidArgumentException Throws an exception when the provided ID is not a string.
216
     * @return Schema
217
     */
218 1
    public function setID(string $id) {
219 1
        if (is_string($id)) {
0 ignored issues
show
introduced by
The condition is_string($id) is always true.
Loading history...
220 1
            $this->schema['id'] = $id;
221
        } else {
222
            throw new \InvalidArgumentException("The ID is not a valid string.", 500);
223
        }
224
225 1
        return $this;
226
    }
227
228
    /**
229
     * Return the validation flags.
230
     *
231
     * @return int Returns a bitwise combination of flags.
232
     */
233 1
    public function getFlags(): int {
234 1
        return $this->flags;
235
    }
236
237
    /**
238
     * Set the validation flags.
239
     *
240
     * @param int $flags One or more of the **Schema::FLAG_*** constants.
241
     * @return Schema Returns the current instance for fluent calls.
242
     */
243 7
    public function setFlags(int $flags) {
244 7
        if (!is_int($flags)) {
0 ignored issues
show
introduced by
The condition is_int($flags) is always true.
Loading history...
245
            throw new \InvalidArgumentException('Invalid flags.', 500);
246
        }
247 7
        $this->flags = $flags;
248
249 7
        return $this;
250
    }
251
252
    /**
253
     * Whether or not the schema has a flag (or combination of flags).
254
     *
255
     * @param int $flag One or more of the **Schema::VALIDATE_*** constants.
256
     * @return bool Returns **true** if all of the flags are set or **false** otherwise.
257
     */
258 12
    public function hasFlag(int $flag): bool {
259 12
        return ($this->flags & $flag) === $flag;
260
    }
261
262
    /**
263
     * Set a flag.
264
     *
265
     * @param int $flag One or more of the **Schema::VALIDATE_*** constants.
266
     * @param bool $value Either true or false.
267
     * @return $this
268
     */
269 1
    public function setFlag(int $flag, bool $value) {
270 1
        if ($value) {
271 1
            $this->flags = $this->flags | $flag;
272
        } else {
273 1
            $this->flags = $this->flags & ~$flag;
274
        }
275 1
        return $this;
276
    }
277
278
    /**
279
     * Merge a schema with this one.
280
     *
281
     * @param Schema $schema A scheme instance. Its parameters will be merged into the current instance.
282
     * @return $this
283
     */
284 4
    public function merge(Schema $schema) {
285 4
        $this->mergeInternal($this->schema, $schema->getSchemaArray(), true, true);
286 4
        return $this;
287
    }
288
289
    /**
290
     * Add another schema to this one.
291
     *
292
     * Adding schemas together is analogous to array addition. When you add a schema it will only add missing information.
293
     *
294
     * @param Schema $schema The schema to add.
295
     * @param bool $addProperties Whether to add properties that don't exist in this schema.
296
     * @return $this
297
     */
298 4
    public function add(Schema $schema, $addProperties = false) {
299 4
        $this->mergeInternal($this->schema, $schema->getSchemaArray(), false, $addProperties);
300 4
        return $this;
301
    }
302
303
    /**
304
     * The internal implementation of schema merging.
305
     *
306
     * @param array &$target The target of the merge.
307
     * @param array $source The source of the merge.
308
     * @param bool $overwrite Whether or not to replace values.
309
     * @param bool $addProperties Whether or not to add object properties to the target.
310
     * @return array
311
     */
312 7
    private function mergeInternal(array &$target, array $source, $overwrite = true, $addProperties = true) {
313
        // We need to do a fix for required properties here.
314 7
        if (isset($target['properties']) && !empty($source['required'])) {
315 5
            $required = isset($target['required']) ? $target['required'] : [];
316
317 5
            if (isset($source['required']) && $addProperties) {
318 4
                $newProperties = array_diff(array_keys($source['properties']), array_keys($target['properties']));
319 4
                $newRequired = array_intersect($source['required'], $newProperties);
320
321 4
                $required = array_merge($required, $newRequired);
322
            }
323
        }
324
325
326 7
        foreach ($source as $key => $val) {
327 7
            if (is_array($val) && array_key_exists($key, $target) && is_array($target[$key])) {
328 7
                if ($key === 'properties' && !$addProperties) {
329
                    // We just want to merge the properties that exist in the destination.
330 2
                    foreach ($val as $name => $prop) {
331 2
                        if (isset($target[$key][$name])) {
332 2
                            $targetProp = &$target[$key][$name];
333
334 2
                            if (is_array($targetProp) && is_array($prop)) {
335 2
                                $this->mergeInternal($targetProp, $prop, $overwrite, $addProperties);
336 1
                            } elseif (is_array($targetProp) && $prop instanceof Schema) {
337
                                $this->mergeInternal($targetProp, $prop->getSchemaArray(), $overwrite, $addProperties);
338 1
                            } elseif ($overwrite) {
339 2
                                $targetProp = $prop;
340
                            }
341
                        }
342
                    }
343 7
                } elseif (isset($val[0]) || isset($target[$key][0])) {
344 5
                    if ($overwrite) {
345
                        // This is a numeric array, so just do a merge.
346 3
                        $merged = array_merge($target[$key], $val);
347 3
                        if (is_string($merged[0])) {
348 3
                            $merged = array_keys(array_flip($merged));
349
                        }
350 5
                        $target[$key] = $merged;
351
                    }
352
                } else {
353 7
                    $target[$key] = $this->mergeInternal($target[$key], $val, $overwrite, $addProperties);
354
                }
355 7
            } elseif (!$overwrite && array_key_exists($key, $target) && !is_array($val)) {
356
                // Do nothing, we aren't replacing.
357
            } else {
358 7
                $target[$key] = $val;
359
            }
360
        }
361
362 7
        if (isset($required)) {
363 5
            if (empty($required)) {
364 1
                unset($target['required']);
365
            } else {
366 5
                $target['required'] = $required;
367
            }
368
        }
369
370 7
        return $target;
371
    }
372
373
//    public function overlay(Schema $schema )
374
375
    /**
376
     * Returns the internal schema array.
377
     *
378
     * @return array
379
     * @see Schema::jsonSerialize()
380
     */
381 17
    public function getSchemaArray(): array {
382 17
        return $this->schema;
383
    }
384
385
    /**
386
     * Parse a short schema and return the associated schema.
387
     *
388
     * @param array $arr The schema array.
389
     * @param mixed[] $args Constructor arguments for the schema instance.
390
     * @return static Returns a new schema.
391
     */
392 178
    public static function parse(array $arr, ...$args) {
393 178
        $schema = new static([], ...$args);
0 ignored issues
show
Unused Code introduced by
The call to Garden\Schema\Schema::__construct() has too many arguments starting with $args. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

393
        $schema = /** @scrutinizer ignore-call */ new static([], ...$args);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
394 178
        $schema->schema = $schema->parseInternal($arr);
395 177
        return $schema;
396
    }
397
398
    /**
399
     * Parse a schema in short form into a full schema array.
400
     *
401
     * @param array $arr The array to parse into a schema.
402
     * @return array The full schema array.
403
     * @throws \InvalidArgumentException Throws an exception when an item in the schema is invalid.
404
     */
405 178
    protected function parseInternal(array $arr): array {
406 178
        if (empty($arr)) {
407
            // An empty schema validates to anything.
408 6
            return [];
409 173
        } elseif (isset($arr['type'])) {
410
            // This is a long form schema and can be parsed as the root.
411 2
            return $this->parseNode($arr);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->parseNode($arr) could return the type ArrayAccess which is incompatible with the type-hinted return array. Consider adding an additional type-check to rule them out.
Loading history...
412
        } else {
413
            // Check for a root schema.
414 172
            $value = reset($arr);
415 172
            $key = key($arr);
416 172
            if (is_int($key)) {
417 107
                $key = $value;
418 107
                $value = null;
419
            }
420 172
            list ($name, $param) = $this->parseShortParam($key, $value);
421 171
            if (empty($name)) {
422 63
                return $this->parseNode($param, $value);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->parseNode($param, $value) could return the type ArrayAccess which is incompatible with the type-hinted return array. Consider adding an additional type-check to rule them out.
Loading history...
423
            }
424
        }
425
426
        // If we are here then this is n object schema.
427 111
        list($properties, $required) = $this->parseProperties($arr);
428
429
        $result = [
430 111
            'type' => 'object',
431 111
            'properties' => $properties,
432 111
            'required' => $required
433
        ];
434
435 111
        return array_filter($result);
436
    }
437
438
    /**
439
     * Parse a schema node.
440
     *
441
     * @param array $node The node to parse.
442
     * @param mixed $value Additional information from the node.
443
     * @return array|\ArrayAccess Returns a JSON schema compatible node.
444
     */
445 172
    private function parseNode($node, $value = null) {
446 172
        if (is_array($value)) {
447 66
            if (is_array($node['type'])) {
448
                trigger_error('Schemas with multiple types is deprecated.', E_USER_DEPRECATED);
449
            }
450
451
            // The value describes a bit more about the schema.
452 66
            switch ($node['type']) {
453 66
                case 'array':
454 11
                    if (isset($value['items'])) {
455
                        // The value includes array schema information.
456 4
                        $node = array_replace($node, $value);
457
                    } else {
458 7
                        $node['items'] = $this->parseInternal($value);
459
                    }
460 11
                    break;
461 56
                case 'object':
462
                    // The value is a schema of the object.
463 12
                    if (isset($value['properties'])) {
464
                        list($node['properties']) = $this->parseProperties($value['properties']);
465
                    } else {
466 12
                        list($node['properties'], $required) = $this->parseProperties($value);
467 12
                        if (!empty($required)) {
468 12
                            $node['required'] = $required;
469
                        }
470
                    }
471 12
                    break;
472
                default:
473 44
                    $node = array_replace($node, $value);
474 66
                    break;
475
            }
476 132
        } elseif (is_string($value)) {
477 102
            if ($node['type'] === 'array' && $arrType = $this->getType($value)) {
478 6
                $node['items'] = ['type' => $arrType];
479 98
            } elseif (!empty($value)) {
480 102
                $node['description'] = $value;
481
            }
482 35
        } elseif ($value === null) {
483
            // Parse child elements.
484 31
            if ($node['type'] === 'array' && isset($node['items'])) {
485
                // The value includes array schema information.
486
                $node['items'] = $this->parseInternal($node['items']);
487 31
            } elseif ($node['type'] === 'object' && isset($node['properties'])) {
488 1
                list($node['properties']) = $this->parseProperties($node['properties']);
489
            }
490
        }
491
492 172
        if (is_array($node)) {
493 171
            if (!empty($node['allowNull'])) {
494 1
                $node['nullable'] = true;
495
            }
496 171
            unset($node['allowNull']);
497
498 171
            if ($node['type'] === null || $node['type'] === []) {
499 4
                unset($node['type']);
500
            }
501
        }
502
503 172
        return $node;
504
    }
505
506
    /**
507
     * Parse the schema for an object's properties.
508
     *
509
     * @param array $arr An object property schema.
510
     * @return array Returns a schema array suitable to be placed in the **properties** key of a schema.
511
     */
512 112
    private function parseProperties(array $arr): array {
513 112
        $properties = [];
514 112
        $requiredProperties = [];
515 112
        foreach ($arr as $key => $value) {
516
            // Fix a schema specified as just a value.
517 112
            if (is_int($key)) {
518 82
                if (is_string($value)) {
519 82
                    $key = $value;
520 82
                    $value = '';
521
                } else {
522
                    throw new \InvalidArgumentException("Schema at position $key is not a valid parameter.", 500);
523
                }
524
            }
525
526
            // The parameter is defined in the key.
527 112
            list($name, $param, $required) = $this->parseShortParam($key, $value);
0 ignored issues
show
Bug introduced by
$value of type string is incompatible with the type array expected by parameter $value of Garden\Schema\Schema::parseShortParam(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

527
            list($name, $param, $required) = $this->parseShortParam($key, /** @scrutinizer ignore-type */ $value);
Loading history...
528
529 112
            $node = $this->parseNode($param, $value);
530
531 112
            $properties[$name] = $node;
532 112
            if ($required) {
533 112
                $requiredProperties[] = $name;
534
            }
535
        }
536 112
        return [$properties, $requiredProperties];
537
    }
538
539
    /**
540
     * Parse a short parameter string into a full array parameter.
541
     *
542
     * @param string $key The short parameter string to parse.
543
     * @param array $value An array of other information that might help resolve ambiguity.
544
     * @return array Returns an array in the form `[string name, array param, bool required]`.
545
     * @throws \InvalidArgumentException Throws an exception if the short param is not in the correct format.
546
     */
547 173
    public function parseShortParam(string $key, $value = []): array {
548
        // Is the parameter optional?
549 173
        if (substr($key, -1) === '?') {
550 70
            $required = false;
551 70
            $key = substr($key, 0, -1);
552
        } else {
553 125
            $required = true;
554
        }
555
556
        // Check for a type.
557 173
        if (false !== ($pos = strrpos($key, ':'))) {
558 167
            $name = substr($key, 0, $pos);
559 167
            $typeStr = substr($key, $pos + 1);
560
561
            // Kludge for names with colons that are not specifying an array of a type.
562 167
            if (isset($value['type']) && 'array' !== $this->getType($typeStr)) {
563 2
                $name = $key;
564 167
                $typeStr = '';
565
            }
566
        } else {
567 16
            $name = $key;
568 16
            $typeStr = '';
569
        }
570 173
        $types = [];
571 173
        $param = [];
572
573 173
        if (!empty($typeStr)) {
574 165
            $shortTypes = explode('|', $typeStr);
575 165
            foreach ($shortTypes as $alias) {
576 165
                $found = $this->getType($alias);
577 165
                if ($found === null) {
578
                    throw new \InvalidArgumentException("Unknown type '$alias'", 500);
579 165
                } elseif ($found === 'datetime') {
580 9
                    $param['format'] = 'date-time';
581 9
                    $types[] = 'string';
582 157
                } elseif ($found === 'timestamp') {
583 12
                    $param['format'] = 'timestamp';
584 12
                    $types[] = 'integer';
585 151
                } elseif ($found === 'null') {
586 11
                    $nullable = true;
587
                } else {
588 165
                    $types[] = $found;
589
                }
590
            }
591
        }
592
593 173
        if ($value instanceof Schema) {
594 6
            if (count($types) === 1 && $types[0] === 'array') {
595 1
                $param += ['type' => $types[0], 'items' => $value];
596
            } else {
597 6
                $param = $value;
598
            }
599 171
        } elseif (isset($value['type'])) {
600 10
            $param = $value + $param;
601
602 10
            if (!empty($types) && $types !== (array)$param['type']) {
603
                $typesStr = implode('|', $types);
604
                $paramTypesStr = implode('|', (array)$param['type']);
605
606 10
                throw new \InvalidArgumentException("Type mismatch between $typesStr and {$paramTypesStr} for field $name.", 500);
607
            }
608
        } else {
609 166
            if (empty($types) && !empty($parts[1])) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $parts seems to never exist and therefore empty should always be true.
Loading history...
610
                throw new \InvalidArgumentException("Invalid type {$parts[1]} for field $name.", 500);
611
            }
612 166
            if (empty($types)) {
613 4
                $param += ['type' => null];
614
            } else {
615 165
                $param += ['type' => count($types) === 1 ? $types[0] : $types];
616
            }
617
618
            // Parsed required strings have a minimum length of 1.
619 166
            if (in_array('string', $types) && !empty($name) && $required && (!isset($value['default']) || $value['default'] !== '')) {
620 41
                $param['minLength'] = 1;
621
            }
622
        }
623
624 173
        if (!empty($nullable)) {
625 11
            $param['nullable'] = true;
626
        }
627
628 173
        if (is_array($param['type'])) {
629 1
            trigger_error('Schemas with multiple types is deprecated.', E_USER_DEPRECATED);
630
        }
631
632 172
        return [$name, $param, $required];
633
    }
634
635
    /**
636
     * Add a custom filter to change data before validation.
637
     *
638
     * @param string $fieldname The name of the field to filter, if any.
639
     *
640
     * If you are adding a filter to a deeply nested field then separate the path with dots.
641
     * @param callable $callback The callback to filter the field.
642
     * @return $this
643
     */
644 2
    public function addFilter(string $fieldname, callable $callback) {
645 2
        $fieldname = $this->parseFieldSelector($fieldname);
646 2
        $this->filters[$fieldname][] = $callback;
647 2
        return $this;
648
    }
649
650
    /**
651
     * Add a custom validator to to validate the schema.
652
     *
653
     * @param string $fieldname The name of the field to validate, if any.
654
     *
655
     * If you are adding a validator to a deeply nested field then separate the path with dots.
656
     * @param callable $callback The callback to validate with.
657
     * @return Schema Returns `$this` for fluent calls.
658
     */
659 5
    public function addValidator(string $fieldname, callable $callback) {
660 5
        $fieldname = $this->parseFieldSelector($fieldname);
661 5
        $this->validators[$fieldname][] = $callback;
662 5
        return $this;
663
    }
664
665
    /**
666
     * Require one of a given set of fields in the schema.
667
     *
668
     * @param array $required The field names to require.
669
     * @param string $fieldname The name of the field to attach to.
670
     * @param int $count The count of required items.
671
     * @return Schema Returns `$this` for fluent calls.
672
     */
673 3
    public function requireOneOf(array $required, string $fieldname = '', int $count = 1) {
674 3
        $result = $this->addValidator(
675 3
            $fieldname,
676
            function ($data, ValidationField $field) use ($required, $count) {
677
                // This validator does not apply to sparse validation.
678 3
                if ($field->isSparse()) {
679 1
                    return true;
680
                }
681
682 2
                $hasCount = 0;
683 2
                $flattened = [];
684
685 2
                foreach ($required as $name) {
686 2
                    $flattened = array_merge($flattened, (array)$name);
687
688 2
                    if (is_array($name)) {
689
                        // This is an array of required names. They all must match.
690 1
                        $hasCountInner = 0;
691 1
                        foreach ($name as $nameInner) {
692 1
                            if (array_key_exists($nameInner, $data)) {
693 1
                                $hasCountInner++;
694
                            } else {
695 1
                                break;
696
                            }
697
                        }
698 1
                        if ($hasCountInner >= count($name)) {
699 1
                            $hasCount++;
700
                        }
701 2
                    } elseif (array_key_exists($name, $data)) {
702 1
                        $hasCount++;
703
                    }
704
705 2
                    if ($hasCount >= $count) {
706 2
                        return true;
707
                    }
708
                }
709
710 2
                if ($count === 1) {
711 1
                    $message = 'One of {required} are required.';
712
                } else {
713 1
                    $message = '{count} of {required} are required.';
714
                }
715
716 2
                $field->addError('missingField', [
717 2
                    'messageCode' => $message,
718 2
                    'required' => $required,
719 2
                    'count' => $count
720
                ]);
721 2
                return false;
722 3
            }
723
        );
724
725 3
        return $result;
726
    }
727
728
    /**
729
     * Validate data against the schema.
730
     *
731
     * @param mixed $data The data to validate.
732
     * @param array $options Validation options.
733
     *
734
     * - **sparse**: Whether or not this is a sparse validation.
735
     * @return mixed Returns a cleaned version of the data.
736
     * @throws ValidationException Throws an exception when the data does not validate against the schema.
737
     * @throws RefNotFoundException Throws an exception when a schema `$ref` is not found.
738
     */
739 211
    public function validate($data, $options = []) {
740 211
        if (is_bool($options)) {
0 ignored issues
show
introduced by
The condition is_bool($options) is always false.
Loading history...
741 1
            trigger_error('The $sparse parameter is deprecated. Use [\'sparse\' => true] instead.', E_USER_DEPRECATED);
742 1
            $options = ['sparse' => true];
743
        }
744 211
        $options += ['sparse' => false];
745
746
747 211
        list($schema, $schemaPath) = $this->lookupSchema($this->schema, '');
748 208
        $field = new ValidationField($this->createValidation(), $schema, '', $schemaPath, $options);
749
750 208
        $clean = $this->validateField($data, $field);
751
752 206
        if (Invalid::isInvalid($clean) && $field->isValid()) {
753
            // This really shouldn't happen, but we want to protect against seeing the invalid object.
754
            $field->addError('invalid', ['messageCode' => '{field} is invalid.', 'status' => 422]);
755
        }
756
757 206
        if (!$field->getValidation()->isValid()) {
758 71
            throw new ValidationException($field->getValidation());
759
        }
760
761 150
        return $clean;
762
    }
763
764
    /**
765
     * Validate data against the schema and return the result.
766
     *
767
     * @param mixed $data The data to validate.
768
     * @param array $options Validation options. See `Schema::validate()`.
769
     * @return bool Returns true if the data is valid. False otherwise.
770
     */
771 44
    public function isValid($data, $options = []) {
772
        try {
773 44
            $this->validate($data, $options);
774 31
            return true;
775 23
        } catch (ValidationException $ex) {
776 23
            return false;
777
        }
778
    }
779
780
    /**
781
     * Validate a field.
782
     *
783
     * @param mixed $value The value to validate.
784
     * @param ValidationField $field A validation object to add errors to.
785
     * @return mixed|Invalid Returns a clean version of the value with all extra fields stripped out or invalid if the value
786
     * is completely invalid.
787
     */
788 208
    protected function validateField($value, ValidationField $field) {
789 208
        $result = $value = $this->filterField($value, $field);
0 ignored issues
show
Unused Code introduced by
The assignment to $result is dead and can be removed.
Loading history...
790
791 208
        if ($field->getField() instanceof Schema) {
792
            try {
793 5
                $result = $field->getField()->validate($value, $field->getOptions());
794 2
            } catch (ValidationException $ex) {
795
                // The validation failed, so merge the validations together.
796 5
                $field->getValidation()->merge($ex->getValidation(), $field->getName());
797
            }
798 208
        } elseif (($value === null || ($value === '' && !$field->hasType('string'))) && ($field->val('nullable') || $field->hasType('null'))) {
0 ignored issues
show
introduced by
Consider adding parentheses for clarity. Current Interpretation: ($value === null || $val...$field->hasType('null'), Probably Intended Meaning: $value === null || ($val...field->hasType('null'))
Loading history...
799 13
            $result = null;
800
        } else {
801
            // Validate the field's type.
802 208
            $type = $field->getType();
803 208
            if (is_array($type)) {
804 29
                $result = $this->validateMultipleTypes($value, $type, $field);
805
            } else {
806 187
                $result = $this->validateSingleType($value, $type, $field);
807
            }
808 208
            if (Invalid::isValid($result)) {
809 199
                $result = $this->validateEnum($result, $field);
810
            }
811
        }
812
813
        // Validate a custom field validator.
814 208
        if (Invalid::isValid($result)) {
815 199
            $this->callValidators($result, $field);
816
        }
817
818 208
        return $result;
819
    }
820
821
    /**
822
     * Validate an array.
823
     *
824
     * @param mixed $value The value to validate.
825
     * @param ValidationField $field The validation results to add.
826
     * @return array|Invalid Returns an array or invalid if validation fails.
827
     * @throws RefNotFoundException Throws an exception if the array has an items `$ref` that cannot be found.
828
     */
829 34
    protected function validateArray($value, ValidationField $field) {
830 34
        if ((!is_array($value) || (count($value) > 0 && !array_key_exists(0, $value))) && !$value instanceof \Traversable) {
0 ignored issues
show
introduced by
Consider adding parentheses for clarity. Current Interpretation: (! is_array($value) || c... instanceof Traversable, Probably Intended Meaning: ! is_array($value) || (c...instanceof Traversable)
Loading history...
831 6
            $field->addTypeError('array');
832 6
            return Invalid::value();
833
        } else {
834 29
            if ((null !== $minItems = $field->val('minItems')) && count($value) < $minItems) {
0 ignored issues
show
Bug introduced by
It seems like $value can also be of type Traversable; however, parameter $var of count() does only seem to accept Countable|array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

834
            if ((null !== $minItems = $field->val('minItems')) && count(/** @scrutinizer ignore-type */ $value) < $minItems) {
Loading history...
835 1
                $field->addError(
836 1
                    'minItems',
837
                    [
838 1
                        'messageCode' => '{field} must contain at least {minItems} {minItems,plural,item}.',
839 1
                        'minItems' => $minItems,
840 1
                        'status' => 422
841
                    ]
842
                );
843
            }
844 29
            if ((null !== $maxItems = $field->val('maxItems')) && count($value) > $maxItems) {
845 1
                $field->addError(
846 1
                    'maxItems',
847
                    [
848 1
                        'messageCode' => '{field} must contain no more than {maxItems} {maxItems,plural,item}.',
849 1
                        'maxItems' => $maxItems,
850 1
                        'status' => 422
851
                    ]
852
                );
853
            }
854
855 29
            if ($field->val('uniqueItems') && count($value) > count(array_unique($value))) {
0 ignored issues
show
Bug introduced by
It seems like $value can also be of type Traversable; however, parameter $array of array_unique() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

855
            if ($field->val('uniqueItems') && count($value) > count(array_unique(/** @scrutinizer ignore-type */ $value))) {
Loading history...
856 1
                $field->addError(
857 1
                    'uniqueItems',
858
                    [
859 1
                        'messageCode' => '{field} must contain unique items.',
860
                        'status' => 422,
861
                    ]
862
                );
863
            }
864
865 29
            if ($field->val('items') !== null) {
866 21
                list ($items, $schemaPath) = $this->lookupSchema($field->val('items'), $field->getSchemaPath().'/items');
867
868
                // Validate each of the types.
869 21
                $itemValidation = new ValidationField(
870 21
                    $field->getValidation(),
871 21
                    $items,
872 21
                    '',
873 21
                    $schemaPath,
874 21
                    $field->getOptions()
875
                );
876
877 21
                $result = [];
878 21
                $count = 0;
879 21
                foreach ($value as $i => $item) {
880 21
                    $itemValidation->setName($field->getName()."/$i");
881 21
                    $validItem = $this->validateField($item, $itemValidation);
882 21
                    if (Invalid::isValid($validItem)) {
883 21
                        $result[] = $validItem;
884
                    }
885 21
                    $count++;
886
                }
887
888 21
                return empty($result) && $count > 0 ? Invalid::value() : $result;
889
            } else {
890
                // Cast the items into a proper numeric array.
891 8
                $result = is_array($value) ? array_values($value) : iterator_to_array($value);
892 8
                return $result;
893
            }
894
        }
895
    }
896
897
    /**
898
     * Validate a boolean value.
899
     *
900
     * @param mixed $value The value to validate.
901
     * @param ValidationField $field The validation results to add.
902
     * @return bool|Invalid Returns the cleaned value or invalid if validation fails.
903
     */
904 32
    protected function validateBoolean($value, ValidationField $field) {
905 32
        $value = $value === null ? $value : filter_var($value, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE);
906 32
        if ($value === null) {
907 4
            $field->addTypeError('boolean');
908 4
            return Invalid::value();
909
        }
910
911 29
        return $value;
912
    }
913
914
    /**
915
     * Validate a date time.
916
     *
917
     * @param mixed $value The value to validate.
918
     * @param ValidationField $field The validation results to add.
919
     * @return \DateTimeInterface|Invalid Returns the cleaned value or **null** if it isn't valid.
920
     */
921 14
    protected function validateDatetime($value, ValidationField $field) {
922 14
        if ($value instanceof \DateTimeInterface) {
923
            // do nothing, we're good
924 11
        } elseif (is_string($value) && $value !== '' && !is_numeric($value)) {
925
            try {
926 7
                $dt = new \DateTimeImmutable($value);
927 6
                if ($dt) {
0 ignored issues
show
introduced by
$dt is of type DateTimeImmutable, thus it always evaluated to true.
Loading history...
928 6
                    $value = $dt;
929
                } else {
930 6
                    $value = null;
931
                }
932 1
            } catch (\Throwable $ex) {
933 7
                $value = Invalid::value();
934
            }
935 4
        } elseif (is_int($value) && $value > 0) {
936
            try {
937 1
                $value = new \DateTimeImmutable('@'.(string)round($value));
938
            } catch (\Throwable $ex) {
939 1
                $value = Invalid::value();
940
            }
941
        } else {
942 3
            $value = Invalid::value();
943
        }
944
945 14
        if (Invalid::isInvalid($value)) {
946 4
            $field->addTypeError('datetime');
947
        }
948 14
        return $value;
949
    }
950
951
    /**
952
     * Validate a float.
953
     *
954
     * @param mixed $value The value to validate.
955
     * @param ValidationField $field The validation results to add.
956
     * @return float|Invalid Returns a number or **null** if validation fails.
957
     */
958 17
    protected function validateNumber($value, ValidationField $field) {
959 17
        $result = filter_var($value, FILTER_VALIDATE_FLOAT);
960 17
        if ($result === false) {
961 4
            $field->addTypeError('number');
962 4
            return Invalid::value();
963
        }
964
965 13
        $result = $this->validateNumberProperties($result, $field);
966
967 13
        return $result;
968
    }
969
    /**
970
     * Validate and integer.
971
     *
972
     * @param mixed $value The value to validate.
973
     * @param ValidationField $field The validation results to add.
974
     * @return int|Invalid Returns the cleaned value or **null** if validation fails.
975
     */
976 58
    protected function validateInteger($value, ValidationField $field) {
977 58
        if ($field->val('format') === 'timestamp') {
978 7
            return $this->validateTimestamp($value, $field);
979
        }
980
981 53
        $result = filter_var($value, FILTER_VALIDATE_INT);
982
983 53
        if ($result === false) {
984 10
            $field->addTypeError('integer');
985 10
            return Invalid::value();
986
        }
987
988 47
        $result = $this->validateNumberProperties($result, $field);
989
990 47
        return $result;
991
    }
992
993
    /**
994
     * Validate an object.
995
     *
996
     * @param mixed $value The value to validate.
997
     * @param ValidationField $field The validation results to add.
998
     * @return object|Invalid Returns a clean object or **null** if validation fails.
999
     */
1000 114
    protected function validateObject($value, ValidationField $field) {
1001 114
        if (!$this->isArray($value) || isset($value[0])) {
1002 6
            $field->addTypeError('object');
1003 6
            return Invalid::value();
1004 114
        } elseif (is_array($field->val('properties')) || null !== $field->val('additionalProperties')) {
1005
            // Validate the data against the internal schema.
1006 107
            $value = $this->validateProperties($value, $field);
1007 7
        } elseif (!is_array($value)) {
1008 3
            $value = $this->toObjectArray($value);
1009
        }
1010
1011 112
        if (($maxProperties = $field->val('maxProperties')) && count($value) > $maxProperties) {
1012 1
            $field->addError(
1013 1
                'maxItems',
1014
                [
1015 1
                    'messageCode' => '{field} must contain no more than {maxProperties} {maxProperties,plural,item}.',
1016 1
                    'maxItems' => $maxProperties,
1017 1
                    'status' => 422
1018
                ]
1019
            );
1020
        }
1021
1022 112
        if (($minProperties = $field->val('minProperties')) && count($value) < $minProperties) {
1023 1
            $field->addError(
1024 1
                'minItems',
1025
                [
1026 1
                    'messageCode' => '{field} must contain at least {minProperties} {minProperties,plural,item}.',
1027 1
                    'minItems' => $minProperties,
1028 1
                    'status' => 422
1029
                ]
1030
            );
1031
        }
1032
1033 112
        return $value;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $value returns the type array which is incompatible with the documented return type object|Garden\Schema\Invalid.
Loading history...
1034
    }
1035
1036
    /**
1037
     * Validate data against the schema and return the result.
1038
     *
1039
     * @param array|\Traversable|\ArrayAccess $data The data to validate.
1040
     * @param ValidationField $field This argument will be filled with the validation result.
1041
     * @return array|Invalid Returns a clean array with only the appropriate properties and the data coerced to proper types.
1042
     * or invalid if there are no valid properties.
1043
     * @throws RefNotFoundException Throws an exception of a property or additional property has a `$ref` that cannot be found.
1044
     */
1045 107
    protected function validateProperties($data, ValidationField $field) {
1046 107
        $properties = $field->val('properties', []);
1047 107
        $additionalProperties = $field->val('additionalProperties');
1048 107
        $required = array_flip($field->val('required', []));
1049 107
        $isRequest = $field->isRequest();
1050 107
        $isResponse = $field->isResponse();
1051
1052 107
        if (is_array($data)) {
1053 103
            $keys = array_keys($data);
1054 103
            $clean = [];
1055
        } else {
1056 4
            $keys = array_keys(iterator_to_array($data));
0 ignored issues
show
Bug introduced by
It seems like $data can also be of type ArrayAccess; however, parameter $iterator of iterator_to_array() does only seem to accept Traversable, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

1056
            $keys = array_keys(iterator_to_array(/** @scrutinizer ignore-type */ $data));
Loading history...
1057 4
            $class = get_class($data);
1058 4
            $clean = new $class;
1059
1060 4
            if ($clean instanceof \ArrayObject && $data instanceof \ArrayObject) {
1061 3
                $clean->setFlags($data->getFlags());
1062 3
                $clean->setIteratorClass($data->getIteratorClass());
1063
            }
1064
        }
1065 107
        $keys = array_combine(array_map('strtolower', $keys), $keys);
1066
1067 107
        $propertyField = new ValidationField($field->getValidation(), [], '', '', $field->getOptions());
1068
1069
        // Loop through the schema fields and validate each one.
1070 107
        foreach ($properties as $propertyName => $property) {
1071 105
            list($property, $schemaPath) = $this->lookupSchema($property, $field->getSchemaPath().'/properties/'.$propertyField->escapeRef($propertyName));
1072
1073
            $propertyField
1074 105
                ->setField($property)
1075 105
                ->setName(ltrim($field->getName().'/'.$propertyField->escapeRef($propertyName), '/'))
1076 105
                ->setSchemaPath($schemaPath)
1077
            ;
1078
1079 105
            $lName = strtolower($propertyName);
1080 105
            $isRequired = isset($required[$propertyName]);
1081
1082
            // Check to strip this field if it is readOnly or writeOnly.
1083 105
            if (($isRequest && $propertyField->val('readOnly')) || ($isResponse && $propertyField->val('writeOnly'))) {
1084 6
                unset($keys[$lName]);
1085 6
                continue;
1086
            }
1087
1088
            // Check for required fields.
1089 105
            if (!array_key_exists($lName, $keys)) {
1090 30
                if ($field->isSparse()) {
1091
                    // Sparse validation can leave required fields out.
1092 29
                } elseif ($propertyField->hasVal('default')) {
1093 3
                    $clean[$propertyName] = $propertyField->val('default');
1094 26
                } elseif ($isRequired) {
1095 30
                    $propertyField->addError('missingField', ['messageCode' => '{field} is required.']);
1096
                }
1097
            } else {
1098 93
                $value = $data[$keys[$lName]];
1099
1100 93
                if (in_array($value, [null, ''], true) && !$isRequired && !($propertyField->val('nullable') || $propertyField->hasType('null'))) {
1101 5
                    if ($propertyField->getType() !== 'string' || $value === null) {
1102 2
                        continue;
1103
                    }
1104
                }
1105
1106 91
                $clean[$propertyName] = $this->validateField($value, $propertyField);
1107
            }
1108
1109 103
            unset($keys[$lName]);
1110
        }
1111
1112
        // Look for extraneous properties.
1113 107
        if (!empty($keys)) {
1114 17
            if ($additionalProperties) {
1115 6
                list($additionalProperties, $schemaPath) = $this->lookupSchema(
1116 6
                    $additionalProperties,
1117 6
                    $field->getSchemaPath().'/additionalProperties'
1118
                );
1119
1120 6
                $propertyField = new ValidationField(
1121 6
                    $field->getValidation(),
1122 6
                    $additionalProperties,
1123 6
                    '',
1124 6
                    $schemaPath,
1125 6
                    $field->getOptions()
1126
                );
1127
1128 6
                foreach ($keys as $key) {
1129
                    $propertyField
1130 6
                        ->setName(ltrim($field->getName()."/$key", '/'));
1131
1132 6
                    $valid = $this->validateField($data[$key], $propertyField);
1133 6
                    if (Invalid::isValid($valid)) {
1134 6
                        $clean[$key] = $valid;
1135
                    }
1136
                }
1137 11
            } elseif ($this->hasFlag(Schema::VALIDATE_EXTRA_PROPERTY_NOTICE)) {
1138 2
                $msg = sprintf("%s has unexpected field(s): %s.", $field->getName() ?: 'value', implode(', ', $keys));
1139 2
                trigger_error($msg, E_USER_NOTICE);
1140 9
            } elseif ($this->hasFlag(Schema::VALIDATE_EXTRA_PROPERTY_EXCEPTION)) {
1141 2
                $field->addError('invalid', [
1142 2
                    'messageCode' => '{field} has {extra,plural,an unexpected field,unexpected fields}: {extra}.',
1143 2
                    'extra' => array_values($keys),
1144 2
                    'status' => 422
1145
                ]);
1146
            }
1147
        }
1148
1149 105
        return $clean;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $clean also could return the type ArrayObject which is incompatible with the documented return type Garden\Schema\Invalid|array.
Loading history...
1150
    }
1151
1152
    /**
1153
     * Validate a string.
1154
     *
1155
     * @param mixed $value The value to validate.
1156
     * @param ValidationField $field The validation results to add.
1157
     * @return string|Invalid Returns the valid string or **null** if validation fails.
1158
     */
1159 83
    protected function validateString($value, ValidationField $field) {
1160 83
        if ($field->val('format') === 'date-time') {
1161 12
            $result = $this->validateDatetime($value, $field);
1162 12
            return $result;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $result also could return the type DateTimeInterface which is incompatible with the documented return type Garden\Schema\Invalid|string.
Loading history...
1163
        }
1164
1165 72
        if (is_string($value) || is_numeric($value)) {
1166 70
            $value = $result = (string)$value;
1167
        } else {
1168 5
            $field->addTypeError('string');
1169 5
            return Invalid::value();
1170
        }
1171
1172 70
        if (($minLength = $field->val('minLength', 0)) > 0 && mb_strlen($value) < $minLength) {
1173 4
            if (!empty($field->getName()) && $minLength === 1) {
1174 2
                $field->addError('missingField', ['messageCode' => '{field} is required.', 'status' => 422]);
1175
            } else {
1176 2
                $field->addError(
1177 2
                    'minLength',
1178
                    [
1179 2
                        'messageCode' => '{field} should be at least {minLength} {minLength,plural,character} long.',
1180 2
                        'minLength' => $minLength,
1181 2
                        'status' => 422
1182
                    ]
1183
                );
1184
            }
1185
        }
1186 70
        if (($maxLength = $field->val('maxLength', 0)) > 0 && mb_strlen($value) > $maxLength) {
1187 1
            $field->addError(
1188 1
                'maxLength',
1189
                [
1190 1
                    'messageCode' => '{field} is {overflow} {overflow,plural,characters} too long.',
1191 1
                    'maxLength' => $maxLength,
1192 1
                    'overflow' => mb_strlen($value) - $maxLength,
1193 1
                    'status' => 422
1194
                ]
1195
            );
1196
        }
1197 70
        if ($pattern = $field->val('pattern')) {
1198 4
            $regex = '`'.str_replace('`', preg_quote('`', '`'), $pattern).'`';
1199
1200 4
            if (!preg_match($regex, $value)) {
1201 2
                $field->addError(
1202 2
                    'invalid',
1203
                    [
1204 2
                        'messageCode' => '{field} is in the incorrect format.',
1205
                        'status' => 422
1206
                    ]
1207
                );
1208
            }
1209
        }
1210 70
        if ($format = $field->val('format')) {
1211 11
            $type = $format;
1212
            switch ($format) {
1213 11
                case 'date':
1214
                    $result = $this->validateDatetime($result, $field);
1215
                    if ($result instanceof \DateTimeInterface) {
1216
                        $result = $result->format("Y-m-d\T00:00:00P");
1217
                    }
1218
                    break;
1219 11
                case 'email':
1220 1
                    $result = filter_var($result, FILTER_VALIDATE_EMAIL);
1221 1
                    break;
1222 10
                case 'ipv4':
1223 1
                    $type = 'IPv4 address';
1224 1
                    $result = filter_var($result, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4);
1225 1
                    break;
1226 9
                case 'ipv6':
1227 1
                    $type = 'IPv6 address';
1228 1
                    $result = filter_var($result, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6);
1229 1
                    break;
1230 8
                case 'ip':
1231 1
                    $type = 'IP address';
1232 1
                    $result = filter_var($result, FILTER_VALIDATE_IP);
1233 1
                    break;
1234 7
                case 'uri':
1235 7
                    $type = 'URI';
1236 7
                    $result = filter_var($result, FILTER_VALIDATE_URL);
1237 7
                    break;
1238
                default:
1239
                    trigger_error("Unrecognized format '$format'.", E_USER_NOTICE);
1240
            }
1241 11
            if ($result === false) {
1242 5
                $field->addTypeError($type);
1243
            }
1244
        }
1245
1246 70
        if ($field->isValid()) {
1247 62
            return $result;
1248
        } else {
1249 12
            return Invalid::value();
1250
        }
1251
    }
1252
1253
    /**
1254
     * Validate a unix timestamp.
1255
     *
1256
     * @param mixed $value The value to validate.
1257
     * @param ValidationField $field The field being validated.
1258
     * @return int|Invalid Returns a valid timestamp or invalid if the value doesn't validate.
1259
     */
1260 8
    protected function validateTimestamp($value, ValidationField $field) {
1261 8
        if (is_numeric($value) && $value > 0) {
1262 3
            $result = (int)$value;
1263 5
        } elseif (is_string($value) && $ts = strtotime($value)) {
1264 1
            $result = $ts;
1265
        } else {
1266 4
            $field->addTypeError('timestamp');
1267 4
            $result = Invalid::value();
1268
        }
1269 8
        return $result;
1270
    }
1271
1272
    /**
1273
     * Validate a null value.
1274
     *
1275
     * @param mixed $value The value to validate.
1276
     * @param ValidationField $field The error collector for the field.
1277
     * @return null|Invalid Returns **null** or invalid.
1278
     */
1279
    protected function validateNull($value, ValidationField $field) {
1280
        if ($value === null) {
1281
            return null;
1282
        }
1283
        $field->addError('invalid', ['messageCode' => '{field} should be null.', 'status' => 422]);
1284
        return Invalid::value();
1285
    }
1286
1287
    /**
1288
     * Validate a value against an enum.
1289
     *
1290
     * @param mixed $value The value to test.
1291
     * @param ValidationField $field The validation object for adding errors.
1292
     * @return mixed|Invalid Returns the value if it is one of the enumerated values or invalid otherwise.
1293
     */
1294 199
    protected function validateEnum($value, ValidationField $field) {
1295 199
        $enum = $field->val('enum');
1296 199
        if (empty($enum)) {
1297 198
            return $value;
1298
        }
1299
1300 1
        if (!in_array($value, $enum, true)) {
1301 1
            $field->addError(
1302 1
                'invalid',
1303
                [
1304 1
                    'messageCode' => '{field} must be one of: {enum}.',
1305 1
                    'enum' => $enum,
1306 1
                    'status' => 422
1307
                ]
1308
            );
1309 1
            return Invalid::value();
1310
        }
1311 1
        return $value;
1312
    }
1313
1314
    /**
1315
     * Call all of the filters attached to a field.
1316
     *
1317
     * @param mixed $value The field value being filtered.
1318
     * @param ValidationField $field The validation object.
1319
     * @return mixed Returns the filtered value. If there are no filters for the field then the original value is returned.
1320
     */
1321 208
    protected function callFilters($value, ValidationField $field) {
1322
        // Strip array references in the name except for the last one.
1323 208
        $key = $field->getSchemaPath();
1324 208
        if (!empty($this->filters[$key])) {
1325 2
            foreach ($this->filters[$key] as $filter) {
1326 2
                $value = call_user_func($filter, $value, $field);
1327
            }
1328
        }
1329 208
        return $value;
1330
    }
1331
1332
    /**
1333
     * Call all of the validators attached to a field.
1334
     *
1335
     * @param mixed $value The field value being validated.
1336
     * @param ValidationField $field The validation object to add errors.
1337
     */
1338 199
    protected function callValidators($value, ValidationField $field) {
1339 199
        $valid = true;
1340
1341
        // Strip array references in the name except for the last one.
1342 199
        $key = $field->getSchemaPath();
1343 199
        if (!empty($this->validators[$key])) {
1344 5
            foreach ($this->validators[$key] as $validator) {
1345 5
                $r = call_user_func($validator, $value, $field);
1346
1347 5
                if ($r === false || Invalid::isInvalid($r)) {
1348 5
                    $valid = false;
1349
                }
1350
            }
1351
        }
1352
1353
        // Add an error on the field if the validator hasn't done so.
1354 199
        if (!$valid && $field->isValid()) {
1355 1
            $field->addError('invalid', ['messageCode' => '{field} is invalid.', 'status' => 422]);
1356
        }
1357 199
    }
1358
1359
    /**
1360
     * Specify data which should be serialized to JSON.
1361
     *
1362
     * This method specifically returns data compatible with the JSON schema format.
1363
     *
1364
     * @return mixed Returns data which can be serialized by **json_encode()**, which is a value of any type other than a resource.
1365
     * @link http://php.net/manual/en/jsonserializable.jsonserialize.php
1366
     * @link http://json-schema.org/
1367
     */
1368 16
    public function jsonSerialize() {
1369
        $fix = function ($schema) use (&$fix) {
1370 16
            if ($schema instanceof Schema) {
1371 1
                return $schema->jsonSerialize();
1372
            }
1373
1374 16
            if (!empty($schema['type'])) {
1375 15
                $types = (array)$schema['type'];
1376
1377 15
                foreach ($types as $i => &$type) {
1378
                    // Swap datetime and timestamp to other types with formats.
1379 15
                    if ($type === 'datetime') {
1380 4
                        $type = 'string';
1381 4
                        $schema['format'] = 'date-time';
1382 14
                    } elseif ($schema['type'] === 'timestamp') {
1383 2
                        $type = 'integer';
1384 15
                        $schema['format'] = 'timestamp';
1385
                    }
1386
                }
1387 15
                $types = array_unique($types);
1388 15
                $schema['type'] = count($types) === 1 ? reset($types) : $types;
1389
            }
1390
1391 16
            if (!empty($schema['items'])) {
1392 4
                $schema['items'] = $fix($schema['items']);
1393
            }
1394 16
            if (!empty($schema['properties'])) {
1395 11
                $properties = [];
1396 11
                foreach ($schema['properties'] as $key => $property) {
1397 11
                    $properties[$key] = $fix($property);
1398
                }
1399 11
                $schema['properties'] = $properties;
1400
            }
1401
1402 16
            return $schema;
1403 16
        };
1404
1405 16
        $result = $fix($this->schema);
1406
1407 16
        return $result;
1408
    }
1409
1410
    /**
1411
     * Look up a type based on its alias.
1412
     *
1413
     * @param string $alias The type alias or type name to lookup.
1414
     * @return mixed
1415
     */
1416 167
    protected function getType($alias) {
1417 167
        if (isset(self::$types[$alias])) {
1418
            return $alias;
1419
        }
1420 167
        foreach (self::$types as $type => $aliases) {
1421 167
            if (in_array($alias, $aliases, true)) {
1422 167
                return $type;
1423
            }
1424
        }
1425 11
        return null;
1426
    }
1427
1428
    /**
1429
     * Get the class that's used to contain validation information.
1430
     *
1431
     * @return Validation|string Returns the validation class.
1432
     * @deprecated
1433
     */
1434 1
    public function getValidationClass() {
1435 1
        trigger_error('Schema::getValidationClass() is deprecated. Use Schema::getValidationFactory() instead.', E_USER_DEPRECATED);
1436 1
        return $this->validationClass;
0 ignored issues
show
Deprecated Code introduced by
The property Garden\Schema\Schema::$validationClass has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

1436
        return /** @scrutinizer ignore-deprecated */ $this->validationClass;
Loading history...
1437
    }
1438
1439
    /**
1440
     * Set the class that's used to contain validation information.
1441
     *
1442
     * @param Validation|string $class Either the name of a class or a class that will be cloned.
1443
     * @return $this
1444
     * @deprecated
1445
     */
1446 1
    public function setValidationClass($class) {
1447 1
        trigger_error('Schema::setValidationClass() is deprecated. Use Schema::setValidationFactory() instead.', E_USER_DEPRECATED);
1448
1449 1
        if (!is_a($class, Validation::class, true)) {
1450
            throw new \InvalidArgumentException("$class must be a subclass of ".Validation::class, 500);
1451
        }
1452
1453
        $this->setValidationFactory(function () use ($class) {
1454 1
            if ($class instanceof Validation) {
1455 1
                $result = clone $class;
1456
            } else {
1457 1
                $result = new $class;
1458
            }
1459 1
            return $result;
1460 1
        });
1461 1
        $this->validationClass = $class;
0 ignored issues
show
Deprecated Code introduced by
The property Garden\Schema\Schema::$validationClass has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

1461
        /** @scrutinizer ignore-deprecated */ $this->validationClass = $class;
Loading history...
1462 1
        return $this;
1463
    }
1464
1465
    /**
1466
     * Create a new validation instance.
1467
     *
1468
     * @return Validation Returns a validation object.
1469
     */
1470 208
    protected function createValidation(): Validation {
1471 208
        return call_user_func($this->getValidationFactory());
1472
    }
1473
1474
    /**
1475
     * Check whether or not a value is an array or accessible like an array.
1476
     *
1477
     * @param mixed $value The value to check.
1478
     * @return bool Returns **true** if the value can be used like an array or **false** otherwise.
1479
     */
1480 114
    private function isArray($value) {
1481 114
        return is_array($value) || ($value instanceof \ArrayAccess && $value instanceof \Traversable);
1482
    }
1483
1484
    /**
1485
     * Cast a value to an array.
1486
     *
1487
     * @param \Traversable $value The value to convert.
1488
     * @return array Returns an array.
1489
     */
1490 3
    private function toObjectArray(\Traversable $value) {
1491 3
        $class = get_class($value);
1492 3
        if ($value instanceof \ArrayObject) {
1493 2
            return new $class($value->getArrayCopy(), $value->getFlags(), $value->getIteratorClass());
0 ignored issues
show
Bug Best Practice introduced by
The expression return new $class($value...ue->getIteratorClass()) returns the type object which is incompatible with the documented return type array.
Loading history...
1494 1
        } elseif ($value instanceof \ArrayAccess) {
1495 1
            $r = new $class;
1496 1
            foreach ($value as $k => $v) {
1497 1
                $r[$k] = $v;
1498
            }
1499 1
            return $r;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $r returns the type object which is incompatible with the documented return type array.
Loading history...
1500
        }
1501
        return iterator_to_array($value);
1502
    }
1503
1504
    /**
1505
     * Return a sparse version of this schema.
1506
     *
1507
     * A sparse schema has no required properties.
1508
     *
1509
     * @return Schema Returns a new sparse schema.
1510
     */
1511 2
    public function withSparse() {
1512 2
        $sparseSchema = $this->withSparseInternal($this, new \SplObjectStorage());
1513 2
        return $sparseSchema;
1514
    }
1515
1516
    /**
1517
     * The internal implementation of `Schema::withSparse()`.
1518
     *
1519
     * @param array|Schema $schema The schema to make sparse.
1520
     * @param \SplObjectStorage $schemas Collected sparse schemas that have already been made.
1521
     * @return mixed
1522
     */
1523 2
    private function withSparseInternal($schema, \SplObjectStorage $schemas) {
1524 2
        if ($schema instanceof Schema) {
1525 2
            if ($schemas->contains($schema)) {
1526 1
                return $schemas[$schema];
1527
            } else {
1528 2
                $schemas[$schema] = $sparseSchema = new Schema();
1529 2
                $sparseSchema->schema = $schema->withSparseInternal($schema->schema, $schemas);
1530 2
                if ($id = $sparseSchema->getID()) {
1531
                    $sparseSchema->setID($id.'Sparse');
1532
                }
1533
1534 2
                return $sparseSchema;
1535
            }
1536
        }
1537
1538 2
        unset($schema['required']);
1539
1540 2
        if (isset($schema['items'])) {
1541 1
            $schema['items'] = $this->withSparseInternal($schema['items'], $schemas);
1542
        }
1543 2
        if (isset($schema['properties'])) {
1544 2
            foreach ($schema['properties'] as $name => &$property) {
1545 2
                $property = $this->withSparseInternal($property, $schemas);
1546
            }
1547
        }
1548
1549 2
        return $schema;
1550
    }
1551
1552
    /**
1553
     * Filter a field's value using built in and custom filters.
1554
     *
1555
     * @param mixed $value The original value of the field.
1556
     * @param ValidationField $field The field information for the field.
1557
     * @return mixed Returns the filtered field or the original field value if there are no filters.
1558
     */
1559 208
    private function filterField($value, ValidationField $field) {
1560
        // Check for limited support for Open API style.
1561 208
        if (!empty($field->val('style')) && is_string($value)) {
1562 8
            $doFilter = true;
1563 8
            if ($field->hasType('boolean') && in_array($value, ['true', 'false', '0', '1'], true)) {
1564 4
                $doFilter = false;
1565 4
            } elseif ($field->hasType('integer') || $field->hasType('number') && is_numeric($value)) {
1566
                $doFilter = false;
1567
            }
1568
1569 8
            if ($doFilter) {
1570 4
                switch ($field->val('style')) {
1571 4
                    case 'form':
1572 2
                        $value = explode(',', $value);
1573 2
                        break;
1574 2
                    case 'spaceDelimited':
1575 1
                        $value = explode(' ', $value);
1576 1
                        break;
1577 1
                    case 'pipeDelimited':
1578 1
                        $value = explode('|', $value);
1579 1
                        break;
1580
                }
1581
            }
1582
        }
1583
1584 208
        $value = $this->callFilters($value, $field);
1585
1586 208
        return $value;
1587
    }
1588
1589
    /**
1590
     * Whether a offset exists.
1591
     *
1592
     * @param mixed $offset An offset to check for.
1593
     * @return boolean true on success or false on failure.
1594
     * @link http://php.net/manual/en/arrayaccess.offsetexists.php
1595
     */
1596 7
    public function offsetExists($offset) {
1597 7
        return isset($this->schema[$offset]);
1598
    }
1599
1600
    /**
1601
     * Offset to retrieve.
1602
     *
1603
     * @param mixed $offset The offset to retrieve.
1604
     * @return mixed Can return all value types.
1605
     * @link http://php.net/manual/en/arrayaccess.offsetget.php
1606
     */
1607 7
    public function offsetGet($offset) {
1608 7
        return isset($this->schema[$offset]) ? $this->schema[$offset] : null;
1609
    }
1610
1611
    /**
1612
     * Offset to set.
1613
     *
1614
     * @param mixed $offset The offset to assign the value to.
1615
     * @param mixed $value The value to set.
1616
     * @link http://php.net/manual/en/arrayaccess.offsetset.php
1617
     */
1618 1
    public function offsetSet($offset, $value) {
1619 1
        $this->schema[$offset] = $value;
1620 1
    }
1621
1622
    /**
1623
     * Offset to unset.
1624
     *
1625
     * @param mixed $offset The offset to unset.
1626
     * @link http://php.net/manual/en/arrayaccess.offsetunset.php
1627
     */
1628 1
    public function offsetUnset($offset) {
1629 1
        unset($this->schema[$offset]);
1630 1
    }
1631
1632
    /**
1633
     * Validate a field against a single type.
1634
     *
1635
     * @param mixed $value The value to validate.
1636
     * @param string $type The type to validate against.
1637
     * @param ValidationField $field Contains field and validation information.
1638
     * @return mixed Returns the valid value or `Invalid`.
1639
     */
1640 208
    protected function validateSingleType($value, $type, ValidationField $field) {
1641
        switch ($type) {
1642 208
            case 'boolean':
1643 32
                $result = $this->validateBoolean($value, $field);
1644 32
                break;
1645 188
            case 'integer':
1646 58
                $result = $this->validateInteger($value, $field);
1647 58
                break;
1648 169
            case 'number':
1649 17
                $result = $this->validateNumber($value, $field);
1650 17
                break;
1651 160
            case 'string':
1652 83
                $result = $this->validateString($value, $field);
1653 83
                break;
1654 136
            case 'timestamp':
1655 1
                trigger_error('The timestamp type is deprecated. Use an integer with a format of timestamp instead.', E_USER_DEPRECATED);
1656 1
                $result = $this->validateTimestamp($value, $field);
1657 1
                break;
1658 136
            case 'datetime':
1659 2
                trigger_error('The datetime type is deprecated. Use a string with a format of date-time instead.', E_USER_DEPRECATED);
1660 2
                $result = $this->validateDatetime($value, $field);
1661 2
                break;
1662 135
            case 'array':
1663 34
                $result = $this->validateArray($value, $field);
1664 34
                break;
1665 115
            case 'object':
1666 114
                $result = $this->validateObject($value, $field);
1667 112
                break;
1668 5
            case 'null':
1669
                $result = $this->validateNull($value, $field);
1670
                break;
1671 5
            case null:
1672
                // No type was specified so we are valid.
1673 5
                $result = $value;
1674 5
                break;
1675
            default:
1676
                throw new \InvalidArgumentException("Unrecognized type $type.", 500);
1677
        }
1678 208
        return $result;
1679
    }
1680
1681
    /**
1682
     * Validate a field against multiple basic types.
1683
     *
1684
     * The first validation that passes will be returned. If no type can be validated against then validation will fail.
1685
     *
1686
     * @param mixed $value The value to validate.
1687
     * @param string[] $types The types to validate against.
1688
     * @param ValidationField $field Contains field and validation information.
1689
     * @return mixed Returns the valid value or `Invalid`.
1690
     */
1691 29
    private function validateMultipleTypes($value, array $types, ValidationField $field) {
1692 29
        trigger_error('Multiple schema types are deprecated.', E_USER_DEPRECATED);
1693
1694
        // First check for an exact type match.
1695 29
        switch (gettype($value)) {
1696 29
            case 'boolean':
1697 4
                if (in_array('boolean', $types)) {
1698 4
                    $singleType = 'boolean';
1699
                }
1700 4
                break;
1701 26
            case 'integer':
1702 7
                if (in_array('integer', $types)) {
1703 5
                    $singleType = 'integer';
1704 2
                } elseif (in_array('number', $types)) {
1705 1
                    $singleType = 'number';
1706
                }
1707 7
                break;
1708 21
            case 'double':
1709 4
                if (in_array('number', $types)) {
1710 4
                    $singleType = 'number';
1711
                } elseif (in_array('integer', $types)) {
1712
                    $singleType = 'integer';
1713
                }
1714 4
                break;
1715 18
            case 'string':
1716 9
                if (in_array('datetime', $types) && preg_match(self::$DATE_REGEX, $value)) {
1717 1
                    $singleType = 'datetime';
1718 8
                } elseif (in_array('string', $types)) {
1719 4
                    $singleType = 'string';
1720
                }
1721 9
                break;
1722 10
            case 'array':
1723 10
                if (in_array('array', $types) && in_array('object', $types)) {
1724 1
                    $singleType = isset($value[0]) || empty($value) ? 'array' : 'object';
1725 9
                } elseif (in_array('object', $types)) {
1726
                    $singleType = 'object';
1727 9
                } elseif (in_array('array', $types)) {
1728 9
                    $singleType = 'array';
1729
                }
1730 10
                break;
1731 1
            case 'NULL':
1732
                if (in_array('null', $types)) {
1733
                    $singleType = $this->validateSingleType($value, 'null', $field);
1734
                }
1735
                break;
1736
        }
1737 29
        if (!empty($singleType)) {
1738 25
            return $this->validateSingleType($value, $singleType, $field);
1739
        }
1740
1741
        // Clone the validation field to collect errors.
1742 6
        $typeValidation = new ValidationField(new Validation(), $field->getField(), '', '', $field->getOptions());
1743
1744
        // Try and validate against each type.
1745 6
        foreach ($types as $type) {
1746 6
            $result = $this->validateSingleType($value, $type, $typeValidation);
1747 6
            if (Invalid::isValid($result)) {
1748 6
                return $result;
1749
            }
1750
        }
1751
1752
        // Since we got here the value is invalid.
1753
        $field->merge($typeValidation->getValidation());
1754
        return Invalid::value();
1755
    }
1756
1757
    /**
1758
     * Validate specific numeric validation properties.
1759
     *
1760
     * @param int|float $value The value to test.
1761
     * @param ValidationField $field Field information.
1762
     * @return int|float|Invalid Returns the number of invalid.
1763
     */
1764 57
    private function validateNumberProperties($value, ValidationField $field) {
1765 57
        $count = $field->getErrorCount();
1766
1767 57
        if ($multipleOf = $field->val('multipleOf')) {
1768 4
            $divided = $value / $multipleOf;
1769
1770 4
            if ($divided != round($divided)) {
1771 2
                $field->addError('multipleOf', ['messageCode' => '{field} is not a multiple of {multipleOf}.', 'status' => 422, 'multipleOf' => $multipleOf]);
1772
            }
1773
        }
1774
1775 57
        if ($maximum = $field->val('maximum')) {
1776 4
            $exclusive = $field->val('exclusiveMaximum');
1777
1778 4
            if ($value > $maximum || ($exclusive && $value == $maximum)) {
1779 2
                if ($exclusive) {
1780 1
                    $field->addError('maximum', ['messageCode' => '{field} is greater than or equal to {maximum}.', 'status' => 422, 'maximum' => $maximum]);
1781
                } else {
1782 1
                    $field->addError('maximum', ['messageCode' => '{field} is greater than {maximum}.', 'status' => 422, 'maximum' => $maximum]);
1783
                }
1784
1785
            }
1786
        }
1787
1788 57
        if ($minimum = $field->val('minimum')) {
1789 4
            $exclusive = $field->val('exclusiveMinimum');
1790
1791 4
            if ($value < $minimum || ($exclusive && $value == $minimum)) {
1792 2
                if ($exclusive) {
1793 1
                    $field->addError('minimum', ['messageCode' => '{field} is greater than or equal to {minimum}.', 'status' => 422, 'minimum' => $minimum]);
1794
                } else {
1795 1
                    $field->addError('minimum', ['messageCode' => '{field} is greater than {minimum}.', 'status' => 422, 'minimum' => $minimum]);
1796
                }
1797
1798
            }
1799
        }
1800
1801 57
        return $field->getErrorCount() === $count ? $value : Invalid::value();
1802
    }
1803
1804
    /**
1805
     * Parse a nested field name selector.
1806
     *
1807
     * Field selectors should be separated by "/" characters, but may currently be separated by "." characters which
1808
     * triggers a deprecated error.
1809
     *
1810
     * @param string $field The field selector.
1811
     * @return string Returns the field selector in the correct format.
1812
     */
1813 15
    private function parseFieldSelector(string $field): string {
1814 15
        if (strlen($field) === 0) {
1815 4
            return $field;
1816
        }
1817
1818 11
        if (strpos($field, '.') !== false) {
1819 1
            if (strpos($field, '/') === false) {
1820 1
                trigger_error('Field selectors must be separated by "/" instead of "."', E_USER_DEPRECATED);
1821
1822 1
                $parts = explode('.', $field);
1823 1
                $parts = @array_map([$this, 'parseFieldSelector'], $parts); // silence because error triggered already.
1824
1825 1
                $field = implode('/', $parts);
1826
            }
1827 11
        } elseif ($field === '[]') {
1828 1
            trigger_error('Field selectors with item selector "[]" must be converted to "items".', E_USER_DEPRECATED);
1829 1
            $field = 'items';
1830 10
        } elseif (strpos($field, '/') === false && !in_array($field, ['items', 'additionalProperties'], true)) {
1831 3
            trigger_error("Field selectors must specify full schema paths. ($field)", E_USER_DEPRECATED);
1832 3
            $field = "/properties/$field";
1833
        }
1834
1835 11
        if (strpos($field, '[]') !== false) {
1836 1
            trigger_error('Field selectors with item selector "[]" must be converted to "/items".', E_USER_DEPRECATED);
1837 1
            $field = str_replace('[]', '/items', $field);
1838
        }
1839
1840 11
        return ltrim($field, '/');
1841
    }
1842
1843
    /**
1844
     * Lookup a schema based on a schema node.
1845
     *
1846
     * The node could be a schema array, `Schema` object, or a schema reference.
1847
     *
1848
     * @param mixed $schema The schema node to lookup with.
1849
     * @param string $schemaPath The current path of the schema.
1850
     * @return array Returns an array with two elements:
1851
     * - Schema|array|\ArrayAccess The schema that was found.
1852
     * - string The path of the schema. This is either the reference or the `$path` parameter for inline schemas.
1853
     * @throws RefNotFoundException Throws an exception when a reference could not be found.
1854
     */
1855 211
    private function lookupSchema($schema, string $schemaPath) {
1856 211
        if ($schema instanceof Schema) {
1857 6
            return [$schema, $schemaPath];
1858
        } else {
1859 211
            $lookup = $this->getRefLookup();
1860 211
            $visited = [];
1861
1862 211
            while (!empty($schema['$ref'])) {
1863 10
                $schemaPath = $schema['$ref'];
1864
1865 10
                if (isset($visited[$schemaPath])) {
1866 1
                    throw new RefNotFoundException("Cyclical reference cannot be resolved. ($schemaPath)", 508);
1867
                }
1868 10
                $visited[$schemaPath] = true;
1869
1870
                try {
1871 10
                    $schema = call_user_func($lookup, $schemaPath);
1872 1
                } catch (\Exception $ex) {
1873 1
                    throw new RefNotFoundException($ex->getMessage(), $ex->getCode(), $ex);
1874
                }
1875 9
                if ($schema === null) {
1876 1
                    throw new RefNotFoundException("Schema reference could not be found. ($schemaPath)");
1877
                }
1878
            }
1879 208
            return [$schema, $schemaPath];
1880
        }
1881
    }
1882
1883
    /**
1884
     * Get the function used to resolve `$ref` lookups.
1885
     *
1886
     * @return callable Returns the current `$ref` lookup.
1887
     */
1888 211
    public function getRefLookup(): callable {
1889 211
        return $this->refLookup;
1890
    }
1891
1892
    /**
1893
     * Set the function used to resolve `$ref` lookups.
1894
     *
1895
     * @param callable $refLookup The new lookup function.
1896
     * @return $this
1897
     */
1898 10
    public function setRefLookup(callable $refLookup) {
1899 10
        $this->refLookup = $refLookup;
1900 10
        return $this;
1901
    }
1902
1903
    /**
1904
     * Get factory used to create validation objects.
1905
     *
1906
     * @return callable Returns the current factory.
1907
     */
1908 208
    public function getValidationFactory(): callable {
1909 208
        return $this->validationFactory;
1910
    }
1911
1912
    /**
1913
     * Set the factory used to create validation objects.
1914
     *
1915
     * @param callable $validationFactory The new factory.
1916
     * @return $this
1917
     */
1918 2
    public function setValidationFactory(callable $validationFactory) {
1919 2
        $this->validationFactory = $validationFactory;
1920 2
        $this->validationClass = null;
0 ignored issues
show
Deprecated Code introduced by
The property Garden\Schema\Schema::$validationClass has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

1920
        /** @scrutinizer ignore-deprecated */ $this->validationClass = null;
Loading history...
1921 2
        return $this;
1922
    }
1923
}
1924