PageRenderTime 47ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/justinrainbow/json-schema/src/JsonSchema/Constraints/CollectionConstraint.php

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