ex
Fork of mbed-os-example-mbed5-blinky by
dumi_doc-master/schema/vendor/justinrainbow/json-schema/src/JsonSchema/Constraints/CollectionConstraint.php@44:c1d8923072ba, 2017-07-18 (annotated)
- Committer:
- TMBOY
- Date:
- Tue Jul 18 16:27:22 2017 +0800
- Revision:
- 44:c1d8923072ba
?
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| TMBOY | 44:c1d8923072ba | 1 | <?php |
| TMBOY | 44:c1d8923072ba | 2 | |
| TMBOY | 44:c1d8923072ba | 3 | /* |
| TMBOY | 44:c1d8923072ba | 4 | * This file is part of the JsonSchema package. |
| TMBOY | 44:c1d8923072ba | 5 | * |
| TMBOY | 44:c1d8923072ba | 6 | * For the full copyright and license information, please view the LICENSE |
| TMBOY | 44:c1d8923072ba | 7 | * file that was distributed with this source code. |
| TMBOY | 44:c1d8923072ba | 8 | */ |
| TMBOY | 44:c1d8923072ba | 9 | |
| TMBOY | 44:c1d8923072ba | 10 | namespace JsonSchema\Constraints; |
| TMBOY | 44:c1d8923072ba | 11 | |
| TMBOY | 44:c1d8923072ba | 12 | use JsonSchema\Entity\JsonPointer; |
| TMBOY | 44:c1d8923072ba | 13 | |
| TMBOY | 44:c1d8923072ba | 14 | /** |
| TMBOY | 44:c1d8923072ba | 15 | * The CollectionConstraint Constraints, validates an array against a given schema |
| TMBOY | 44:c1d8923072ba | 16 | * |
| TMBOY | 44:c1d8923072ba | 17 | * @author Robert Schönthal <seroscho@googlemail.com> |
| TMBOY | 44:c1d8923072ba | 18 | * @author Bruno Prieto Reis <bruno.p.reis@gmail.com> |
| TMBOY | 44:c1d8923072ba | 19 | */ |
| TMBOY | 44:c1d8923072ba | 20 | class CollectionConstraint extends Constraint |
| TMBOY | 44:c1d8923072ba | 21 | { |
| TMBOY | 44:c1d8923072ba | 22 | /** |
| TMBOY | 44:c1d8923072ba | 23 | * {@inheritDoc} |
| TMBOY | 44:c1d8923072ba | 24 | */ |
| TMBOY | 44:c1d8923072ba | 25 | public function check($value, $schema = null, JsonPointer $path = null, $i = null) |
| TMBOY | 44:c1d8923072ba | 26 | { |
| TMBOY | 44:c1d8923072ba | 27 | // Verify minItems |
| TMBOY | 44:c1d8923072ba | 28 | if (isset($schema->minItems) && count($value) < $schema->minItems) { |
| TMBOY | 44:c1d8923072ba | 29 | $this->addError($path, "There must be a minimum of " . $schema->minItems . " items in the array", 'minItems', array('minItems' => $schema->minItems,)); |
| TMBOY | 44:c1d8923072ba | 30 | } |
| TMBOY | 44:c1d8923072ba | 31 | |
| TMBOY | 44:c1d8923072ba | 32 | // Verify maxItems |
| TMBOY | 44:c1d8923072ba | 33 | if (isset($schema->maxItems) && count($value) > $schema->maxItems) { |
| TMBOY | 44:c1d8923072ba | 34 | $this->addError($path, "There must be a maximum of " . $schema->maxItems . " items in the array", 'maxItems', array('maxItems' => $schema->maxItems,)); |
| TMBOY | 44:c1d8923072ba | 35 | } |
| TMBOY | 44:c1d8923072ba | 36 | |
| TMBOY | 44:c1d8923072ba | 37 | // Verify uniqueItems |
| TMBOY | 44:c1d8923072ba | 38 | if (isset($schema->uniqueItems) && $schema->uniqueItems) { |
| TMBOY | 44:c1d8923072ba | 39 | $unique = $value; |
| TMBOY | 44:c1d8923072ba | 40 | if (is_array($value) && count($value)) { |
| TMBOY | 44:c1d8923072ba | 41 | $unique = array_map(function($e) { return var_export($e, true); }, $value); |
| TMBOY | 44:c1d8923072ba | 42 | } |
| TMBOY | 44:c1d8923072ba | 43 | if (count(array_unique($unique)) != count($value)) { |
| TMBOY | 44:c1d8923072ba | 44 | $this->addError($path, "There are no duplicates allowed in the array", 'uniqueItems'); |
| TMBOY | 44:c1d8923072ba | 45 | } |
| TMBOY | 44:c1d8923072ba | 46 | } |
| TMBOY | 44:c1d8923072ba | 47 | |
| TMBOY | 44:c1d8923072ba | 48 | // Verify items |
| TMBOY | 44:c1d8923072ba | 49 | if (isset($schema->items)) { |
| TMBOY | 44:c1d8923072ba | 50 | $this->validateItems($value, $schema, $path, $i); |
| TMBOY | 44:c1d8923072ba | 51 | } |
| TMBOY | 44:c1d8923072ba | 52 | } |
| TMBOY | 44:c1d8923072ba | 53 | |
| TMBOY | 44:c1d8923072ba | 54 | /** |
| TMBOY | 44:c1d8923072ba | 55 | * Validates the items |
| TMBOY | 44:c1d8923072ba | 56 | * |
| TMBOY | 44:c1d8923072ba | 57 | * @param array $value |
| TMBOY | 44:c1d8923072ba | 58 | * @param \stdClass $schema |
| TMBOY | 44:c1d8923072ba | 59 | * @param JsonPointer|null $path |
| TMBOY | 44:c1d8923072ba | 60 | * @param string $i |
| TMBOY | 44:c1d8923072ba | 61 | */ |
| TMBOY | 44:c1d8923072ba | 62 | protected function validateItems($value, $schema = null, JsonPointer $path = null, $i = null) |
| TMBOY | 44:c1d8923072ba | 63 | { |
| TMBOY | 44:c1d8923072ba | 64 | if (is_object($schema->items)) { |
| TMBOY | 44:c1d8923072ba | 65 | // just one type definition for the whole array |
| TMBOY | 44:c1d8923072ba | 66 | foreach ($value as $k => $v) { |
| TMBOY | 44:c1d8923072ba | 67 | $initErrors = $this->getErrors(); |
| TMBOY | 44:c1d8923072ba | 68 | |
| TMBOY | 44:c1d8923072ba | 69 | // First check if its defined in "items" |
| TMBOY | 44:c1d8923072ba | 70 | $this->checkUndefined($v, $schema->items, $path, $k); |
| TMBOY | 44:c1d8923072ba | 71 | |
| TMBOY | 44:c1d8923072ba | 72 | // Recheck with "additionalItems" if the first test fails |
| TMBOY | 44:c1d8923072ba | 73 | if (count($initErrors) < count($this->getErrors()) && (isset($schema->additionalItems) && $schema->additionalItems !== false)) { |
| TMBOY | 44:c1d8923072ba | 74 | $secondErrors = $this->getErrors(); |
| TMBOY | 44:c1d8923072ba | 75 | $this->checkUndefined($v, $schema->additionalItems, $path, $k); |
| TMBOY | 44:c1d8923072ba | 76 | } |
| TMBOY | 44:c1d8923072ba | 77 | |
| TMBOY | 44:c1d8923072ba | 78 | // Reset errors if needed |
| TMBOY | 44:c1d8923072ba | 79 | if (isset($secondErrors) && count($secondErrors) < count($this->getErrors())) { |
| TMBOY | 44:c1d8923072ba | 80 | $this->errors = $secondErrors; |
| TMBOY | 44:c1d8923072ba | 81 | } elseif (isset($secondErrors) && count($secondErrors) === count($this->getErrors())) { |
| TMBOY | 44:c1d8923072ba | 82 | $this->errors = $initErrors; |
| TMBOY | 44:c1d8923072ba | 83 | } |
| TMBOY | 44:c1d8923072ba | 84 | } |
| TMBOY | 44:c1d8923072ba | 85 | } else { |
| TMBOY | 44:c1d8923072ba | 86 | // Defined item type definitions |
| TMBOY | 44:c1d8923072ba | 87 | foreach ($value as $k => $v) { |
| TMBOY | 44:c1d8923072ba | 88 | if (array_key_exists($k, $schema->items)) { |
| TMBOY | 44:c1d8923072ba | 89 | $this->checkUndefined($v, $schema->items[$k], $path, $k); |
| TMBOY | 44:c1d8923072ba | 90 | } else { |
| TMBOY | 44:c1d8923072ba | 91 | // Additional items |
| TMBOY | 44:c1d8923072ba | 92 | if (property_exists($schema, 'additionalItems')) { |
| TMBOY | 44:c1d8923072ba | 93 | if ($schema->additionalItems !== false) { |
| TMBOY | 44:c1d8923072ba | 94 | $this->checkUndefined($v, $schema->additionalItems, $path, $k); |
| TMBOY | 44:c1d8923072ba | 95 | } else { |
| TMBOY | 44:c1d8923072ba | 96 | $this->addError( |
| TMBOY | 44:c1d8923072ba | 97 | $path, 'The item ' . $i . '[' . $k . '] is not defined and the definition does not allow additional items', 'additionalItems', array('additionalItems' => $schema->additionalItems,)); |
| TMBOY | 44:c1d8923072ba | 98 | } |
| TMBOY | 44:c1d8923072ba | 99 | } else { |
| TMBOY | 44:c1d8923072ba | 100 | // Should be valid against an empty schema |
| TMBOY | 44:c1d8923072ba | 101 | $this->checkUndefined($v, new \stdClass(), $path, $k); |
| TMBOY | 44:c1d8923072ba | 102 | } |
| TMBOY | 44:c1d8923072ba | 103 | } |
| TMBOY | 44:c1d8923072ba | 104 | } |
| TMBOY | 44:c1d8923072ba | 105 | |
| TMBOY | 44:c1d8923072ba | 106 | // Treat when we have more schema definitions than values, not for empty arrays |
| TMBOY | 44:c1d8923072ba | 107 | if (count($value) > 0) { |
| TMBOY | 44:c1d8923072ba | 108 | for ($k = count($value); $k < count($schema->items); $k++) { |
| TMBOY | 44:c1d8923072ba | 109 | $this->checkUndefined($this->factory->createInstanceFor('undefined'), $schema->items[$k], $path, $k); |
| TMBOY | 44:c1d8923072ba | 110 | } |
| TMBOY | 44:c1d8923072ba | 111 | } |
| TMBOY | 44:c1d8923072ba | 112 | } |
| TMBOY | 44:c1d8923072ba | 113 | } |
| TMBOY | 44:c1d8923072ba | 114 | } |
