1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of php-simple-request. |
4
|
|
|
* |
5
|
|
|
* php-simple-request is free software: you can redistribute it and/or modify |
6
|
|
|
* it under the terms of the GNU Lesser General Public License as published by |
7
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
8
|
|
|
* (at your option) any later version. |
9
|
|
|
* |
10
|
|
|
* php-simple-request is distributed in the hope that it will be useful, |
11
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
12
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13
|
|
|
* GNU General Public License for more details. |
14
|
|
|
* |
15
|
|
|
* You should have received a copy of the GNU General Public License |
16
|
|
|
* along with php-simple-request. If not, see <http://www.gnu.org/licenses/>. |
17
|
|
|
*/ |
18
|
|
|
namespace Mcustiel\SimpleRequest\Validator; |
19
|
|
|
|
20
|
|
|
use Mcustiel\SimpleRequest\Interfaces\ValidatorInterface; |
21
|
|
|
use Mcustiel\SimpleRequest\Annotation\ValidatorAnnotation; |
22
|
|
|
use Mcustiel\SimpleRequest\Exception\UnspecifiedValidatorException; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Checks that each element of an array validates against its corresponding |
26
|
|
|
* validator in a collection. |
27
|
|
|
* <a href="http://spacetelescope.github.io/understanding-json-schema/UnderstandingJSONSchema.pdf">Here</a> |
28
|
|
|
* you can see examples of use for this validator. |
29
|
|
|
* |
30
|
|
|
* @author mcustiel |
31
|
|
|
*/ |
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) |
48
|
|
|
{ |
49
|
90 |
|
$this->checkSpecificationIsArray($specification); |
50
|
|
|
|
51
|
89 |
|
$this->initItems($specification); |
52
|
87 |
|
$this->initAdditionalItems($specification); |
53
|
86 |
|
} |
54
|
|
|
|
55
|
87 |
|
private function initAdditionalItems($specification) |
56
|
|
|
{ |
57
|
87 |
|
if (isset($specification[self::ADDITIONAL_ITEMS_INDEX])) { |
58
|
86 |
|
$this->setAdditionalItems($specification[self::ADDITIONAL_ITEMS_INDEX]); |
59
|
85 |
|
} |
60
|
86 |
|
} |
61
|
|
|
|
62
|
89 |
|
private function initItems($specification) |
63
|
|
|
{ |
64
|
89 |
|
if (isset($specification[self::ITEMS_INDEX])) { |
65
|
88 |
|
$this->setItems($specification[self::ITEMS_INDEX]); |
66
|
86 |
|
} |
67
|
87 |
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* {@inheritdoc} |
71
|
|
|
* |
72
|
|
|
* @see \Mcustiel\SimpleRequest\Validator\AbstractAnnotationSpecifiedValidator::validate() |
73
|
|
|
*/ |
74
|
88 |
|
public function validate($value) |
75
|
|
|
{ |
76
|
88 |
|
if (!is_array($value)) { |
77
|
1 |
|
return false; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
// From json-schema definition: if "items" is not present, or its value is an object, |
81
|
|
|
// validation of the instance always succeeds, regardless of the value of "additionalItems"; |
82
|
87 |
|
if (empty($this->items)) { |
83
|
|
|
return true; |
84
|
|
|
} |
85
|
|
|
|
86
|
87 |
|
return $this->executeItemsValidation($value); |
87
|
|
|
} |
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) |
157
|
|
|
{ |
158
|
88 |
|
if ($specification instanceof ValidatorAnnotation) { |
159
|
2 |
|
$this->items = $this->createValidatorInstanceFromAnnotation( |
|
|
|
|
160
|
|
|
$specification |
161
|
2 |
|
); |
162
|
2 |
|
} else { |
163
|
86 |
|
$this->checkSpecificationIsArray($specification); |
164
|
85 |
|
$this->items = $this->convertAnnotationsToValidators($specification); |
|
|
|
|
165
|
|
|
} |
166
|
86 |
|
} |
167
|
|
|
|
168
|
85 |
|
private function convertAnnotationsToValidators(array $specification) |
169
|
|
|
{ |
170
|
85 |
|
$items = []; |
171
|
85 |
|
foreach ($specification as $index => $validator) { |
172
|
85 |
|
$items[$index] = $this->checkIfAnnotationAndReturnObject($validator); |
173
|
84 |
|
} |
174
|
84 |
|
return $items; |
175
|
|
|
} |
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) |
183
|
|
|
{ |
184
|
86 |
|
if (is_bool($specification)) { |
185
|
83 |
|
$this->additionalItems = $specification; |
186
|
86 |
|
} elseif ($specification instanceof ValidatorAnnotation) { |
187
|
2 |
|
$this->additionalItems = $this->createValidatorInstanceFromAnnotation( |
188
|
|
|
$specification |
189
|
2 |
|
); |
190
|
2 |
|
} else { |
191
|
1 |
|
throw new UnspecifiedValidatorException( |
192
|
|
|
'The validator is being initialized without a valid validator Annotation' |
193
|
1 |
|
); |
194
|
|
|
} |
195
|
85 |
|
} |
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) |
204
|
|
|
{ |
205
|
4 |
|
foreach ($array as $item) { |
206
|
4 |
|
if (!$validator->validate($item)) { |
207
|
1 |
|
return false; |
208
|
|
|
} |
209
|
3 |
|
} |
210
|
|
|
|
211
|
3 |
|
return true; |
212
|
|
|
} |
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.