Completed
Pull Request — master (#54)
by Todd
03:11 queued 01:04
created

Schema::merge()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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

377
        $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...
378 176
        $schema->schema = $schema->parseInternal($arr);
379 175
        return $schema;
380
    }
381
382
    /**
383
     * Parse a schema in short form into a full schema array.
384
     *
385
     * @param array $arr The array to parse into a schema.
386
     * @return array The full schema array.
387
     * @throws \InvalidArgumentException Throws an exception when an item in the schema is invalid.
388
     */
389 176
    protected function parseInternal(array $arr): array {
390 176
        if (empty($arr)) {
391
            // An empty schema validates to anything.
392 6
            return [];
393 171
        } elseif (isset($arr['type'])) {
394
            // This is a long form schema and can be parsed as the root.
395 1
            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...
396
        } else {
397
            // Check for a root schema.
398 170
            $value = reset($arr);
399 170
            $key = key($arr);
400 170
            if (is_int($key)) {
401 106
                $key = $value;
402 106
                $value = null;
403
            }
404 170
            list ($name, $param) = $this->parseShortParam($key, $value);
405 169
            if (empty($name)) {
406 62
                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...
407
            }
408
        }
409
410
        // If we are here then this is n object schema.
411 110
        list($properties, $required) = $this->parseProperties($arr);
412
413
        $result = [
414 110
            'type' => 'object',
415 110
            'properties' => $properties,
416 110
            'required' => $required
417
        ];
418
419 110
        return array_filter($result);
420
    }
421
422
    /**
423
     * Parse a schema node.
424
     *
425
     * @param array $node The node to parse.
426
     * @param mixed $value Additional information from the node.
427
     * @return array|\ArrayAccess Returns a JSON schema compatible node.
428
     */
429 170
    private function parseNode($node, $value = null) {
430 170
        if (is_array($value)) {
431 66
            if (is_array($node['type'])) {
432
                trigger_error('Schemas with multiple types is deprecated.', E_USER_DEPRECATED);
433
            }
434
435
            // The value describes a bit more about the schema.
436 66
            switch ($node['type']) {
437 66
                case 'array':
438 11
                    if (isset($value['items'])) {
439
                        // The value includes array schema information.
440 4
                        $node = array_replace($node, $value);
441
                    } else {
442 7
                        $node['items'] = $this->parseInternal($value);
443
                    }
444 11
                    break;
445 56
                case 'object':
446
                    // The value is a schema of the object.
447 12
                    if (isset($value['properties'])) {
448
                        list($node['properties']) = $this->parseProperties($value['properties']);
449
                    } else {
450 12
                        list($node['properties'], $required) = $this->parseProperties($value);
451 12
                        if (!empty($required)) {
452 12
                            $node['required'] = $required;
453
                        }
454
                    }
455 12
                    break;
456
                default:
457 44
                    $node = array_replace($node, $value);
458 66
                    break;
459
            }
460 130
        } elseif (is_string($value)) {
461 102
            if ($node['type'] === 'array' && $arrType = $this->getType($value)) {
462 6
                $node['items'] = ['type' => $arrType];
463 98
            } elseif (!empty($value)) {
464 102
                $node['description'] = $value;
465
            }
466 33
        } elseif ($value === null) {
467
            // Parse child elements.
468 29
            if ($node['type'] === 'array' && isset($node['items'])) {
469
                // The value includes array schema information.
470
                $node['items'] = $this->parseInternal($node['items']);
471 29
            } elseif ($node['type'] === 'object' && isset($node['properties'])) {
472 1
                list($node['properties']) = $this->parseProperties($node['properties']);
473
474
            }
475
        }
476
477 170
        if (is_array($node)) {
478 169
            if (!empty($node['allowNull'])) {
479 1
                $node['nullable'] = true;
480
            }
481 169
            unset($node['allowNull']);
482
483 169
            if ($node['type'] === null || $node['type'] === []) {
484 4
                unset($node['type']);
485
            }
486
        }
487
488 170
        return $node;
489
    }
490
491
    /**
492
     * Parse the schema for an object's properties.
493
     *
494
     * @param array $arr An object property schema.
495
     * @return array Returns a schema array suitable to be placed in the **properties** key of a schema.
496
     */
