Completed
Push — master ( 009802...3370db )
by Todd
04:41 queued 02:40
created

Schema::withSparseInternal()   B

Complexity

Conditions 7
Paths 7

Size

Total Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 7.0119

Importance

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

353
        $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...
354 145
        $schema->schema = $schema->parseInternal($arr);
355 145
        return $schema;
356
    }
357
358
    /**
359
     * Parse a schema in short form into a full schema array.
360
     *
361
     * @param array $arr The array to parse into a schema.
362
     * @return array The full schema array.
363
     * @throws \InvalidArgumentException Throws an exception when an item in the schema is invalid.
364
     */
365 145
    protected function parseInternal(array $arr) {
366 145
        if (empty($arr)) {
367
            // An empty schema validates to anything.
368 7
            return [];
369 139
        } elseif (isset($arr['type'])) {
370
            // This is a long form schema and can be parsed as the root.
371
            return $this->parseNode($arr);
372
        } else {
373
            // Check for a root schema.
374 139
            $value = reset($arr);
375 139
            $key = key($arr);
376 139
            if (is_int($key)) {
377 83
                $key = $value;
378 83
                $value = null;
379
            }
380 139
            list ($name, $param) = $this->parseShortParam($key, $value);
381 139
            if (empty($name)) {
382 48
                return $this->parseNode($param, $value);
383
            }
384
        }
385
386
        // If we are here then this is n object schema.
387 94
        list($properties, $required) = $this->parseProperties($arr);
388
389
        $result = [
390 94
            'type' => 'object',
391 94
            'properties' => $properties,
392 94
            'required' => $required
393
        ];
394
395 94
        return array_filter($result);
396
    }
397
398
    /**
399
     * Parse a schema node.
400
     *
401
     * @param array $node The node to parse.
402
     * @param mixed $value Additional information from the node.
403
     * @return array Returns a JSON schema compatible node.
404
     */
405 139
    private function parseNode($node, $value = null) {
406 139
        if (is_array($value)) {
407
            // The value describes a bit more about the schema.
408 59
            switch ($node['type']) {
409 59
                case 'array':
410 11
                    if (isset($value['items'])) {
411
                        // The value includes array schema information.
412 4
                        $node = array_replace($node, $value);
413
                    } else {
414 7
                        $node['items'] = $this->parseInternal($value);
415
                    }
416 11
                    break;
417 49
                case 'object':
418
                    // The value is a schema of the object.
419 12
                    if (isset($value['properties'])) {
420
                        list($node['properties']) = $this->parseProperties($value['properties']);
421
                    } else {
422 12
                        list($node['properties'], $required) = $this->parseProperties($value);
423 12
                        if (!empty($required)) {
424 12
                            $node['required'] = $required;
425
                        }
426
                    }
427 12
                    break;
428
                default:
429 37
                    $node = array_replace($node, $value);
430 59
                    break;
431
            }
432 100
        } elseif (is_string($value)) {
433 80
            if ($node['type'] === 'array' && $arrType = $this->getType($value)) {
434 6
                $node['items'] = ['type' => $arrType];
435 76
            } elseif (!empty($value)) {
436 80
                $node['description'] = $value;
437
            }
438 25
        } elseif ($value === null) {
439
            // Parse child elements.
440 21
            if ($node['type'] === 'array' && isset($node['items'])) {
441
                // The value includes array schema information.
442
                $node['items'] = $this->parseInternal($node['items']);
443 21
            } elseif ($node['type'] === 'object' && isset($node['properties'])) {
444
                list($node['properties']) = $this->parseProperties($node['properties']);
445
446
            }
447
        }
448
449 139
        if (is_array($node)) {
450 138
            if (!empty($node['allowNull'])) {
451 1
                $node['type'] = array_merge((array)$node['type'], ['null']);
452
            }
453 138
            unset($node['allowNull']);
454
455 138
            if ($node['type'] === null || $node['type'] === []) {
456 4
                unset($node['type']);
457
            }
458
        }
459
460 139
        return $node;
461
    }
462
463
    /**
464
     * Parse the schema for an object's properties.
465
     *
466
     * @param array $arr An object property schema.
467
     * @return array Returns a schema array suitable to be placed in the **properties** key of a schema.
468
     */
