1 | <?php |
||
32 | class Items extends AbstractIterableValidator |
||
33 | { |
||
34 | const ITEMS_INDEX = 'items'; |
||
35 | const ADDITIONAL_ITEMS_INDEX = 'additionalItems'; |
||
36 | |||
37 | /** |
||
38 | * @var bool|\Mcustiel\SimpleRequest\Interfaces\ValidatorInterface |
||
39 | */ |
||
40 | private $additionalItems = true; |
||
41 | |||
42 | /** |
||
43 | * {@inheritdoc} |
||
44 | * |
||
45 | * @see \Mcustiel\SimpleRequest\Validator\AbstractIterableValidator::setSpecification() |
||
46 | */ |
||
47 | 90 | public function setSpecification($specification = null) |
|
54 | |||
55 | 87 | private function initAdditionalItems($specification) |
|
61 | |||
62 | 89 | private function initItems($specification) |
|
68 | |||
69 | /** |
||
70 | * {@inheritdoc} |
||
71 | * |
||
72 | * @see \Mcustiel\SimpleRequest\Validator\AbstractAnnotationSpecifiedValidator::validate() |
||
73 | */ |
||
74 | 88 | public function validate($value) |
|
88 | |||
89 | 87 | private function executeItemsValidation($value) |
|
90 | { |
||
91 | 87 | if ($this->items instanceof ValidatorInterface) { |
|
92 | 2 | return $this->validateArray($value, $this->items); |
|
93 | } |
||
94 | 85 | $valid = $this->validateTuple($value); |
|
95 | // From json-schema definition: if the value of "additionalItems" is boolean value false and |
||
96 | // the value of "items" is an array, the instance is valid if its size is less than, or |
||
97 | // equal to, the size of "items". |
||
98 | 85 | if ($this->additionalItems === false) { |
|
99 | 83 | return $valid && (count($value) <= count($this->items)); |
|
100 | } |
||
101 | |||
102 | // From json-schema definition: if the value of "additionalItems" is |
||
103 | // boolean value true or an object, validation of the instance always succeeds; |
||
104 | 2 | return $this->validateRest($value); |
|
105 | } |
||
106 | |||
107 | /** |
||
108 | * Validates each element against its corresponding validator. Then, |
||
109 | * if additionalItems is a validator, checks the rest again those |
||
110 | * validators. |
||
111 | * |
||
112 | * @param array $list |
||
113 | * |
||
114 | * @return bool |
||
115 | */ |
||
116 | 2 | private function validateRest(array $list) |
|
117 | { |
||
118 | 2 | $count = count($this->items); |
|
119 | 2 | return $this->additionalItems === true || |
|
120 | 2 | $this->validateArray( |
|
121 | 2 | array_slice($list, $count, count($list) - $count), |
|
122 | 2 | $this->additionalItems |
|
|
|||
123 | 2 | ); |
|
124 | } |
||
125 | |||
126 | /** |
||
127 | * Validates each one of the elements of the array against |
||
128 | * its corresponding specified validator. |
||
129 | * |
||
130 | * @param array $tuple |
||
131 | * |
||
132 | * @return bool |
||
133 | */ |
||
134 | 85 | private function validateTuple(array $tuple) |
|
135 | { |
||
136 | 85 | $keys = array_keys($tuple); |
|
137 | 85 | $count = count($this->items); |
|
138 | 85 | for ($index = 0; $index < $count; $index++) { |
|
139 | // In the specification is not clear what to do when instance size |
||
140 | // is less than items size. I chose to pass null and if null passes |
||
141 | // the validation, it returns true. |
||
142 | 85 | $value = isset($keys[$index]) ? $tuple[$keys[$index]] : null; |
|
143 | 85 | if (!$this->items[$index]->validate($value)) { |
|
144 | 6 | return false; |
|
145 | } |
||
146 | 85 | } |
|
147 | |||
148 | 79 | return true; |
|
149 | } |
||
150 | |||
151 | /** |
||
152 | * Checks and sets the specified items values. |
||
153 | * |
||
154 | * @param array|\Mcustiel\SimpleRequest\Interfaces\ValidatorInterface $specification |
||
155 | */ |
||
156 | 88 | private function setItems($specification) |
|
167 | |||
168 | 85 | private function convertAnnotationsToValidators(array $specification) |
|
176 | |||
177 | /** |
||
178 | * Checks and sets the specified additionalItems. |
||
179 | * |
||
180 | * @param bool|\Mcustiel\SimpleRequest\Interfaces\ValidatorInterface $specification |
||
181 | */ |
||
182 | 86 | private function setAdditionalItems($specification) |
|
196 | |||
197 | /** |
||
198 | * Validates an array against a specific validator. |
||
199 | * |
||
200 | * @param array $array |
||
201 | * @param \Mcustiel\SimpleRequest\Interfaces\ValidatorInterface $validator |
||
202 | */ |
||
203 | 4 | private function validateArray(array $array, ValidatorInterface $validator) |
|
213 | } |
||
214 |
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.