497 111
    private function parseProperties(array $arr): array {
498 111
        $properties = [];
499 111
        $requiredProperties = [];
500 111
        foreach ($arr as $key => $value) {
501
            // Fix a schema specified as just a value.
502 111
            if (is_int($key)) {
503 82
                if (is_string($value)) {
504 82
                    $key = $value;
505 82
                    $value = '';
506
                } else {
507
                    throw new \InvalidArgumentException("Schema at position $key is not a valid parameter.", 500);
508
                }
509
            }
510
511
            // The parameter is defined in the key.
512 111
            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

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

816
            if ((null !== $minItems = $field->val('minItems')) && count(/** @scrutinizer ignore-type */ $value) < $minItems) {
Loading history...
817 1
                $field->addError(
818 1
                    'minItems',
819
                    [
820 1
                        'messageCode' => '{field} must contain at least {minItems} {minItems,plural,item}.',
821 1
                        'minItems' => $minItems,
822 1
                        'status' => 422
823
                    ]
824
                );
825
            }
826 27
            if ((null !== $maxItems = $field->val('maxItems')) && count($value) > $maxItems) {
827 1
                $field->addError(
828 1
                    'maxItems',
829
                    [
830 1
                        'messageCode' => '{field} must contain no more than {maxItems} {maxItems,plural,item}.',
831 1
                        'maxItems' => $maxItems,
832 1
                        'status' => 422
833
                    ]
834
                );
835
            }
836
837 27
            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

837
            if ($field->val('uniqueItems') && count($value) > count(array_unique(/** @scrutinizer ignore-type */ $value))) {
Loading history...
838 1
                $field->addError(
839 1
                    'uniqueItems',
840
                    [
841 1
                        'messageCode' => '{field} must contain unique items.',
842
                        'status' => 422,
843
                    ]
844
                );
845
            }
846
847 27
            if ($field->val('items') !== null) {
848 19
                $result = [];
849
850
                // Validate each of the types.
851 19
                $itemValidation = new ValidationField(
852 19
                    $field->getValidation(), $field->val('items'), '', $field->getSchemaPath().'/items', $field->getOptions()
853
                );
854
855 19
                $count = 0;
856 19
                foreach ($value as $i => $item) {
857 19
                    $itemValidation->setName($field->getName()."/$i");
858 19
                    $validItem = $this->validateField($item, $itemValidation);
859 19
                    if (Invalid::isValid($validItem)) {
860 19
                        $result[] = $validItem;
861
                    }
862 19
                    $count++;
863
                }
864
865 19
                return empty($result) && $count > 0 ? Invalid::value() : $result;
866
            } else {
867
                // Cast the items into a proper numeric array.
868 8
                $result = is_array($value) ? array_values($value) : iterator_to_array($value);
869 8
                return $result;
870
            }
871
        }
872
    }
873
874
    /**
875
     * Validate a boolean value.
876
     *
877
     * @param mixed $value The value to validate.
878
     * @param ValidationField $field The validation results to add.
879
     * @return bool|Invalid Returns the cleaned value or invalid if validation fails.
880
     */
881 32
    protected function validateBoolean($value, ValidationField $field) {
882 32
        $value = $value === null ? $value : filter_var($value, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE);
883 32
        if ($value === null) {
884 4
            $field->addTypeError('boolean');
885 4
            return Invalid::value();
886
        }
887
888 29
        return $value;
889
    }
890
891
    /**
892
     * Validate a date time.
893
     *
894
     * @param mixed $value The value to validate.
895
     * @param ValidationField $field The validation results to add.
896
     * @return \DateTimeInterface|Invalid Returns the cleaned value or **null** if it isn't valid.
897
     */
898 14
    protected function validateDatetime($value, ValidationField $field) {
899 14
        if ($value instanceof \DateTimeInterface) {
900
            // do nothing, we're good
901 11
        } elseif (is_string($value) && $value !== '' && !is_numeric($value)) {
902
            try {
903 7
                $dt = new \DateTimeImmutable($value);
904 6
                if ($dt) {
0 ignored issues
show
introduced by
$dt is of type DateTimeImmutable, thus it always evaluated to true.
Loading history...
905 6
                    $value = $dt;
906
                } else {
907 6
                    $value = null;
908
                }
909 1
            } catch (\Throwable $ex) {
910 7
                $value = Invalid::value();
911
            }
912 4
        } elseif (is_int($value) && $value > 0) {
913
            try {
914 1
                $value = new \DateTimeImmutable('@'.(string)round($value));
915
            } catch (\Throwable $ex) {
916 1
                $value = Invalid::value();
917
            }
918
        } else {
919 3
            $value = Invalid::value();
920
        }
921
922 14
        if (Invalid::isInvalid($value)) {
923 4
            $field->addTypeError('datetime');
924
        }
925 14
        return $value;
926
    }
927
928
    /**
929
     * Validate a float.
930
     *
931
     * @param mixed $value The value to validate.
932
     * @param ValidationField $field The validation results to add.
933
     * @return float|Invalid Returns a number or **null** if validation fails.
934
     */
935 17
    protected function validateNumber($value, ValidationField $field) {
936 17
        $result = filter_var($value, FILTER_VALIDATE_FLOAT);
937 17
        if ($result === false) {
938 4
            $field->addTypeError('number');
939 4
            return Invalid::value();
940
        }
941
942 13
        $result = $this->validateNumberProperties($result, $field);
943
944 13
        return $result;
945
    }
946
    /**
947
     * Validate and integer.
948
     *
949
     * @param mixed $value The value to validate.
950
     * @param ValidationField $field The validation results to add.
951
     * @return int|Invalid Returns the cleaned value or **null** if validation fails.
952
     */
953 51
    protected function validateInteger($value, ValidationField $field) {
954 51
        if ($field->val('format') === 'timestamp') {
955 7
            return $this->validateTimestamp($value, $field);
956
        }
957
958 46
        $result = filter_var($value, FILTER_VALIDATE_INT);
959
960 46
        if ($result === false) {
961 9
            $field->addTypeError('integer');
962 9
            return Invalid::value();
963
        }
964
965 41
        $result = $this->validateNumberProperties($result, $field);
966
967 41
        return $result;
968
    }
969
970
    /**
971
     * Validate an object.
972
     *
973
     * @param mixed $value The value to validate.
974
     * @param ValidationField $field The validation results to add.
975
     * @return object|Invalid Returns a clean object or **null** if validation fails.
976
     */