469 94
    private function parseProperties(array $arr) {
470 94
        $properties = [];
471 94
        $requiredProperties = [];
472 94
        foreach ($arr as $key => $value) {
473
            // Fix a schema specified as just a value.
474 94
            if (is_int($key)) {
475 67
                if (is_string($value)) {
476 67
                    $key = $value;
477 67
                    $value = '';
478
                } else {
479
                    throw new \InvalidArgumentException("Schema at position $key is not a valid parameter.", 500);
480
                }
481
            }
482
483
            // The parameter is defined in the key.
484 94
            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

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

751
            if ((null !== $minItems = $field->val('minItems')) && count(/** @scrutinizer ignore-type */ $value) < $minItems) {
Loading history...
752 1
                $field->addError(
753 1
                    'minItems',
754
                    [
755 1
                        'messageCode' => '{field} must contain at least {minItems} {minItems,plural,item}.',
756 1
                        'minItems' => $minItems,
757 1
                        'status' => 422
758
                    ]
759
                );
760
            }
761 16
            if ((null !== $maxItems = $field->val('maxItems')) && count($value) > $maxItems) {
762 1
                $field->addError(
763 1
                    'maxItems',
764
                    [
765 1
                        'messageCode' => '{field} must contain no more than {maxItems} {maxItems,plural,item}.',
766 1
                        'maxItems' => $maxItems,
767 1
                        'status' => 422
768
                    ]
769
                );
770
            }
771
772 16
            if ($field->val('items') !== null) {
773 12
                $result = [];
774
775
                // Validate each of the types.
776 12
                $itemValidation = new ValidationField(
777 12
                    $field->getValidation(),
778 12
                    $field->val('items'),
779 12
                    '',
780 12
                    $sparse
781
                );
782
783 12
                $count = 0;
784 12
                foreach ($value as $i => $item) {
785 12
                    $itemValidation->setName($field->getName()."[{$i}]");
786 12
                    $validItem = $this->validateField($item, $itemValidation, $sparse);
787 12
                    if (Invalid::isValid($validItem)) {
788 12
                        $result[] = $validItem;
789
                    }
790 12
                    $count++;
791
                }
792
793 12
                return empty($result) && $count > 0 ? Invalid::value() : $result;
794
            } else {
795
                // Cast the items into a proper numeric array.
796 4
                $result = is_array($value) ? array_values($value) : iterator_to_array($value);
797 4
                return $result;
798
            }
799
        }
800
    }
801
802
    /**
803
     * Validate a boolean value.
804
     *
805
     * @param mixed $value The value to validate.
806
     * @param ValidationField $field The validation results to add.
807
     * @return bool|Invalid Returns the cleaned value or invalid if validation fails.
808
     */
809 28
    protected function validateBoolean($value, ValidationField $field) {
810 28
        $value = $value === null ? $value : filter_var($value, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE);
811 28
        if ($value === null) {
812 4
            $field->addTypeError('boolean');
813 4
            return Invalid::value();
814
        }
815
816 25
        return $value;
817
    }
818
819
    /**
820
     * Validate a date time.
821
     *
822
     * @param mixed $value The value to validate.
823
     * @param ValidationField $field The validation results to add.
824
     * @return \DateTimeInterface|Invalid Returns the cleaned value or **null** if it isn't valid.
825
     */
826 11
    protected function validateDatetime($value, ValidationField $field) {
827 11
        if ($value instanceof \DateTimeInterface) {
828
            // do nothing, we're good
829 10
        } elseif (is_string($value) && $value !== '' && !is_numeric($value)) {
830
            try {
831 7
                $dt = new \DateTimeImmutable($value);
832 6
                if ($dt) {
0 ignored issues
show
introduced by
$dt is of type DateTimeImmutable, thus it always evaluated to true.
Loading history...
833 6
                    $value = $dt;
834
                } else {
835 6
                    $value = null;
836
                }
837 1
            } catch (\Exception $ex) {
838 7
                $value = Invalid::value();
839
            }
840 3
        } elseif (is_int($value) && $value > 0) {
841 1
            $value = new \DateTimeImmutable('@'.(string)round($value));
842
        } else {
843 2
            $value = Invalid::value();
844
        }
845
846 11
        if (Invalid::isInvalid($value)) {
847 3
            $field->addTypeError('datetime');
848
        }
849 11
        return $value;
850
    }
851
852
    /**
853
     * Validate a float.
854
     *
855
     * @param mixed $value The value to validate.
856
     * @param ValidationField $field The validation results to add.
857
     * @return float|Invalid Returns a number or **null** if validation fails.
858
     */
859 10
    protected function validateNumber($value, ValidationField $field) {
860 10
        $result = filter_var($value, FILTER_VALIDATE_FLOAT);
861 10
        if ($result === false) {
862 3
            $field->addTypeError('number');
863 3
            return Invalid::value();
864
        }
865 7
        return $result;
866
    }
867
    /**
868
     * Validate and integer.
869
     *
870
     * @param mixed $value The value to validate.
871
     * @param ValidationField $field The validation results to add.
872
     * @return int|Invalid Returns the cleaned value or **null** if validation fails.
873
     */
