Passed
Push — master ( 4c903b...ecf39e )
by Todd
01:33
created

Schema::validateProperties()   F

Complexity

Conditions 26
Paths 135

Size

Total Lines 90
Code Lines 57

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 54
CRAP Score 26

Importance

Changes 0
Metric Value
cc 26
eloc 57
nc 135
nop 2
dl 0
loc 90
ccs 54
cts 54
cp 1
crap 26
rs 3.875
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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

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

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

793
            if ((null !== $minItems = $field->val('minItems')) && count(/** @scrutinizer ignore-type */ $value) < $minItems) {
Loading history...
794 1
                $field->addError(
795 1
                    'minItems',
796
                    [
797 1
                        'messageCode' => '{field} must contain at least {minItems} {minItems,plural,item}.',
798 1
                        'minItems' => $minItems,
799 1
                        'status' => 422
800
                    ]
801
                );
802
            }
803 27
            if ((null !== $maxItems = $field->val('maxItems')) && count($value) > $maxItems) {
804 1
                $field->addError(
805 1
                    'maxItems',
806
                    [
807 1
                        'messageCode' => '{field} must contain no more than {maxItems} {maxItems,plural,item}.',
808 1
                        'maxItems' => $maxItems,
809 1
                        'status' => 422
810
                    ]
811
                );
812
            }
813
814 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

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

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