977 110
    protected function validateObject($value, ValidationField $field) {
978 110
        if (!$this->isArray($value) || isset($value[0])) {
979 6
            $field->addTypeError('object');
980 6
            return Invalid::value();
981 110
        } elseif (is_array($field->val('properties')) || null !== $field->val('additionalProperties')) {
982
            // Validate the data against the internal schema.
983 103
            $value = $this->validateProperties($value, $field);
984 7
        } elseif (!is_array($value)) {
985 3
            $value = $this->toObjectArray($value);
986
        }
987
988 108
        if (($maxProperties = $field->val('maxProperties')) && count($value) > $maxProperties) {
989 1
            $field->addError(
990 1
                'maxItems',
991
                [
992 1
                    'messageCode' => '{field} must contain no more than {maxProperties} {maxProperties,plural,item}.',
993 1
                    'maxItems' => $maxProperties,
994 1
                    'status' => 422
995
                ]
996
            );
997
        }
998
999 108
        if (($minProperties = $field->val('minProperties')) && count($value) < $minProperties) {
1000 1
            $field->addError(
1001 1
                'minItems',
1002
                [
1003 1
                    'messageCode' => '{field} must contain at least {minProperties} {minProperties,plural,item}.',
1004 1
                    'minItems' => $minProperties,
1005 1
                    'status' => 422
1006
                ]
1007
            );
1008
        }
1009
1010 108
        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...
1011
    }
1012
1013
    /**
1014
     * Validate data against the schema and return the result.
1015
     *
1016
     * @param array|\Traversable&\ArrayAccess $data The data to validate.
1017
     * @param ValidationField $field This argument will be filled with the validation result.
1018
     * @return array|Invalid Returns a clean array with only the appropriate properties and the data coerced to proper types.
1019
     * or invalid if there are no valid properties.
1020
     */
1021 103
    protected function validateProperties($data, ValidationField $field) {
1022 103
        $properties = $field->val('properties', []);
1023 103
        $additionalProperties = $field->val('additionalProperties');
1024 103
        $required = array_flip($field->val('required', []));
1025 103
        $isRequest = $field->isRequest();
1026 103
        $isResponse = $field->isResponse();
1027
1028 103
        if (is_array($data)) {
0 ignored issues
show
introduced by
The condition is_array($data) is always false.
Loading history...
1029 99
            $keys = array_keys($data);
1030 99
            $clean = [];
1031
        } else {
1032 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

1032
            $keys = array_keys(iterator_to_array(/** @scrutinizer ignore-type */ $data));
Loading history...
1033 4
            $class = get_class($data);
1034 4
            $clean = new $class;
1035
1036 4
            if ($clean instanceof \ArrayObject && $data instanceof \ArrayObject) {
1037 3
                $clean->setFlags($data->getFlags());
1038 3
                $clean->setIteratorClass($data->getIteratorClass());
1039
            }
1040
        }
1041 103
        $keys = array_combine(array_map('strtolower', $keys), $keys);
1042
1043 103
        $propertyField = new ValidationField($field->getValidation(), [], '', '', $field->getOptions());
1044
1045
        // Loop through the schema fields and validate each one.
1046 103
        foreach ($properties as $propertyName => $property) {
1047
            $propertyField
1048 102
                ->setField($property)
1049 102
                ->setName(ltrim($field->getName().'/'.$propertyField->escapeRef($propertyName), '/'))
1050 102
                ->setSchemaPath($field->getSchemaPath().'/properties/'.$propertyField->escapeRef($propertyName))
1051
            ;
1052
1053 102
            $lName = strtolower($propertyName);
1054 102
            $isRequired = isset($required[$propertyName]);
1055
1056
            // Check to strip this field if it is readOnly or writeOnly.
1057 102
            if (($isRequest && $propertyField->val('readOnly')) || ($isResponse && $propertyField->val('writeOnly'))) {
1058 6
                unset($keys[$lName]);
1059 6
                continue;
1060
            }
1061
1062
            // Check for required fields.
1063 102
            if (!array_key_exists($lName, $keys)) {
1064 28
                if ($field->isSparse()) {
1065
                    // Sparse validation can leave required fields out.
1066 27
                } elseif ($propertyField->hasVal('default')) {
1067 2
                    $clean[$propertyName] = $propertyField->val('default');
1068 25
                } elseif ($isRequired) {
1069 28
                    $propertyField->addError('missingField', ['messageCode' => '{field} is required.']);
1070
                }
1071
            } else {
1072 91
                $value = $data[$keys[$lName]];
1073
1074 91
                if (in_array($value, [null, ''], true) && !$isRequired && !($propertyField->val('nullable') || $propertyField->hasType('null'))) {
1075 5
                    if ($propertyField->getType() !== 'string' || $value === null) {
1076 2
                        continue;
1077
                    }
1078
                }
1079
1080 89
                $clean[$propertyName] = $this->validateField($value, $propertyField);
1081
            }
1082
1083 100
            unset($keys[$lName]);
1084
        }
1085
1086
        // Look for extraneous properties.
1087 103
        if (!empty($keys)) {
1088 16
            if ($additionalProperties) {
1089 5
                $propertyField = new ValidationField(
1090 5
                    $field->getValidation(),
1091 5
                    $additionalProperties,
1092 5
                    '',
1093 5
                    $field->getSchemaPath().'/additionalProperties',
1094 5
                    $field->getOptions()
1095
                );
1096
1097 5
                foreach ($keys as $key) {
1098
                    $propertyField
1099 5
                        ->setName(ltrim($field->getName()."/$key", '/'));
1100
1101 5
                    $valid = $this->validateField($data[$key], $propertyField);
1102 5
                    if (Invalid::isValid($valid)) {
1103 5
                        $clean[$key] = $valid;
1104
                    }
1105
                }
1106 11
            } elseif ($this->hasFlag(Schema::VALIDATE_EXTRA_PROPERTY_NOTICE)) {
1107 2
                $msg = sprintf("%s has unexpected field(s): %s.", $field->getName() ?: 'value', implode(', ', $keys));
1108 2
                trigger_error($msg, E_USER_NOTICE);
1109 9
            } elseif ($this->hasFlag(Schema::VALIDATE_EXTRA_PROPERTY_EXCEPTION)) {
1110 2
                $field->addError('invalid', [
1111 2
                    'messageCode' => '{field} has {extra,plural,an unexpected field,unexpected fields}: {extra}.',
1112 2
                    'extra' => array_values($keys),
1113 2
                    'status' => 422
1114
                ]);
1115
            }
1116
        }
1117
1118 101
        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...
1119
    }