874 35
    protected function validateInteger($value, ValidationField $field) {
875 35
        $result = filter_var($value, FILTER_VALIDATE_INT);
876
877 35
        if ($result === false) {
878 8
            $field->addTypeError('integer');
879 8
            return Invalid::value();
880
        }
881 31
        return $result;
882
    }
883
884
    /**
885
     * Validate an object.
886
     *
887
     * @param mixed $value The value to validate.
888
     * @param ValidationField $field The validation results to add.
889
     * @param bool $sparse Whether or not this is a sparse validation.
890
     * @return object|Invalid Returns a clean object or **null** if validation fails.
891
     */
892 83
    protected function validateObject($value, ValidationField $field, $sparse = false) {
893 83
        if (!$this->isArray($value) || isset($value[0])) {
894 5
            $field->addTypeError('object');
895 5
            return Invalid::value();
896 83
        } elseif (is_array($field->val('properties'))) {
897
            // Validate the data against the internal schema.
898 80
            $value = $this->validateProperties($value, $field, $sparse);
899 3
        } elseif (!is_array($value)) {
900 3
            $value = $this->toObjectArray($value);
901
        }
902 81
        return $value;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $value also could return the type array which is incompatible with the documented return type object|Garden\Schema\Invalid.
Loading history...
903
    }
904
905
    /**
906
     * Validate data against the schema and return the result.
907
     *
908
     * @param array|\ArrayAccess $data The data to validate.
909
     * @param ValidationField $field This argument will be filled with the validation result.
910
     * @param bool $sparse Whether or not this is a sparse validation.
911
     * @return array|Invalid Returns a clean array with only the appropriate properties and the data coerced to proper types.
912
     * or invalid if there are no valid properties.
913
     */
914 80
    protected function validateProperties($data, ValidationField $field, $sparse = false) {
915 80
        $properties = $field->val('properties', []);
916 80
        $required = array_flip($field->val('required', []));
917
918 80
        if (is_array($data)) {
919 76
            $keys = array_keys($data);
920 76
            $clean = [];
921
        } else {
922 4
            $keys = array_keys(iterator_to_array($data));
0 ignored issues
show
Bug introduced by
$data of type ArrayAccess is incompatible with the type Traversable expected by parameter $iterator of iterator_to_array(). ( Ignorable by Annotation )

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

922
            $keys = array_keys(iterator_to_array(/** @scrutinizer ignore-type */ $data));
Loading history...
923 4
            $class = get_class($data);
924 4
            $clean = new $class;
925
926 4
            if ($clean instanceof \ArrayObject) {
927 3
                $clean->setFlags($data->getFlags());
0 ignored issues
show
Bug introduced by
The method getFlags() does not exist on ArrayAccess. It seems like you code against a sub-type of ArrayAccess such as Phar or CachingIterator or Garden\Schema\Schema or ArrayObject or ArrayIterator. ( Ignorable by Annotation )

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

927
                $clean->setFlags($data->/** @scrutinizer ignore-call */ getFlags());
Loading history...
928 3
                $clean->setIteratorClass($data->getIteratorClass());
0 ignored issues
show
Bug introduced by
The method getIteratorClass() does not exist on ArrayAccess. It seems like you code against a sub-type of ArrayAccess such as ArrayObject. ( Ignorable by Annotation )

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

928
                $clean->setIteratorClass($data->/** @scrutinizer ignore-call */ getIteratorClass());
Loading history...
929
            }
930
        }
931 80
        $keys = array_combine(array_map('strtolower', $keys), $keys);
932
933 80
        $propertyField = new ValidationField($field->getValidation(), [], null, $sparse);
934
935
        // Loop through the schema fields and validate each one.
936 80
        foreach ($properties as $propertyName => $property) {
937
            $propertyField
938 80
                ->setField($property)
939 80
                ->setName(ltrim($field->getName().".$propertyName", '.'));
940
941 80
            $lName = strtolower($propertyName);
942 80
            $isRequired = isset($required[$propertyName]);
943
944
            // First check for required fields.
945 80
            if (!array_key_exists($lName, $keys)) {
946 19
                if ($sparse) {
947
                    // Sparse validation can leave required fields out.
948 18
                } elseif ($propertyField->hasVal('default')) {
949 2
                    $clean[$propertyName] = $propertyField->val('default');
950 16
                } elseif ($isRequired) {
951 19
                    $propertyField->addError('missingField', ['messageCode' => '{field} is required.']);
952
                }
953
            } else {
954 77
                $value = $data[$keys[$lName]];
955
956 77
                if (in_array($value, [null, ''], true) && !$isRequired && !$propertyField->hasType('null')) {
957 5
                    if ($propertyField->getType() !== 'string' || $value === null) {
958 2
                        continue;
959
                    }
960
                }
961
962 75
                $clean[$propertyName] = $this->validateField($value, $propertyField, $sparse);
963
            }
964
965 78
            unset($keys[$lName]);
966
        }
967
968
        // Look for extraneous properties.
969 80
        if (!empty($keys)) {
970 11
            if ($this->hasFlag(Schema::VALIDATE_EXTRA_PROPERTY_NOTICE)) {
971 2
                $msg = sprintf("%s has unexpected field(s): %s.", $field->getName() ?: 'value', implode(', ', $keys));
972 2
                trigger_error($msg, E_USER_NOTICE);
973
            }
974
975 9
            if ($this->hasFlag(Schema::VALIDATE_EXTRA_PROPERTY_EXCEPTION)) {
976 2
                $field->addError('invalid', [
977 2
                    'messageCode' => '{field} has {extra,plural,an unexpected field,unexpected fields}: {extra}.',
978 2
                    'extra' => array_values($keys),
979 2
                    'status' => 422
980
                ]);
981
            }
982
        }
983
984 78
        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...
985
    }
