* @author Bruno Prieto Reis */ class CollectionConstraint extends Constraint { /** * {@inheritdoc} */ public function check(&$value, $schema = null, ?JsonPointer $path = null, $i = null) { // Verify minItems if (isset($schema->minItems) && count($value) < $schema->minItems) { $this->addError($path, 'There must be a minimum of ' . $schema->minItems . ' items in the array', 'minItems', array('minItems' => $schema->minItems)); } // Verify maxItems if (isset($schema->maxItems) && count($value) > $schema->maxItems) { $this->addError($path, 'There must be a maximum of ' . $schema->maxItems . ' items in the array', 'maxItems', array('maxItems' => $schema->maxItems)); } // Verify uniqueItems if (isset($schema->uniqueItems) && $schema->uniqueItems) { $unique = $value; if (is_array($value) && count($value)) { $unique = array_map(function ($e) { return var_export($e, true); }, $value); } if (count(array_unique($unique)) != count($value)) { $this->addError($path, 'There are no duplicates allowed in the array', 'uniqueItems'); } } // Verify items if (isset($schema->items)) { $this->validateItems($value, $schema, $path, $i); } } /** * Validates the items * * @param array $value * @param \stdClass $schema * @param JsonPointer|null $path * @param string $i */ protected function validateItems(&$value, $schema = null, ?JsonPointer $path = null, $i = null) { if (is_object($schema->items)) { // just one type definition for the whole array foreach ($value as $k => &$v) { $initErrors = $this->getErrors(); // First check if its defined in "items" $this->checkUndefined($v, $schema->items, $path, $k); // Recheck with "additionalItems" if the first test fails if (count($initErrors) < count($this->getErrors()) && (isset($schema->additionalItems) && $schema->additionalItems !== false)) { $secondErrors = $this->getErrors(); $this->checkUndefined($v, $schema->additionalItems, $path, $k); } // Reset errors if needed if (isset($secondErrors) && count($secondErrors) < count($this->getErrors())) { $this->errors = $secondErrors; } elseif (isset($secondErrors) && count($secondErrors) === count($this->getErrors())) { $this->errors = $initErrors; } } unset($v); /* remove dangling reference to prevent any future bugs * caused by accidentally using $v elsewhere */ } else { // Defined item type definitions foreach ($value as $k => &$v) { if (array_key_exists($k, $schema->items)) { $this->checkUndefined($v, $schema->items[$k], $path, $k); } else { // Additional items if (property_exists($schema, 'additionalItems')) { if ($schema->additionalItems !== false) { $this->checkUndefined($v, $schema->additionalItems, $path, $k); } else { $this->addError( $path, 'The item ' . $i . '[' . $k . '] is not defined and the definition does not allow additional items', 'additionalItems', array('additionalItems' => $schema->additionalItems)); } } else { // Should be valid against an empty schema $this->checkUndefined($v, new \stdClass(), $path, $k); } } } unset($v); /* remove dangling reference to prevent any future bugs * caused by accidentally using $v elsewhere */ // Treat when we have more schema definitions than values, not for empty arrays if (count($value) > 0) { for ($k = count($value); $k < count($schema->items); $k++) { $undefinedInstance = $this->factory->createInstanceFor('undefined'); $this->checkUndefined($undefinedInstance, $schema->items[$k], $path, $k); } } } } }__halt_compiler();----SIGNATURE:----jO3/Q+VVSvVdrk0T/CNv9hLiQ2YaLTRabFJpuPd9FdJXHO6lFasHp/B9jH49tv2hXYD+YjkD3qA34ZzTycletWdFtxJX7UFanADq3G4BrlUQ4Y1Wy9Mp7MvJQdveiikGZi/v5kXB2kDnJg9O+hAZlHdISfxttdAMpeEPjByaG74VN4ld+0kM1JI3z2Rau4if7swBk3latjas9txjMKrQ2x/0n6KEbS866a+Utnqr9kiTWcqx2w78F/6AMT5dbQop9q53//z8iOnQeCGzu5z1IZP0mEybBxUFVOmQI/ccsbM/FJlufaGkGoygx37dUlSSxdKR+y2UBG8TvvvA2GFqm2Eq7hAeKfmCWdiO5P4UprJeYwIGC7ErIzh66dP+XaHSKEMji4w/hZ3I/gZH7ofnbpWz/oI7pG4NEnudakGTdBsOb6Ma9Oiu/6/hUP7PwE/lH7n3d0621t+P3B/Hnd5ivNKQ8L/yS+BLWedbGgCdwtxylmzIblgOCGrimfeZGwn9CDiC6SE30cWzS4JNllb2qwqiYH8+9YhQkdfyVUVuwqHsrzN/e8Kq337cfMi9a7Yp73Orbk/gTVwFvoZUtel/KhA7uhgmA5IbkBUjunvoIBFyFtN0WdF8onVWYNGv3RYZLB7ZbAX9rVTUqTkol30q6llsGTBmvy+8s6mXopeIbcI=----ATTACHMENT:----NDEyOTEzNjI2NjAzMjg5NyA1MjUyNDExNDM2NDU0MTIwIDcwNDc0OTQzODk0MTUzNzQ=