1120
1121
    /**
1122
     * Validate a string.
1123
     *
1124
     * @param mixed $value The value to validate.
1125
     * @param ValidationField $field The validation results to add.
1126
     * @return string|Invalid Returns the valid string or **null** if validation fails.
1127
     */
1128 81
    protected function validateString($value, ValidationField $field) {
1129 81
        if ($field->val('format') === 'date-time') {
1130 12
            $result = $this->validateDatetime($value, $field);
1131 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...
1132
        }
1133
1134 70
        if (is_string($value) || is_numeric($value)) {
1135 68
            $value = $result = (string)$value;
1136
        } else {
1137 5
            $field->addTypeError('string');
1138 5
            return Invalid::value();
1139
        }
1140
1141 68
        if (($minLength = $field->val('minLength', 0)) > 0 && mb_strlen($value) < $minLength) {
1142 4
            if (!empty($field->getName()) && $minLength === 1) {
1143 2
                $field->addError('missingField', ['messageCode' => '{field} is required.', 'status' => 422]);
1144
            } else {
1145 2
                $field->addError(
1146 2
                    'minLength',
1147
                    [
1148 2
                        'messageCode' => '{field} should be at least {minLength} {minLength,plural,character} long.',
1149 2
                        'minLength' => $minLength,
1150 2
                        'status' => 422
1151
                    ]
1152
                );
1153
            }
1154
        }
1155 68
        if (($maxLength = $field->val('maxLength', 0)) > 0 && mb_strlen($value) > $maxLength) {
1156 1
            $field->addError(
1157 1
                'maxLength',
1158
                [
1159 1
                    'messageCode' => '{field} is {overflow} {overflow,plural,characters} too long.',
1160 1
                    'maxLength' => $maxLength,
1161 1
                    'overflow' => mb_strlen($value) - $maxLength,
1162 1
                    'status' => 422
1163
                ]
1164
            );
1165
        }
1166 68
        if ($pattern = $field->val('pattern')) {
1167 4
            $regex = '`'.str_replace('`', preg_quote('`', '`'), $pattern).'`';
1168
1169 4
            if (!preg_match($regex, $value)) {
1170 2
                $field->addError(
1171 2
                    'invalid',
1172
                    [
1173 2
                        'messageCode' => '{field} is in the incorrect format.',
1174
                        'status' => 422
1175
                    ]
1176
                );
1177
            }
1178
        }
1179 68
        if ($format = $field->val('format')) {
1180 11
            $type = $format;
1181 11
            switch ($format) {
1182 11
                case 'date':
1183
                    $result = $this->validateDatetime($result, $field);
1184
                    if ($result instanceof \DateTimeInterface) {
1185
                        $result = $result->format("Y-m-d\T00:00:00P");
1186
                    }
1187
                    break;
1188 11
                case 'email':
1189 1
                    $result = filter_var($result, FILTER_VALIDATE_EMAIL);
1190 1
                    break;
1191 10
                case 'ipv4':
1192 1
                    $type = 'IPv4 address';
1193 1
                    $result = filter_var($result, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4);
1194 1
                    break;
1195 9
                case 'ipv6':
1196 1
                    $type = 'IPv6 address';
1197 1
                    $result = filter_var($result, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6);
1198 1
                    break;
1199 8
                case 'ip':
1200 1
                    $type = 'IP address';
1201 1
                    $result = filter_var($result, FILTER_VALIDATE_IP);
1202 1
                    break;
1203 7
                case 'uri':
1204 7
                    $type = 'URI';
1205 7
                    $result = filter_var($result, FILTER_VALIDATE_URL);
1206 7
                    break;
1207
                default:
1208
                    trigger_error("Unrecognized format '$format'.", E_USER_NOTICE);
1209
            }
1210 11
            if ($result === false) {
1211 5
                $field->addTypeError($type);
1212
            }
1213
        }
1214
1215 68
        if ($field->isValid()) {
1216 60
            return $result;
1217
        } else {
1218 12
            return Invalid::value();
1219
        }
1220
    }
1221
1222
    /**
1223
     * Validate a unix timestamp.
1224
     *
1225
     * @param mixed $value The value to validate.
1226
     * @param ValidationField $field The field being validated.
1227
     * @return int|Invalid Returns a valid timestamp or invalid if the value doesn't validate.
1228
     */
1229 8
    protected function validateTimestamp($value, ValidationField $field) {
1230 8
        if (is_numeric($value) && $value > 0) {
1231 3
            $result = (int)$value;
1232 5
        } elseif (is_string($value) && $ts = strtotime($value)) {
1233 1
            $result = $ts;
1234
        } else {
1235 4
            $field->addTypeError('timestamp');
1236 4
            $result = Invalid::value();
1237
        }
1238 8
        return $result;
1239
    }