986
987
    /**
988
     * Validate a string.
989
     *
990
     * @param mixed $value The value to validate.
991
     * @param ValidationField $field The validation results to add.
992
     * @return string|Invalid Returns the valid string or **null** if validation fails.
993
     */
994 63
    protected function validateString($value, ValidationField $field) {
995 63
        if (is_string($value) || is_numeric($value)) {
996 61
            $value = $result = (string)$value;
997
        } else {
998 4
            $field->addTypeError('string');
999 4
            return Invalid::value();
1000
        }
1001
1002 61
        $errorCount = $field->getErrorCount();
0 ignored issues
show
Unused Code introduced by
The assignment to $errorCount is dead and can be removed.
Loading history...
1003 61
        if (($minLength = $field->val('minLength', 0)) > 0 && mb_strlen($value) < $minLength) {
1004 3
            if (!empty($field->getName()) && $minLength === 1) {
1005 1
                $field->addError('missingField', ['messageCode' => '{field} is required.', 'status' => 422]);
1006
            } else {
1007 2
                $field->addError(
1008 2
                    'minLength',
1009
                    [
1010 2
                        'messageCode' => '{field} should be at least {minLength} {minLength,plural,character} long.',
1011 2
                        'minLength' => $minLength,
1012 2
                        'status' => 422
1013
                    ]
1014
                );
1015
            }
1016
        }
1017 61
        if (($maxLength = $field->val('maxLength', 0)) > 0 && mb_strlen($value) > $maxLength) {
1018 1
            $field->addError(
1019 1
                'maxLength',
1020
                [
1021 1
                    'messageCode' => '{field} is {overflow} {overflow,plural,characters} too long.',
1022 1
                    'maxLength' => $maxLength,
1023 1
                    'overflow' => mb_strlen($value) - $maxLength,
1024 1
                    'status' => 422
1025
                ]
1026
            );
1027
        }
1028 61
        if ($pattern = $field->val('pattern')) {
1029 4
            $regex = '`'.str_replace('`', preg_quote('`', '`'), $pattern).'`';
1030
1031 4
            if (!preg_match($regex, $value)) {
1032 2
                $field->addError(
1033 2
                    'invalid',
1034
                    [
1035 2
                        'messageCode' => '{field} is in the incorrect format.',
1036
                        'status' => 422
1037
                    ]
1038
                );
1039
            }
1040
        }
1041 61
        if ($format = $field->val('format')) {
1042 15
            $type = $format;
1043
            switch ($format) {
1044 15
                case 'date-time':
1045 4
                    $result = $this->validateDatetime($result, $field);
1046 4
                    if ($result instanceof \DateTimeInterface) {
1047 4
                        $result = $result->format(\DateTime::RFC3339);
1048
                    }
1049 4
                    break;
1050 11
                case 'email':
1051 1
                    $result = filter_var($result, FILTER_VALIDATE_EMAIL);
1052 1
                    break;
1053 10
                case 'ipv4':
1054 1
                    $type = 'IPv4 address';
1055 1
                    $result = filter_var($result, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4);
1056 1
                    break;
1057 9
                case 'ipv6':
1058 1
                    $type = 'IPv6 address';
1059 1
                    $result = filter_var($result, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6);
1060 1
                    break;
1061 8
                case 'ip':
1062 1
                    $type = 'IP address';
1063 1
                    $result = filter_var($result, FILTER_VALIDATE_IP);
1064 1
                    break;
1065 7
                case 'uri':
1066 7
                    $type = 'URI';
1067 7
                    $result = filter_var($result, FILTER_VALIDATE_URL, FILTER_FLAG_HOST_REQUIRED | FILTER_FLAG_SCHEME_REQUIRED);
1068 7
                    break;
1069
                default:
1070
                    trigger_error("Unrecognized format '$format'.", E_USER_NOTICE);
1071
            }
1072 15
            if ($result === false) {
1073 5
                $field->addTypeError($type);
1074
            }
1075
        }
1076
1077 61
        if ($field->isValid()) {
1078 54
            return $result;
1079
        } else {
1080 11
            return Invalid::value();
1081
        }
1082
    }
