Passed
Push — master ( 009802...5d11c8 )
by Todd
29s
created

Schema   F

Complexity

Total Complexity 322

Size/Duplication

Total Lines 1595
Duplicated Lines 0 %

Test Coverage

Coverage 95.14%

Importance

Changes 0
Metric Value
dl 0
loc 1595
ccs 646
cts 679
cp 0.9514
rs 0.8
c 0
b 0
f 0
wmc 322

57 Methods

Rating   Name   Duplication   Size   Complexity  
A createValidation() 0 10 2
A parseProperties() 0 25 5
A setDescription() 0 8 2
A validateBoolean() 0 8 3
A getFlags() 0 2 1
A setValidationClass() 0 7 2
A addValidator() 0 3 1
A setFlags() 0 7 2
A getDescription() 0 2 2
A validateNumber() 0 7 2
A setConcatMainMessage() 0 3 1
A validateEnum() 0 18 3
A callFilters() 0 9 3
A setID() 0 8 2
A isArray() 0 2 3
B withSparseInternal() 0 27 7
A addFilter() 0 3 1
D validateMultipleTypes() 0 62 25
B requireOneOf() 0 53 10
C filterField() 0 28 12
B jsonSerialize() 0 40 10
A validateTimestamp() 0 10 5
B validateSingleType() 0 37 11
A add() 0 3 1
A getField() 0 16 6
A getID() 0 2 2
A validateObject() 0 11 5
A isValid() 0 6 2
A setFlag() 0 7 2
B validateDatetime() 0 24 10
A validateInteger() 0 8 2
A offsetGet() 0 2 2
B validateField() 0 31 10
A withSparse() 0 3 1
D parseNode() 0 56 20
A merge() 0 3 1
D validateProperties() 0 76 18
A hasFlag() 0 2 1
A validate() 0 15 4
A offsetExists() 0 2 1
A parseInternal() 0 31 5
A offsetUnset() 0 2 1
C validateArray() 0 52 15
B setField() 0 22 6
F mergeInternal() 0 59 28
A toObjectArray() 0 12 4
A concatMainMessage() 0 2 1
D parseShortParam() 0 58 20
A getSchemaArray() 0 2 1
A offsetSet() 0 2 1
A getType() 0 10 4
A __construct() 0 2 1
F validateString() 0 87 21
A getValidationClass() 0 2 1
B callValidators() 0 18 7
A parse() 0 4 1
A validateNull() 0 6 2

How to fix   Complexity   

Complex Class

Complex classes like Schema often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Schema, and based on these observations, apply Extract Interface, too.

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

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

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

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

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

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

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

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

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

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

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

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