1240
1241
    /**
1242
     * Validate a null value.
1243
     *
1244
     * @param mixed $value The value to validate.
1245
     * @param ValidationField $field The error collector for the field.
1246
     * @return null|Invalid Returns **null** or invalid.
1247
     */
1248
    protected function validateNull($value, ValidationField $field) {
1249
        if ($value === null) {
1250
            return null;
1251
        }
1252
        $field->addError('invalid', ['messageCode' => '{field} should be null.', 'status' => 422]);
1253
        return Invalid::value();
1254
    }
1255
1256
    /**
1257
     * Validate a value against an enum.
1258
     *
1259
     * @param mixed $value The value to test.
1260
     * @param ValidationField $field The validation object for adding errors.
1261
     * @return mixed|Invalid Returns the value if it is one of the enumerated values or invalid otherwise.
1262
     */
1263 191
    protected function validateEnum($value, ValidationField $field) {
1264 191
        $enum = $field->val('enum');
1265 191
        if (empty($enum)) {
1266 190
            return $value;
1267
        }
1268
1269 1
        if (!in_array($value, $enum, true)) {
1270 1
            $field->addError(
1271 1
                'invalid',
1272
                [
1273 1
                    'messageCode' => '{field} must be one of: {enum}.',
1274 1
                    'enum' => $enum,
1275 1
                    'status' => 422
1276
                ]
1277
            );
1278 1
            return Invalid::value();
1279
        }
1280 1
        return $value;
1281
    }
1282
1283
    /**
1284
     * Call all of the filters attached to a field.
1285
     *
1286
     * @param mixed $value The field value being filtered.
1287
     * @param ValidationField $field The validation object.
1288
     * @return mixed Returns the filtered value. If there are no filters for the field then the original value is returned.
1289
     */
1290 199
    protected function callFilters($value, ValidationField $field) {
1291
        // Strip array references in the name except for the last one.
1292 199
        $key = $field->getSchemaPath();
1293 199
        if (!empty($this->filters[$key])) {
1294 1
            foreach ($this->filters[$key] as $filter) {
1295 1
                $value = call_user_func($filter, $value, $field);
1296
            }
1297
        }
1298 199
        return $value;
1299
    }
1300
1301
    /**
1302
     * Call all of the validators attached to a field.
1303
     *
1304
     * @param mixed $value The field value being validated.
1305
     * @param ValidationField $field The validation object to add errors.
1306
     */
1307 191
    protected function callValidators($value, ValidationField $field) {
1308 191
        $valid = true;
1309
1310
        // Strip array references in the name except for the last one.
1311 191
        $key = $field->getSchemaPath();
1312 191
        if (!empty($this->validators[$key])) {
1313 4
            foreach ($this->validators[$key] as $validator) {
1314 4
                $r = call_user_func($validator, $value, $field);
1315
1316 4
                if ($r === false || Invalid::isInvalid($r)) {
1317 4
                    $valid = false;
1318
                }
1319
            }
1320
        }
1321
1322
        // Add an error on the field if the validator hasn't done so.
1323 191
        if (!$valid && $field->isValid()) {
1324
            $field->addError('invalid', ['messageCode' => '{field} is invalid.', 'status' => 422]);
1325
        }
1326 191
    }
1327
1328
    /**
1329
     * Specify data which should be serialized to JSON.
1330
     *
1331
     * This method specifically returns data compatible with the JSON schema format.
1332
     *
1333
     * @return mixed Returns data which can be serialized by **json_encode()**, which is a value of any type other than a resource.
1334
     * @link http://php.net/manual/en/jsonserializable.jsonserialize.php
1335
     * @link http://json-schema.org/
1336
     */
1337 16
    public function jsonSerialize() {
1338
        $fix = function ($schema) use (&$fix) {
1339 16
            if ($schema instanceof Schema) {
1340 1
                return $schema->jsonSerialize();
1341
            }
1342
1343 16
            if (!empty($schema['type'])) {
1344 15
                $types = (array)$schema['type'];
1345
1346 15
                foreach ($types as $i => &$type) {
1347
                    // Swap datetime and timestamp to other types with formats.
1348 15
                    if ($type === 'datetime') {
1349 4
                        $type = 'string';
1350 4
                        $schema['format'] = 'date-time';
1351 14
                    } elseif ($schema['type'] === 'timestamp') {
1352 2
                        $type = 'integer';
1353 15
                        $schema['format'] = 'timestamp';
1354
                    }
1355
                }
1356 15
                $types = array_unique($types);
1357 15
                $schema['type'] = count($types) === 1 ? reset($types) : $types;
1358
            }
1359
1360 16
            if (!empty($schema['items'])) {
1361 4
                $schema['items'] = $fix($schema['items']);
1362
            }
1363 16
            if (!empty($schema['properties'])) {
1364 11
                $properties = [];
1365 11
                foreach ($schema['properties'] as $key => $property) {
1366 11
                    $properties[$key] = $fix($property);
1367
                }
1368 11
                $schema['properties'] = $properties;
1369
            }
1370
1371 16
            return $schema;
1372 16
        };
1373
1374 16
        $result = $fix($this->schema);
1375
1376 16
        return $result;
1377
    }
1378
1379
    /**
1380
     * Look up a type based on its alias.
1381
     *
1382
     * @param string $alias The type alias or type name to lookup.
1383
     * @return mixed
1384
     */