1083
1084
    /**
1085
     * Validate a unix timestamp.
1086
     *
1087
     * @param mixed $value The value to validate.
1088
     * @param ValidationField $field The field being validated.
1089
     * @return int|Invalid Returns a valid timestamp or invalid if the value doesn't validate.
1090
     */
1091 5
    protected function validateTimestamp($value, ValidationField $field) {
1092 5
        if (is_numeric($value) && $value > 0) {
1093 1
            $result = (int)$value;
1094 4
        } elseif (is_string($value) && $ts = strtotime($value)) {
1095 1
            $result = $ts;
1096
        } else {
1097 3
            $field->addTypeError('timestamp');
1098 3
            $result = Invalid::value();
1099
        }
1100 5
        return $result;
1101
    }
1102
1103
    /**
1104
     * Validate a null value.
1105
     *
1106
     * @param mixed $value The value to validate.
1107
     * @param ValidationField $field The error collector for the field.
1108
     * @return null|Invalid Returns **null** or invalid.
1109
     */
1110 1
    protected function validateNull($value, ValidationField $field) {
1111 1
        if ($value === null) {
1112
            return null;
1113
        }
1114 1
        $field->addError('invalid', ['messageCode' => '{field} should be null.', 'status' => 422]);
1115 1
        return Invalid::value();
1116
    }
1117
1118
    /**
1119
     * Validate a value against an enum.
1120
     *
1121
     * @param mixed $value The value to test.
1122
     * @param ValidationField $field The validation object for adding errors.
1123
     * @return mixed|Invalid Returns the value if it is one of the enumerated values or invalid otherwise.
1124
     */
1125 142
    protected function validateEnum($value, ValidationField $field) {
1126 142
        $enum = $field->val('enum');
1127 142
        if (empty($enum)) {
1128 141
            return $value;
1129
        }
1130
1131 1
        if (!in_array($value, $enum, true)) {
1132 1
            $field->addError(
1133 1
                'invalid',
1134
                [
1135 1
                    'messageCode' => '{field} must be one of: {enum}.',
1136 1
                    'enum' => $enum,
1137 1
                    'status' => 422
1138
                ]
1139
            );
1140 1
            return Invalid::value();
1141
        }
1142 1
        return $value;
1143
    }
1144
1145
    /**
1146
     * Call all of the filters attached to a field.
1147
     *
1148
     * @param mixed $value The field value being filtered.
1149
     * @param ValidationField $field The validation object.
1150
     * @return mixed Returns the filtered value. If there are no filters for the field then the original value is returned.
1151
     */
1152 145
    protected function callFilters($value, ValidationField $field) {
1153
        // Strip array references in the name except for the last one.
1154 145
        $key = preg_replace(['`\[\d+\]$`', '`\[\d+\]`'], ['[]', ''], $field->getName());
1155 145
        if (!empty($this->filters[$key])) {
1156 1
            foreach ($this->filters[$key] as $filter) {
1157 1
                $value = call_user_func($filter, $value, $field);
1158
            }
1159
        }
1160 145
        return $value;
1161
    }
1162
1163
    /**
1164
     * Call all of the validators attached to a field.
1165
     *
1166
     * @param mixed $value The field value being validated.
1167
     * @param ValidationField $field The validation object to add errors.
1168
     */
1169 143
    protected function callValidators($value, ValidationField $field) {
1170 143
        $valid = true;
1171
1172
        // Strip array references in the name except for the last one.
1173 143
        $key = preg_replace(['`\[\d+\]$`', '`\[\d+\]`'], ['[]', ''], $field->getName());
1174 143
        if (!empty($this->validators[$key])) {
1175 4
            foreach ($this->validators[$key] as $validator) {
1176 4
                $r = call_user_func($validator, $value, $field);
1177
1178 4
                if ($r === false || Invalid::isInvalid($r)) {
1179 4
                    $valid = false;
1180
                }
1181
            }
1182
        }
1183
1184
        // Add an error on the field if the validator hasn't done so.
1185 143
        if (!$valid && $field->isValid()) {
1186
            $field->addError('invalid', ['messageCode' => '{field} is invalid.', 'status' => 422]);
1187
        }
1188 143
    }
1189
1190
    /**
1191
     * Specify data which should be serialized to JSON.
1192
     *
1193
     * This method specifically returns data compatible with the JSON schema format.
1194
     *
1195
     * @return mixed Returns data which can be serialized by **json_encode()**, which is a value of any type other than a resource.
1196
     * @link http://php.net/manual/en/jsonserializable.jsonserialize.php
1197
     * @link http://json-schema.org/
1198
     */
