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