1385 166
    protected function getType($alias) {
1386 166
        if (isset(self::$types[$alias])) {
1387
            return $alias;
1388
        }
1389 166
        foreach (self::$types as $type => $aliases) {
1390 166
            if (in_array($alias, $aliases, true)) {
1391 166
                return $type;
1392
            }
1393
        }
1394 11
        return null;
1395
    }
1396
1397
    /**
1398
     * Get the class that's used to contain validation information.
1399
     *
1400
     * @return Validation|string Returns the validation class.
1401
     */
1402 199
    public function getValidationClass() {
1403 199
        return $this->validationClass;
1404
    }
1405
1406
    /**
1407
     * Set the class that's used to contain validation information.
1408
     *
1409
     * @param Validation|string $class Either the name of a class or a class that will be cloned.
1410
     * @return $this
1411
     */
1412 1
    public function setValidationClass($class) {
1413 1
        if (!is_a($class, Validation::class, true)) {
1414
            throw new \InvalidArgumentException("$class must be a subclass of ".Validation::class, 500);
1415
        }
1416
1417 1
        $this->validationClass = $class;
1418 1
        return $this;
1419
    }
1420
1421
    /**
1422
     * Create a new validation instance.
1423
     *
1424
     * @return Validation Returns a validation object.
1425
     */
1426 199
    protected function createValidation() {
1427 199
        $class = $this->getValidationClass();
1428
1429 199
        if ($class instanceof Validation) {
1430 1
            $result = clone $class;
1431
        } else {
1432 199
            $result = new $class;
1433
        }
1434 199
        return $result;
1435
    }
1436
1437
    /**
1438
     * Check whether or not a value is an array or accessible like an array.
1439
     *
1440
     * @param mixed $value The value to check.
1441
     * @return bool Returns **true** if the value can be used like an array or **false** otherwise.
1442
     */
1443 110
    private function isArray($value) {
1444 110
        return is_array($value) || ($value instanceof \ArrayAccess && $value instanceof \Traversable);
1445
    }
1446
1447
    /**
1448
     * Cast a value to an array.
1449
     *
1450
     * @param \Traversable $value The value to convert.
1451
     * @return array Returns an array.
1452
     */
1453 3
    private function toObjectArray(\Traversable $value) {
1454 3
        $class = get_class($value);
1455 3
        if ($value instanceof \ArrayObject) {
1456 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...
1457 1
        } elseif ($value instanceof \ArrayAccess) {
1458 1
            $r = new $class;
1459 1
            foreach ($value as $k => $v) {
1460 1
                $r[$k] = $v;
1461
            }
1462 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...
1463
        }
1464
        return iterator_to_array($value);
1465
    }
1466
1467
    /**
1468
     * Return a sparse version of this schema.
1469
     *
1470
     * A sparse schema has no required properties.
1471
     *
1472
     * @return Schema Returns a new sparse schema.
1473
     */
1474 2
    public function withSparse() {
1475 2
        $sparseSchema = $this->withSparseInternal($this, new \SplObjectStorage());
1476 2
        return $sparseSchema;
1477
    }
1478
1479
    /**
1480
     * The internal implementation of `Schema::withSparse()`.
1481
     *
1482
     * @param array|Schema $schema The schema to make sparse.
1483
     * @param \SplObjectStorage $schemas Collected sparse schemas that have already been made.
1484
     * @return mixed
1485
     */
1486 2
    private function withSparseInternal($schema, \SplObjectStorage $schemas) {
1487 2
        if ($schema instanceof Schema) {
1488 2
            if ($schemas->contains($schema)) {
1489 1
                return $schemas[$schema];
1490
            } else {
1491 2
                $schemas[$schema] = $sparseSchema = new Schema();
1492 2
                $sparseSchema->schema = $schema->withSparseInternal($schema->schema, $schemas);
1493 2
                if ($id = $sparseSchema->getID()) {
1494
                    $sparseSchema->setID($id.'Sparse');
1495
                }
1496
1497 2
                return $sparseSchema;
1498
            }
1499
        }
1500
1501 2
        unset($schema['required']);
1502
1503 2
        if (isset($schema['items'])) {
1504 1
            $schema['items'] = $this->withSparseInternal($schema['items'], $schemas);
1505
        }
1506 2
        if (isset($schema['properties'])) {
1507 2
            foreach ($schema['properties'] as $name => &$property) {
1508 2
                $property = $this->withSparseInternal($property, $schemas);
1509
            }
1510
        }
1511
1512 2
        return $schema;
1513
    }
1514
1515
    /**
1516
     * Filter a field's value using built in and custom filters.
1517
     *
1518
     * @param mixed $value The original value of the field.
1519
     * @param ValidationField $field The field information for the field.
1520
     * @return mixed Returns the filtered field or the original field value if there are no filters.
1521
     */
1522 199
    private function filterField($value, ValidationField $field) {
1523
        // Check for limited support for Open API style.
1524 199
        if (!empty($field->val('style')) && is_string($value)) {
1525 8
            $doFilter = true;
1526 8
            if ($field->hasType('boolean') && in_array($value, ['true', 'false', '0', '1'], true)) {
1527 4
                $doFilter = false;
1528 4
            } elseif ($field->hasType('integer') || $field->hasType('number') && is_numeric($value)) {
1529
                $doFilter = false;
1530
            }
1531
1532 8
            if ($doFilter) {
1533 4
                switch ($field->val('style')) {
1534 4
                    case 'form':
1535 2
                        $value = explode(',', $value);
1536 2
                        break;
1537 2
                    case 'spaceDelimited':
1538 1
                        $value = explode(' ', $value);
1539 1
                        break;
1540 1
                    case 'pipeDelimited':
1541 1
                        $value = explode('|', $value);
1542 1
                        break;
1543
                }
1544
            }
1545
        }
1546
1547 199
        $value = $this->callFilters($value, $field);
1548
1549 199
        return $value;
1550
    }