1199
    public function jsonSerialize() {
1200 16
        $fix = function ($schema) use (&$fix) {
1201 16
            if ($schema instanceof Schema) {
1202 1
                return $schema->jsonSerialize();
1203
            }
1204
1205 16
            if (!empty($schema['type'])) {
1206 15
                $types = (array)$schema['type'];
1207
1208 15
                foreach ($types as $i => &$type) {
1209
                    // Swap datetime and timestamp to other types with formats.
1210 15
                    if ($type === 'datetime') {
1211 5
                        $type = 'string';
1212 5
                        $schema['format'] = 'date-time';
1213 14
                    } elseif ($schema['type'] === 'timestamp') {
1214 3
                        $type = 'integer';
1215 15
                        $schema['format'] = 'timestamp';
1216
                    }
1217
                }
1218 15
                $types = array_unique($types);
1219 15
                $schema['type'] = count($types) === 1 ? reset($types) : $types;
1220
            }
1221
1222 16
            if (!empty($schema['items'])) {
1223 4
                $schema['items'] = $fix($schema['items']);
1224
            }
1225 16
            if (!empty($schema['properties'])) {
1226 11
                $properties = [];
1227 11
                foreach ($schema['properties'] as $key => $property) {
1228 11
                    $properties[$key] = $fix($property);
1229
                }
1230 11
                $schema['properties'] = $properties;
1231
            }
1232
1233 16
            return $schema;
1234 16
        };
1235
1236 16
        $result = $fix($this->schema);
1237
1238 16
        return $result;
1239
    }
1240
1241
    /**
1242
     * Look up a type based on its alias.
1243
     *
1244
     * @param string $alias The type alias or type name to lookup.
1245
     * @return mixed
1246
     */
1247 134
    protected function getType($alias) {
1248 134
        if (isset(self::$types[$alias])) {
1249
            return $alias;
1250
        }
1251 134
        foreach (self::$types as $type => $aliases) {
1252 134
            if (in_array($alias, $aliases, true)) {
1253 134
                return $type;
1254
            }
1255
        }
1256 6
        return null;
1257
    }
1258
1259
    /**
1260
     * Get the class that's used to contain validation information.
1261
     *
1262
     * @return Validation|string Returns the validation class.
1263
     */
1264 145
    public function getValidationClass() {
1265 145
        return $this->validationClass;
1266
    }
1267
1268
    /**
1269
     * Set the class that's used to contain validation information.
1270
     *
1271
     * @param Validation|string $class Either the name of a class or a class that will be cloned.
1272
     * @return $this
1273
     */
1274 1
    public function setValidationClass($class) {
1275 1
        if (!is_a($class, Validation::class, true)) {
1276
            throw new \InvalidArgumentException("$class must be a subclass of ".Validation::class, 500);
1277
        }
1278
1279 1
        $this->validationClass = $class;
1280 1
        return $this;
1281
    }
1282
1283
    /**
1284
     * Create a new validation instance.
1285
     *
1286
     * @return Validation Returns a validation object.
1287
     */
1288 145
    protected function createValidation() {
1289 145
        $class = $this->getValidationClass();
1290
1291 145
        if ($class instanceof Validation) {
1292 1
            $result = clone $class;
1293
        } else {
1294 145
            $result = new $class;
1295
        }
1296 145
        return $result;
1297
    }
1298
1299
    /**
1300
     * Check whether or not a value is an array or accessible like an array.
1301
     *
1302
     * @param mixed $value The value to check.
1303
     * @return bool Returns **true** if the value can be used like an array or **false** otherwise.
1304
     */
1305 83
    private function isArray($value) {
1306 83
        return is_array($value) || ($value instanceof \ArrayAccess && $value instanceof \Traversable);
1307
    }
1308
1309
    /**
1310
     * Cast a value to an array.
1311
     *
1312
     * @param \Traversable $value The value to convert.
1313
     * @return array Returns an array.
1314
     */
1315 3
    private function toObjectArray(\Traversable $value) {
1316 3
        $class = get_class($value);
1317 3
        if ($value instanceof \ArrayObject) {
1318 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...
1319 1
        } elseif ($value instanceof \ArrayAccess) {
1320 1
            $r = new $class;
1321 1
            foreach ($value as $k => $v) {
1322 1
                $r[$k] = $v;
1323
            }
1324 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...
1325
        }
1326
        return iterator_to_array($value);
1327
    }
1328
1329
    /**
1330
     * Return a sparse version of this schema.
1331
     *
1332
     * A sparse schema has no required properties.
1333
     *
1334
     * @return Schema Returns a new sparse schema.
1335
     */
