Completed
Pull Request — master (#44)
by Todd
02:07
created

Schema::validateDatetime()   B

Complexity

Conditions 11
Paths 14

Size

Total Lines 28
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 11.0245

Importance

Changes 0
Metric Value
cc 11
eloc 20
nc 14
nop 2
dl 0
loc 28
ccs 16
cts 17
cp 0.9412
crap 11.0245
rs 7.3166
c 0
b 0
f 0

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

349
        $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...
350 173
        $schema->schema = $schema->parseInternal($arr);
351 172
        return $schema;
352
    }
353
354
    /**
355
     * Parse a schema in short form into a full schema array.
356
     *
357
     * @param array $arr The array to parse into a schema.
358
     * @return array The full schema array.
359
     * @throws \InvalidArgumentException Throws an exception when an item in the schema is invalid.
360
     */
361 173
    protected function parseInternal(array $arr): array {
362 173
        if (empty($arr)) {
363
            // An empty schema validates to anything.
364 6
            return [];
365 168
        } elseif (isset($arr['type'])) {
366
            // This is a long form schema and can be parsed as the root.
367
            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...
368
        } else {
369
            // Check for a root schema.
370 168
            $value = reset($arr);
371 168
            $key = key($arr);
372 168
            if (is_int($key)) {
373 105
                $key = $value;
374 105
                $value = null;
375
            }
376 168
            list ($name, $param) = $this->parseShortParam($key, $value);
377 167
            if (empty($name)) {
378 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...
379
            }
380
        }
381
382
        // If we are here then this is n object schema.
383 108
        list($properties, $required) = $this->parseProperties($arr);
384
385
        $result = [
386 108
            'type' => 'object',
387 108
            'properties' => $properties,
388 108
            'required' => $required
389
        ];
390
391 108
        return array_filter($result);
392
    }
393
394
    /**
395
     * Parse a schema node.
396
     *
397
     * @param array $node The node to parse.
398
     * @param mixed $value Additional information from the node.
399
     * @return array|\ArrayAccess Returns a JSON schema compatible node.
400
     */