1551
1552
    /**
1553
     * Whether a offset exists.
1554
     *
1555
     * @param mixed $offset An offset to check for.
1556
     * @return boolean true on success or false on failure.
1557
     * @link http://php.net/manual/en/arrayaccess.offsetexists.php
1558
     */
1559 6
    public function offsetExists($offset) {
1560 6
        return isset($this->schema[$offset]);
1561
    }
1562
1563
    /**
1564
     * Offset to retrieve.
1565
     *
1566
     * @param mixed $offset The offset to retrieve.
1567
     * @return mixed Can return all value types.
1568
     * @link http://php.net/manual/en/arrayaccess.offsetget.php
1569
     */
1570 6
    public function offsetGet($offset) {
1571 6
        return isset($this->schema[$offset]) ? $this->schema[$offset] : null;
1572
    }
1573
1574
    /**
1575
     * Offset to set.
1576
     *
1577
     * @param mixed $offset The offset to assign the value to.
1578
     * @param mixed $value The value to set.
1579
     * @link http://php.net/manual/en/arrayaccess.offsetset.php
1580
     */
1581 1
    public function offsetSet($offset, $value) {
1582 1
        $this->schema[$offset] = $value;
1583 1
    }
1584
1585
    /**
1586
     * Offset to unset.
1587
     *
1588
     * @param mixed $offset The offset to unset.
1589
     * @link http://php.net/manual/en/arrayaccess.offsetunset.php
1590
     */
1591 1
    public function offsetUnset($offset) {
1592 1
        unset($this->schema[$offset]);
1593 1
    }
1594
1595
    /**
1596
     * Validate a field against a single type.
1597
     *
1598
     * @param mixed $value The value to validate.
1599
     * @param string $type The type to validate against.
1600
     * @param ValidationField $field Contains field and validation information.
1601
     * @return mixed Returns the valid value or `Invalid`.
1602
     */
1603 199
    protected function validateSingleType($value, $type, ValidationField $field) {
1604 5
        switch ($type) {
1605 199
            case 'boolean':
1606 32
                $result = $this->validateBoolean($value, $field);
1607 32
                break;
1608 179
            case 'integer':
1609 51
                $result = $this->validateInteger($value, $field);
1610 51
                break;
1611 164
            case 'number':
1612 17
                $result = $this->validateNumber($value, $field);
1613 17
                break;
1614 155
            case 'string':
1615 81
                $result = $this->validateString($value, $field);
1616 81
                break;
1617 131
            case 'timestamp':
1618 1
                trigger_error('The timestamp type is deprecated. Use an integer with a format of timestamp instead.', E_USER_DEPRECATED);
1619 1
                $result = $this->validateTimestamp($value, $field);
1620 1
                break;
1621 131
            case 'datetime':
1622 2
                trigger_error('The datetime type is deprecated. Use a string with a format of date-time instead.', E_USER_DEPRECATED);
1623 2
                $result = $this->validateDatetime($value, $field);
1624 2
                break;
1625 130
            case 'array':
1626 32
                $result = $this->validateArray($value, $field);
1627 32
                break;
1628 111
            case 'object':
1629 110
                $result = $this->validateObject($value, $field);
1630 108
                break;
1631 5
            case 'null':
1632
                $result = $this->validateNull($value, $field);
1633
                break;
1634
            case null:
1635
                // No type was specified so we are valid.
1636 5
                $result = $value;
1637 5
                break;
1638
            default:
1639
                throw new \InvalidArgumentException("Unrecognized type $type.", 500);
1640
        }
1641 199
        return $result;
1642
    }
1643
1644
    /**
1645
     * Validate a field against multiple basic types.
1646
     *
1647
     * The first validation that passes will be returned. If no type can be validated against then validation will fail.
1648
     *
1649
     * @param mixed $value The value to validate.
1650
     * @param string[] $types The types to validate against.
1651
     * @param ValidationField $field Contains field and validation information.
1652
     * @return mixed Returns the valid value or `Invalid`.
1653
     */
