1 | <?php |
||
31 | class Items extends AbstractIterableValidator |
||
32 | { |
||
33 | const ITEMS_INDEX = 'items'; |
||
34 | const ADDITIONAL_ITEMS_INDEX = 'additionalItems'; |
||
35 | |||
36 | /** |
||
37 | * @var bool|\Mcustiel\SimpleRequest\Interfaces\ValidatorInterface |
||
38 | */ |
||
39 | private $additionalItems = true; |
||
40 | |||
41 | /** |
||
42 | * {@inheritdoc} |
||
43 | * @see \Mcustiel\SimpleRequest\Validator\AbstractIterableValidator::setSpecification() |
||
44 | */ |
||
45 | 74 | public function setSpecification($specification = null) |
|
56 | |||
57 | /** |
||
58 | * {@inheritdoc} |
||
59 | * @see \Mcustiel\SimpleRequest\Validator\AbstractAnnotationSpecifiedValidator::validate() |
||
60 | */ |
||
61 | 75 | public function validate($value) |
|
75 | |||
76 | private function executeItemsValidation($value) |
||
77 | { |
||
78 | if ($this->items instanceof ValidatorInterface) { |
||
79 | return $this->validateArray($value, $this->items); |
||
80 | } |
||
81 | |||
82 | // From json-schema definition: if the value of "additionalItems" is boolean value false and |
||
83 | // the value of "items" is an array, the instance is valid if its size is less than, or |
||
84 | // equal to, the size of "items". |
||
85 | if ($this->additionalItems === false) { |
||
86 | return (count($value) <= count($this->items)) |
||
87 | && $this->validateTuple($value); |
||
88 | } |
||
89 | |||
90 | // From json-schema definition: if the value of "additionalItems" is |
||
91 | // boolean value true or an object, validation of the instance always succeeds; |
||
92 | return $this->validateList($value); |
||
93 | } |
||
94 | |||
95 | /** |
||
96 | * Validates each element against its corresponding validator. Then, |
||
97 | * if additionalItems is a validator, checks the rest again those |
||
98 | * validators. |
||
99 | * |
||
100 | * @param array $list |
||
101 | * |
||
102 | * @return bool |
||
103 | */ |
||
104 | private function validateList(array $list) |
||
115 | |||
116 | /** |
||
117 | * Validates each one of the elements of the array against |
||
118 | * its corresponding specified validator. |
||
119 | * |
||
120 | * @param array $tuple |
||
121 | * |
||
122 | * @return bool |
||
123 | */ |
||
124 | private function validateTuple(array $tuple) |
||
125 | { |
||
126 | $keys = array_keys($tuple); |
||
127 | $count = count($this->items); |
||
128 | for ($index = 0; $index < $count; $index++) { |
||
129 | $validator = $this->items[$index]; |
||
130 | // In the specification is not clear what to do when instance size |
||
131 | // is less than items size. I chose to pass null and if null passes |
||
132 | // the validation, it returns true. |
||
133 | if (!$validator->validate( |
||
134 | isset($tuple[$keys[$index]]) ? $tuple[$keys[$index]] : null |
||
135 | )) { |
||
136 | return false; |
||
137 | } |
||
138 | } |
||
139 | |||
140 | return true; |
||
141 | } |
||
142 | |||
143 | /** |
||
144 | * Checks and sets the specified items values. |
||
145 | * |
||
146 | * @param array|\Mcustiel\SimpleRequest\Interfaces\ValidatorInterface $specification |
||
147 | */ |
||
148 | 74 | private function setItems($specification) |
|
158 | |||
159 | /** |
||
160 | * Checks and sets the specified additionalItems. |
||
161 | * |
||
162 | * @param bool|\Mcustiel\SimpleRequest\Interfaces\ValidatorInterface $specification |
||
163 | */ |
||
164 | 74 | private function setAdditionalItems($specification) |
|
174 | |||
175 | /** |
||
176 | * Validates an array against a specific validator. |
||
177 | * |
||
178 | * @param array $array |
||
179 | * @param \Mcustiel\SimpleRequest\Interfaces\ValidatorInterface $validator |
||
180 | */ |
||
181 | private function validateArray(array $array, ValidatorInterface $validator) |
||
191 | } |
||
192 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.