401 167
    private function parseNode($node, $value = null) {
402 167
        if (is_array($value)) {
403 64
            if (is_array($node['type'])) {
404
                trigger_error('Schemas with multiple types is deprecated.', E_USER_DEPRECATED);
405
            }
406
407
            // The value describes a bit more about the schema.
408 64
            switch ($node['type']) {
409 64
                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 54
                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 42
                    $node = array_replace($node, $value);
430 64
                    break;
431
            }
432 128
        } elseif (is_string($value)) {
433 101
            if ($node['type'] === 'array' && $arrType = $this->getType($value)) {
434 6
                $node['items'] = ['type' => $arrType];
435 97
            } elseif (!empty($value)) {
436 101
                $node['description'] = $value;
437
            }
438 32
        } elseif ($value === null) {
439
            // Parse child elements.
440 28
            if ($node['type'] === 'array' && isset($node['items'])) {
441
                // The value includes array schema information.
442
                $node['items'] = $this->parseInternal($node['items']);
443 28
            } elseif ($node['type'] === 'object' && isset($node['properties'])) {
444
                list($node['properties']) = $this->parseProperties($node['properties']);
445
446
            }
447
        }
448
449 167
        if (is_array($node)) {
450 166
            if (!empty($node['allowNull'])) {
451 1
                $node['nullable'] = true;
452
            }
453 166
            unset($node['allowNull']);
454
455 166
            if ($node['type'] === null || $node['type'] === []) {
456 4
                unset($node['type']);
457
            }
458
        }
459
460 167
        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 108
    private function parseProperties(array $arr): array {
470 108
        $properties = [];
471 108
        $requiredProperties = [];
472 108
        foreach ($arr as $key => $value) {
473
            // Fix a schema specified as just a value.
474 108
            if (is_int($key)) {
475 81
                if (is_string($value)) {
476 81
                    $key = $value;
477 81
                    $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 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

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

775
            if ((null !== $minItems = $field->val('minItems')) && count(/** @scrutinizer ignore-type */ $value) < $minItems) {
Loading history...
776 1
                $field->addError(
777 1
                    'minItems',
778
                    [
779 1
                        'messageCode' => '{field} must contain at least {minItems} {minItems,plural,item}.',
780 1
                        'minItems' => $minItems,
781 1
                        'status' => 422
782
                    ]
783
                );
784
            }
785 24
            if ((null !== $maxItems = $field->val('maxItems')) && count($value) > $maxItems) {
786 1
                $field->addError(
787 1
                    'maxItems',
788
                    [
789 1
                        'messageCode' => '{field} must contain no more than {maxItems} {maxItems,plural,item}.',
790 1
                        'maxItems' => $maxItems,
791 1
                        'status' => 422
792
                    ]
793
                );
794
            }
795
796 24
            if ($field->val('items') !== null) {
797 19
                $result = [];
798
799
                // Validate each of the types.
800 19
                $itemValidation = new ValidationField(
801 19
                    $field->getValidation(),
802 19
                    $field->val('items'),
803 19
                    '',
804 19
                    $field->getOptions()
805
                );
806
807 19
                $count = 0;
808 19
                foreach ($value as $i => $item) {
809 19
                    $itemValidation->setName($field->getName()."[{$i}]");
810 19
                    $validItem = $this->validateField($item, $itemValidation);
811 19
                    if (Invalid::isValid($validItem)) {
812 19
                        $result[] = $validItem;
813
                    }
814 19
                    $count++;
815
                }
816
817 19
                return empty($result) && $count > 0 ? Invalid::value() : $result;
818
            } else {
819
                // Cast the items into a proper numeric array.
820 5
                $result = is_array($value) ? array_values($value) : iterator_to_array($value);
821 5
                return $result;
822
            }
823
        }
824
    }
825
826
    /**
827
     * Validate a boolean value.
828
     *
829
     * @param mixed $value The value to validate.
830
     * @param ValidationField $field The validation results to add.
831
     * @return bool|Invalid Returns the cleaned value or invalid if validation fails.
832
     */
833 30
    protected function validateBoolean($value, ValidationField $field) {
834 30
        $value = $value === null ? $value : filter_var($value, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE);
835 30
        if ($value === null) {
836 4
            $field->addTypeError('boolean');
837 4
            return Invalid::value();
838
        }
839
840 27
        return $value;
841
    }
842
843
    /**
844
     * Validate a date time.
845
     *
846
     * @param mixed $value The value to validate.
847
     * @param ValidationField $field The validation results to add.
848
     * @return \DateTimeInterface|Invalid Returns the cleaned value or **null** if it isn't valid.
849
     */
850 14
    protected function validateDatetime($value, ValidationField $field) {
851 14
        if ($value instanceof \DateTimeInterface) {
852
            // do nothing, we're good
853 11
        } elseif (is_string($value) && $value !== '' && !is_numeric($value)) {
854
            try {
855 7
                $dt = new \DateTimeImmutable($value);
856 6
                if ($dt) {
0 ignored issues
show
introduced by
$dt is of type DateTimeImmutable, thus it always evaluated to true.
Loading history...
857 6
                    $value = $dt;
858
                } else {
859 6
                    $value = null;
860
                }
861 1
            } catch (\Throwable $ex) {
862 7
                $value = Invalid::value();
863
            }
864 4
        } elseif (is_int($value) && $value > 0) {
865
            try {
866 1
                $value = new \DateTimeImmutable('@'.(string)round($value));
867
            } catch (\Throwable $ex) {
868 1
                $value = Invalid::value();
869
            }
870
        } else {
871 3
            $value = Invalid::value();
872
        }
873
874 14
        if (Invalid::isInvalid($value)) {
875 4
            $field->addTypeError('datetime');
876
        }
877 14
        return $value;
878
    }
879
880
    /**
881
     * Validate a float.
882
     *
883
     * @param mixed $value The value to validate.
884
     * @param ValidationField $field The validation results to add.
885
     * @return float|Invalid Returns a number or **null** if validation fails.
886
     */
887 13
    protected function validateNumber($value, ValidationField $field) {
888 13
        $result = filter_var($value, FILTER_VALIDATE_FLOAT);
889 13
        if ($result === false) {
890 4
            $field->addTypeError('number');
891 4
            return Invalid::value();
892
        }
893 9
        return $result;
894
    }
895
    /**
896
     * Validate and integer.
897
     *
898
     * @param mixed $value The value to validate.
899
     * @param ValidationField $field The validation results to add.
900
     * @return int|Invalid Returns the cleaned value or **null** if validation fails.
901
     */
902 43
    protected function validateInteger($value, ValidationField $field) {
903 43
        if ($field->val('format') === 'timestamp') {
904 7
            return $this->validateTimestamp($value, $field);
905
        }
906
907 38
        $result = filter_var($value, FILTER_VALIDATE_INT);
908
909 38
        if ($result === false) {
910 9
            $field->addTypeError('integer');
911 9
            return Invalid::value();
912
        }
913 33
        return $result;
914
    }
915
916
    /**
917
     * Validate an object.
918
     *
919
     * @param mixed $value The value to validate.
920
     * @param ValidationField $field The validation results to add.
921
     * @return object|Invalid Returns a clean object or **null** if validation fails.
922
     */
923 99
    protected function validateObject($value, ValidationField $field) {
924 99
        if (!$this->isArray($value) || isset($value[0])) {
925 6
            $field->addTypeError('object');
926 6
            return Invalid::value();
927 99
        } elseif (is_array($field->val('properties'))) {
928
            // Validate the data against the internal schema.
929 94
            $value = $this->validateProperties($value, $field);
930 5
        } elseif (!is_array($value)) {
931 3
            $value = $this->toObjectArray($value);
932
        }
933 97
        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...
934
    }
935
936
    /**
937
     * Validate data against the schema and return the result.
938
     *
939
     * @param array|\Traversable&\ArrayAccess $data The data to validate.
940
     * @param ValidationField $field This argument will be filled with the validation result.
941
     * @return array|Invalid Returns a clean array with only the appropriate properties and the data coerced to proper types.
942
     * or invalid if there are no valid properties.
943
     */
944 94
    protected function validateProperties($data, ValidationField $field) {
945 94
        $properties = $field->val('properties', []);
946 94
        $required = array_flip($field->val('required', []));
947
948 94
        if (is_array($data)) {
0 ignored issues
show
introduced by
The condition is_array($data) is always false.
Loading history...
949 90
            $keys = array_keys($data);
950 90
            $clean = [];
951
        } else {
952 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

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