/src/JsonSchema/Constraints/Collection.php

https://github.com/igorw/json-schema · PHP · 95 lines · 58 code · 8 blank · 29 comment · 25 complexity · 64c6ba01cff5ace8ed5ea23040cd8863 MD5 · raw file

  1. <?php
  2. namespace JsonSchema\Constraints;
  3. /**
  4. * The Collection Constraints, validates an array against a given schema
  5. *
  6. * @author Robert Schönthal <seroscho@googlemail.com>
  7. * @author Bruno Prieto Reis <bruno.p.reis@gmail.com>
  8. */
  9. class Collection extends Constraint
  10. {
  11. /**
  12. * {inheritDoc}
  13. */
  14. public function check($value, $schema = null, $path = null, $i = null)
  15. {
  16. // verify minItems
  17. if (isset($schema->minItems) && count($value) < $schema->minItems) {
  18. $this->addError($path, "There must be a minimum of " . $schema->minItems . " in the array");
  19. }
  20. // verify maxItems
  21. if (isset($schema->maxItems) && count($value) > $schema->maxItems) {
  22. $this->addError($path, "There must be a maximum of " . $schema->maxItems . " in the array");
  23. }
  24. // verify uniqueItems
  25. //TODO array_unique doesnt work with objects
  26. if (isset($schema->uniqueItems) && array_unique($value) != $value) {
  27. $this->addError($path, "There are no duplicates allowed in the array");
  28. }
  29. //verify items
  30. if (isset($schema->items)) {
  31. $this->validateItems($value, $schema, $path, $i);
  32. }
  33. }
  34. /**
  35. * validates the items
  36. *
  37. * @param array $value
  38. * @param \stdClass $schema
  39. * @param string $path
  40. * @param string $i
  41. */
  42. protected function validateItems($value, $schema = null, $path = null, $i = null)
  43. {
  44. if (!is_array($schema->items)) {
  45. // just one type definition for the whole array
  46. foreach ($value as $k => $v) {
  47. $initErrors = $this->getErrors();
  48. //first check if its defined in "items"
  49. if (!isset($schema->additionalItems) || $schema->additionalItems === false) {
  50. $this->checkUndefined($v, $schema->items, $path, $k);
  51. }
  52. //recheck with "additionalItems" if the first test fails
  53. if (count($initErrors) < count($this->getErrors()) && (isset($schema->additionalItems) && $schema->additionalItems !== false)) {
  54. $secondErrors = $this->getErrors();
  55. $this->checkUndefined($v, $schema->additionalItems, $path, $k);
  56. }
  57. //reset errors if needed
  58. if (isset($secondErrors) && count($secondErrors) < $this->getErrors()) {
  59. $this->errors = $secondErrors;
  60. } elseif (isset($secondErrors) && count($secondErrors) == count($this->getErrors())) {
  61. $this->errors = $initErrors;
  62. }
  63. }
  64. } else {
  65. //defined item type definitions
  66. foreach ($value as $k => $v) {
  67. if (array_key_exists($k, $schema->items)) {
  68. $this->checkUndefined($v, $schema->items[$k], $path, $k);
  69. } else {
  70. // additional items
  71. if (array_key_exists('additionalItems', $schema) && $schema->additionalItems !== false) {
  72. $this->checkUndefined($v, $schema->additionalItems, $path, $k);
  73. } else {
  74. $this->addError(
  75. $path,
  76. 'The item ' . $i . '[' . $k . '] is not defined in the objTypeDef and the objTypeDef does not allow additional properties'
  77. );
  78. }
  79. }
  80. }
  81. // treat when we have more schema definitions than values
  82. for ($k = count($value); $k < count($schema->items); $k++) {
  83. $this->checkUndefined(new Undefined(), $schema->items[$k], $path, $k);
  84. }
  85. }
  86. }
  87. }