1336 2
    public function withSparse() {
1337 2
        $sparseSchema = $this->withSparseInternal($this, new \SplObjectStorage());
1338 2
        return $sparseSchema;
1339
    }
1340
1341
    /**
1342
     * The internal implementation of `Schema::withSparse()`.
1343
     *
1344
     * @param array|Schema $schema The schema to make sparse.
1345
     * @param \SplObjectStorage $schemas Collected sparse schemas that have already been made.
1346
     * @return mixed
1347
     */
1348 2
    private function withSparseInternal($schema, \SplObjectStorage $schemas) {
1349 2
        if ($schema instanceof Schema) {
1350 2
            if ($schemas->contains($schema)) {
1351 1
                return $schemas[$schema];
1352
            } else {
1353 2
                $schemas[$schema] = $sparseSchema = new Schema();
1354 2
                $sparseSchema->schema = $schema->withSparseInternal($schema->schema, $schemas);
1355 2
                if ($id = $sparseSchema->getID()) {
1356
                    $sparseSchema->setID($id.'Sparse');
1357
                }
1358
1359 2
                return $sparseSchema;
1360
            }
1361
        }
1362
1363 2
        unset($schema['required']);
1364
1365 2
        if (isset($schema['items'])) {
1366 1
            $schema['items'] = $this->withSparseInternal($schema['items'], $schemas);
1367
        }
1368 2
        if (isset($schema['properties'])) {
1369 2
            foreach ($schema['properties'] as $name => &$property) {
1370 2
                $property = $this->withSparseInternal($property, $schemas);
1371
            }
1372
        }
1373
1374 2
        return $schema;
1375
    }
1376
1377
    /**
1378
     * Filter a field's value using built in and custom filters.
1379
     *
1380
     * @param mixed $value The original value of the field.
1381
     * @param ValidationField $field The field information for the field.
1382
     * @return mixed Returns the filtered field or the original field value if there are no filters.
1383
     */
1384 145
    private function filterField($value, ValidationField $field) {
1385
        // Check for limited support for Open API style.
1386 145
        if (!empty($field->val('style')) && is_string($value)) {
1387 8
            $doFilter = true;
1388 8
            if ($field->hasType('boolean') && in_array($value, ['true', 'false', '0', '1'], true)) {
1389 4
                $doFilter = false;
1390 4
            } elseif ($field->hasType('integer') || $field->hasType('number') && is_numeric($value)) {
1391
                $doFilter = false;
1392
            }
1393
1394 8
            if ($doFilter) {
1395 4
                switch ($field->val('style')) {
1396 4
                    case 'form':
1397 2
                        $value = explode(',', $value);
1398 2
                        break;
1399 2
                    case 'spaceDelimited':
1400 1
                        $value = explode(' ', $value);
1401 1
                        break;
1402 1
                    case 'pipeDelimited':
1403 1
                        $value = explode('|', $value);
1404 1
                        break;
1405
                }
1406
            }
1407
        }
1408
1409 145
        $value = $this->callFilters($value, $field);
1410
1411 145
        return $value;
1412
    }
1413
1414
    /**
1415
     * Whether a offset exists.
1416
     *
1417
     * @param mixed $offset An offset to check for.
1418
     * @return boolean true on success or false on failure.
1419
     * @link http://php.net/manual/en/arrayaccess.offsetexists.php
1420
     */
1421 6
    public function offsetExists($offset) {
1422 6
        return isset($this->schema[$offset]);
1423
    }
1424
1425
    /**
1426
     * Offset to retrieve.
1427
     *
1428
     * @param mixed $offset The offset to retrieve.
1429
     * @return mixed Can return all value types.
1430
     * @link http://php.net/manual/en/arrayaccess.offsetget.php
1431
     */
1432 2
    public function offsetGet($offset) {
1433 2
        return isset($this->schema[$offset]) ? $this->schema[$offset] : null;
1434
    }
1435
1436
    /**
1437
     * Offset to set.
1438
     *
1439
     * @param mixed $offset The offset to assign the value to.
1440
     * @param mixed $value The value to set.
1441
     * @link http://php.net/manual/en/arrayaccess.offsetset.php
1442
     */
1443 1
    public function offsetSet($offset, $value) {
1444 1
        $this->schema[$offset] = $value;
1445 1
    }
1446
1447
    /**
1448
     * Offset to unset.
1449
     *
1450
     * @param mixed $offset The offset to unset.
1451
     * @link http://php.net/manual/en/arrayaccess.offsetunset.php
1452
     */
1453 1
    public function offsetUnset($offset) {
1454 1
        unset($this->schema[$offset]);
1455 1
    }
1456
1457
    /**
1458
     * Validate a field against a single type.
1459
     *
1460
     * @param mixed $value The value to validate.
1461
     * @param string $type The type to validate against.
1462
     * @param ValidationField $field Contains field and validation information.
1463
     * @param bool $sparse Whether or not this should be a sparse validation.
1464
     * @return mixed Returns the valid value or `Invalid`.
1465
     */