1654 29
    private function validateMultipleTypes($value, array $types, ValidationField $field) {
1655 29
        trigger_error('Multiple schema types are deprecated.', E_USER_DEPRECATED);
1656
1657
        // First check for an exact type match.
1658 29
        switch (gettype($value)) {
1659 29
            case 'boolean':
1660 4
                if (in_array('boolean', $types)) {
1661 4
                    $singleType = 'boolean';
1662
                }
1663 4
                break;
1664 26
            case 'integer':
1665 7
                if (in_array('integer', $types)) {
1666 5
                    $singleType = 'integer';
1667 2
                } elseif (in_array('number', $types)) {
1668 1
                    $singleType = 'number';
1669
                }
1670 7
                break;
1671 21
            case 'double':
1672 4
                if (in_array('number', $types)) {
1673 4
                    $singleType = 'number';
1674
                } elseif (in_array('integer', $types)) {
1675
                    $singleType = 'integer';
1676
                }
1677 4
                break;
1678 18
            case 'string':
1679 9
                if (in_array('datetime', $types) && preg_match(self::$DATE_REGEX, $value)) {
1680 1
                    $singleType = 'datetime';
1681 8
                } elseif (in_array('string', $types)) {
1682 4
                    $singleType = 'string';
1683
                }
1684 9
                break;
1685 10
            case 'array':
1686 10
                if (in_array('array', $types) && in_array('object', $types)) {
1687 1
                    $singleType = isset($value[0]) || empty($value) ? 'array' : 'object';
1688 9
                } elseif (in_array('object', $types)) {
1689
                    $singleType = 'object';
1690 9
                } elseif (in_array('array', $types)) {
1691 9
                    $singleType = 'array';
1692
                }
1693 10
                break;
1694 1
            case 'NULL':
1695
                if (in_array('null', $types)) {
1696
                    $singleType = $this->validateSingleType($value, 'null', $field);
1697
                }
1698
                break;
1699
        }
1700 29
        if (!empty($singleType)) {
1701 25
            return $this->validateSingleType($value, $singleType, $field);
1702
        }
1703
1704
        // Clone the validation field to collect errors.
1705 6
        $typeValidation = new ValidationField(new Validation(), $field->getField(), '', '', $field->getOptions());
1706
1707
        // Try and validate against each type.
1708 6
        foreach ($types as $type) {
1709 6
            $result = $this->validateSingleType($value, $type, $typeValidation);
1710 6
            if (Invalid::isValid($result)) {
1711 6
                return $result;
1712
            }
1713
        }
1714
1715
        // Since we got here the value is invalid.
1716
        $field->merge($typeValidation->getValidation());
1717
        return Invalid::value();
1718
    }
1719
1720
    /**
1721
     * Validate specific numeric validation properties.
1722
     *
1723
     * @param int|float $value The value to test.
1724
     * @param ValidationField $field Field information.
1725
     * @return int|float|Invalid Returns the number of invalid.
1726
     */
1727 51
    private function validateNumberProperties($value, ValidationField $field) {
1728 51
        $count = $field->getErrorCount();
1729
1730 51
        if ($multipleOf = $field->val('multipleOf')) {
1731 4
            $divided = $value / $multipleOf;
1732
1733 4
            if ($divided != round($divided)) {
1734 2
                $field->addError('multipleOf', ['messageCode' => '{field} is not a multiple of {multipleOf}.', 'status' => 422, 'multipleOf' => $multipleOf]);
1735
            }
1736
        }
1737
1738 51
        if ($maximum = $field->val('maximum')) {
1739 4
            $exclusive = $field->val('exclusiveMaximum');
1740
1741 4
            if ($value > $maximum || ($exclusive && $value == $maximum)) {
1742 2
                if ($exclusive) {
1743 1
                    $field->addError('maximum', ['messageCode' => '{field} is greater than or equal to {maximum}.', 'status' => 422, 'maximum' => $maximum]);
1744
                } else {
1745 1
                    $field->addError('maximum', ['messageCode' => '{field} is greater than {maximum}.', 'status' => 422, 'maximum' => $maximum]);
1746
                }
1747
1748
            }
1749
        }
1750
1751 51
        if ($minimum = $field->val('minimum')) {
1752 4
            $exclusive = $field->val('exclusiveMinimum');
1753
1754 4
            if ($value < $minimum || ($exclusive && $value == $minimum)) {
1755 2
                if ($exclusive) {
1756 1
                    $field->addError('minimum', ['messageCode' => '{field} is greater than or equal to {minimum}.', 'status' => 422, 'minimum' => $minimum]);
1757
                } else {
1758 1
                    $field->addError('minimum', ['messageCode' => '{field} is greater than {minimum}.', 'status' => 422, 'minimum' => $minimum]);
1759
                }
1760
1761
            }
1762
        }
1763
1764 51
        return $field->getErrorCount() === $count ? $value : Invalid::value();
1765
    }
1766
1767
    /**
1768
     * Parse a nested field name selector.
1769
     *
1770
     * Field selectors should be separated by "/" characters, but may currently be separated by "." characters which
1771
     * triggers a deprecated error.
1772
     *
1773
     * @param string $field The field selector.
1774
     * @return string Returns the field selector in the correct format.
1775
     */
1776 13
    private function parseFieldSelector(string $field): string {
1777 13
        if (strlen($field) === 0) {
1778 4
            return $field;
1779
        }
1780
1781 9
        if (strpos($field, '.') !== false) {
1782 1
            if (strpos($field, '/') === false) {
1783 1
                trigger_error('Field selectors must be separated by "/" instead of "."', E_USER_DEPRECATED);
1784
1785 1
                $parts = explode('.', $field);
1786 1
                $parts = @array_map([$this, 'parseFieldSelector'], $parts); // silence because error triggered already.
1787
1788 1
                $field = implode('/', $parts);
1789
            }
1790 9
        } elseif ($field === '[]') {
1791 1
            trigger_error('Field selectors with item selector "[]" must be converted to "items".', E_USER_DEPRECATED);
1792 1
            $field = 'items';
1793 8
        } elseif (strpos($field, '/') === false && !in_array($field, ['items', 'additionalProperties'], true)) {
1794 3
            trigger_error("Field selectors must specify full schema paths. ($field)", E_USER_DEPRECATED);
1795 3
            $field = "/properties/$field";
1796
        }
1797
1798 9
        if (strpos($field, '[]') !== false) {
1799 1
            trigger_error('Field selectors with item selector "[]" must be converted to "/items".', E_USER_DEPRECATED);
1800 1
            $field = str_replace('[]', '/items', $field);
1801
        }
1802
1803 9
        return ltrim($field, '/');
1804
    }
1805
}
1806