Passed
Push — master ( a9a521...b795e1 )
by Todd
36s
created

Schema::parseShortParam()   F

Complexity

Conditions 23
Paths 154

Size

Total Lines 68
Code Lines 43

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 35
CRAP Score 23.5713

Importance

Changes 0
Metric Value
cc 23
eloc 43
nc 154
nop 2
dl 0
loc 68
ccs 35
cts 39
cp 0.8974
crap 23.5713
rs 3.7166
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

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

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

Commonly applied refactorings include:

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

347
        $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...
348 173
        $schema->schema = $schema->parseInternal($arr);
349 172
        return $schema;
350
    }
351
352
    /**
353
     * Parse a schema in short form into a full schema array.
354
     *
355
     * @param array $arr The array to parse into a schema.
356
     * @return array The full schema array.
357
     * @throws \InvalidArgumentException Throws an exception when an item in the schema is invalid.
358
     */
359 173
    protected function parseInternal(array $arr): array {
360 173
        if (empty($arr)) {
361
            // An empty schema validates to anything.
362 6
            return [];
363 168
        } elseif (isset($arr['type'])) {
364
            // This is a long form schema and can be parsed as the root.
365
            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...
366
        } else {
367
            // Check for a root schema.
368 168
            $value = reset($arr);
369 168
            $key = key($arr);
370 168
            if (is_int($key)) {
371 105
                $key = $value;
372 105
                $value = null;
373
            }
374 168
            list ($name, $param) = $this->parseShortParam($key, $value);
375 167
            if (empty($name)) {
376 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...
Bug introduced by
It seems like $param can also be of type Garden\Schema\Schema; however, parameter $node of Garden\Schema\Schema::parseNode() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

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

376
                return $this->parseNode(/** @scrutinizer ignore-type */ $param, $value);
Loading history...
377
            }
378
        }
379
380
        // If we are here then this is n object schema.
381 108
        list($properties, $required) = $this->parseProperties($arr);
382
383
        $result = [
384 108
            'type' => 'object',
385 108
            'properties' => $properties,
386 108
            'required' => $required
387
        ];
388
389 108
        return array_filter($result);
390
    }
391
392
    /**
393
     * Parse a schema node.
394
     *
395
     * @param array $node The node to parse.
396
     * @param mixed $value Additional information from the node.
397
     * @return array|\ArrayAccess Returns a JSON schema compatible node.
398
     */
399 167
    private function parseNode($node, $value = null) {
400 167
        if (is_array($value)) {
401 59
            if (is_array($node['type'])) {
402
                trigger_error('Schemas with multiple types is deprecated.', E_USER_DEPRECATED);
403
            }
404
405
            // The value describes a bit more about the schema.
406 59
            switch ($node['type']) {
407 59
                case 'array':
408 11
                    if (isset($value['items'])) {
409
                        // The value includes array schema information.
410 4
                        $node = array_replace($node, $value);
411
                    } else {
412 7
                        $node['items'] = $this->parseInternal($value);
413
                    }
414 11
                    break;
415 49
                case 'object':
416
                    // The value is a schema of the object.
417 12
                    if (isset($value['properties'])) {
418
                        list($node['properties']) = $this->parseProperties($value['properties']);
419
                    } else {
420 12
                        list($node['properties'], $required) = $this->parseProperties($value);
421 12
                        if (!empty($required)) {
422 12
                            $node['required'] = $required;
423
                        }
424
                    }
425 12
                    break;
426
                default:
427 37
                    $node = array_replace($node, $value);
428 59
                    break;
429
            }
430 128
        } elseif (is_string($value)) {
431 101
            if ($node['type'] === 'array' && $arrType = $this->getType($value)) {
432 6
                $node['items'] = ['type' => $arrType];
433 97
            } elseif (!empty($value)) {
434 101
                $node['description'] = $value;
435
            }
436 32
        } elseif ($value === null) {
437
            // Parse child elements.
438 28
            if ($node['type'] === 'array' && isset($node['items'])) {
439
                // The value includes array schema information.
440
                $node['items'] = $this->parseInternal($node['items']);
441 28
            } elseif ($node['type'] === 'object' && isset($node['properties'])) {
442
                list($node['properties']) = $this->parseProperties($node['properties']);
443
444
            }
445
        }
446
447 167
        if (is_array($node)) {
448 166
            if (!empty($node['allowNull'])) {
449 1
                $node['nullable'] = true;
450
            }
451 166
            unset($node['allowNull']);
452
453 166
            if ($node['type'] === null || $node['type'] === []) {
454 4
                unset($node['type']);
455
            }
456
        }
457
458 167
        return $node;
459
    }
460
461
    /**
462
     * Parse the schema for an object's properties.
463
     *
464
     * @param array $arr An object property schema.
465
     * @return array Returns a schema array suitable to be placed in the **properties** key of a schema.
466
     */
467 108
    private function parseProperties(array $arr): array {
468 108
        $properties = [];
469 108
        $requiredProperties = [];
470 108
        foreach ($arr as $key => $value) {
471
            // Fix a schema specified as just a value.
472 108
            if (is_int($key)) {
473 81
                if (is_string($value)) {
474 81
                    $key = $value;
475 81
                    $value = '';
476
                } else {
477
                    throw new \InvalidArgumentException("Schema at position $key is not a valid parameter.", 500);
478
                }
479
            }
480
481
            // The parameter is defined in the key.
482 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

482
            list($name, $param, $required) = $this->parseShortParam($key, /** @scrutinizer ignore-type */ $value);
Loading history...
483
484 108
            $node = $this->parseNode($param, $value);
0 ignored issues
show
Bug introduced by
It seems like $param can also be of type string; however, parameter $node of Garden\Schema\Schema::parseNode() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

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

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

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

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