1466 144
    protected function validateSingleType($value, $type, ValidationField $field, $sparse) {
1467
        switch ($type) {
1468 144
            case 'boolean':
1469 28
                $result = $this->validateBoolean($value, $field);
1470 28
                break;
1471 125
            case 'integer':
1472 35
                $result = $this->validateInteger($value, $field);
1473 35
                break;
1474 120
            case 'number':
1475 10
                $result = $this->validateNumber($value, $field);
1476 10
                break;
1477 116
            case 'string':
1478 63
                $result = $this->validateString($value, $field);
1479 63
                break;
1480 96
            case 'timestamp':
1481 5
                $result = $this->validateTimestamp($value, $field);
1482 5
                break;
1483 96
            case 'datetime':
1484 7
                $result = $this->validateDatetime($value, $field);
1485 7
                break;
1486 93
            case 'array':
1487 20
                $result = $this->validateArray($value, $field, $sparse);
1488 20
                break;
1489 84
            case 'object':
1490 83
                $result = $this->validateObject($value, $field, $sparse);
1491 81
                break;
1492 3
            case 'null':
1493 1
                $result = $this->validateNull($value, $field);
1494 1
                break;
1495 2
            case null:
1496
                // No type was specified so we are valid.
1497 2
                $result = $value;
1498 2
                break;
1499
            default:
1500
                throw new \InvalidArgumentException("Unrecognized type $type.", 500);
1501
        }
1502 144
        return $result;
1503
    }
1504
1505
    /**
1506
     * Validate a field against multiple basic types.
1507
     *
1508
     * The first validation that passes will be returned. If no type can be validated against then validation will fail.
1509
     *
1510
     * @param mixed $value The value to validate.
1511
     * @param string[] $types The types to validate against.
1512
     * @param ValidationField $field Contains field and validation information.
1513
     * @param bool $sparse Whether or not this should be a sparse validation.
1514
     * @return mixed Returns the valid value or `Invalid`.
1515
     */
1516 24
    private function validateMultipleTypes($value, array $types, ValidationField $field, $sparse) {
1517
        // First check for an exact type match.
1518 24
        switch (gettype($value)) {
1519 24
            case 'boolean':
1520 3
                if (in_array('boolean', $types)) {
1521 3
                    $singleType = 'boolean';
1522
                }
1523 3
                break;
1524 21
            case 'integer':
1525 5
                if (in_array('integer', $types)) {
1526 4
                    $singleType = 'integer';
1527 1
                } elseif (in_array('number', $types)) {
1528 1
                    $singleType = 'number';
1529
                }
1530 5
                break;
1531 16
            case 'double':
1532 3
                if (in_array('number', $types)) {
1533 3
                    $singleType = 'number';
1534
                } elseif (in_array('integer', $types)) {
1535
                    $singleType = 'integer';
1536
                }
1537 3
                break;
1538 13
            case 'string':
1539 10
                if (in_array('datetime', $types) && preg_match(self::$DATE_REGEX, $value)) {
1540 1
                    $singleType = 'datetime';
1541 9
                } elseif (in_array('string', $types)) {
1542 5
                    $singleType = 'string';
1543
                }
1544 10
                break;
1545 3
            case 'array':
1546 3
                if (in_array('array', $types) && in_array('object', $types)) {
1547
                    $singleType = isset($value[0]) || empty($value) ? 'array' : 'object';
1548 3
                } elseif (in_array('object', $types)) {
1549
                    $singleType = 'object';
1550 3
                } elseif (in_array('array', $types)) {
1551 3
                    $singleType = 'array';
1552
                }
1553 3
                break;
1554
            case 'NULL':
1555
                if (in_array('null', $types)) {
1556
                    $singleType = $this->validateSingleType($value, 'null', $field, $sparse);
1557
                }
1558
                break;
1559
        }
1560 24
        if (!empty($singleType)) {
1561 20
            return $this->validateSingleType($value, $singleType, $field, $sparse);
1562
        }
1563
1564
        // Clone the validation field to collect errors.
1565 4
        $typeValidation = new ValidationField(new Validation(), $field->getField(), '', $sparse);
1566
1567
        // Try and validate against each type.
1568 4
        foreach ($types as $type) {
1569 4
            $result = $this->validateSingleType($value, $type, $typeValidation, $sparse);
1570 4
            if (Invalid::isValid($result)) {
1571 4
                return $result;
1572
            }
1573
        }
1574
1575
        // Since we got here the value is invalid.
1576
        $field->merge($typeValidation->getValidation());
1577
        return Invalid::value();
1578
    }
1579
}
1580