|
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
|
87 |
|
public function setSpecification($specification = null) |
|
48
|
|
|
{ |
|
49
|
87 |
|
$this->checkSpecificationIsArray($specification); |
|
50
|
|
|
|
|
51
|
86 |
|
$this->initItems($specification); |
|
52
|
84 |
|
$this->initAdditionalItems($specification); |
|
53
|
83 |
|
} |
|
54
|
|
|
|
|
55
|
84 |
|
private function initAdditionalItems($specification) |
|
56
|
|
|
{ |
|
57
|
84 |
|
if (isset($specification[self::ADDITIONAL_ITEMS_INDEX])) { |
|
58
|
83 |
|
$this->setAdditionalItems($specification[self::ADDITIONAL_ITEMS_INDEX]); |
|
59
|
82 |
|
} |
|
60
|
83 |
|
} |
|
61
|
|
|
|
|
62
|
86 |
|
private function initItems($specification) |
|
63
|
|
|
{ |
|
64
|
86 |
|
if (isset($specification[self::ITEMS_INDEX])) { |
|
65
|
85 |
|
$this->setItems($specification[self::ITEMS_INDEX]); |
|
66
|
83 |
|
} |
|
67
|
84 |
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* {@inheritdoc} |
|
71
|
|
|
* |
|
72
|
|
|
* @see \Mcustiel\SimpleRequest\Validator\AbstractAnnotationSpecifiedValidator::validate() |
|
73
|
|
|
*/ |
|
74
|
85 |
|
public function validate($value) |
|
75
|
|
|
{ |
|
76
|
85 |
|
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
|
84 |
|
if (empty($this->items)) { |
|
83
|
|
|
return true; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
84 |
|
return $this->executeItemsValidation($value); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
84 |
|
private function executeItemsValidation($value) |
|
90
|
|
|
{ |
|
91
|
84 |
|
if ($this->items instanceof ValidatorInterface) { |
|
92
|
2 |
|
return $this->validateArray($value, $this->items); |
|
93
|
|
|
} |
|
94
|
82 |
|
$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
|
82 |
|
if ($this->additionalItems === false) { |
|
99
|
82 |
|
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
|
|
|
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
|
|
|
private function validateRest(array $list) |
|
117
|
|
|
{ |
|
118
|
|
|
$count = count($this->items); |
|
119
|
|
|
return $this->additionalItems === true || |
|
120
|
|
|
$this->validateArray( |
|
121
|
|
|
array_slice($list, $count, count($list) - $count), |
|
122
|
|
|
$this->additionalItems |
|
|
|
|
|
|
123
|
|
|
); |
|
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
|
82 |
|
private function validateTuple(array $tuple) |
|
135
|
|
|
{ |
|
136
|
82 |
|
$keys = array_keys($this->items); |
|
137
|
82 |
|
$count = count($this->items); |
|
138
|
82 |
|
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
|
82 |
|
if (isset($tuple[$keys[$index]]) && |
|
143
|
82 |
|
!$this->items[$index]->validate($tuple[$keys[$index]])) { |
|
144
|
2 |
|
return false; |
|
145
|
|
|
} |
|
146
|
82 |
|
} |
|
147
|
|
|
|
|
148
|
80 |
|
return true; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* Checks and sets the specified items values. |
|
153
|
|
|
* |
|
154
|
|
|
* @param array|\Mcustiel\SimpleRequest\Interfaces\ValidatorInterface $specification |
|
155
|
|
|
*/ |
|
156
|
85 |
|
private function setItems($specification) |
|
157
|
|
|
{ |
|
158
|
85 |
|
if ($specification instanceof ValidatorAnnotation) { |
|
159
|
2 |
|
$this->items = $this->createValidatorInstanceFromAnnotation( |
|
|
|
|
|
|
160
|
|
|
$specification |
|
161
|
2 |
|
); |
|
162
|
2 |
|
} else { |
|
163
|
83 |
|
$this->checkSpecificationIsArray($specification); |
|
164
|
82 |
|
$this->items = $this->convertAnnotationsToValidators($specification); |
|
|
|
|
|
|
165
|
|
|
} |
|
166
|
83 |
|
} |
|
167
|
|
|
|
|
168
|
82 |
|
private function convertAnnotationsToValidators(array $specification) |
|
169
|
|
|
{ |
|
170
|
82 |
|
$items = []; |
|
171
|
82 |
|
foreach ($specification as $index => $validator) { |
|
172
|
82 |
|
$items[$index] = $this->checkIfAnnotationAndReturnObject($validator); |
|
173
|
81 |
|
} |
|
174
|
81 |
|
return $items; |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
/** |
|
178
|
|
|
* Checks and sets the specified additionalItems. |
|
179
|
|
|
* |
|
180
|
|
|
* @param bool|\Mcustiel\SimpleRequest\Interfaces\ValidatorInterface $specification |
|
181
|
|
|
*/ |
|
182
|
83 |
|
private function setAdditionalItems($specification) |
|
183
|
|
|
{ |
|
184
|
83 |
|
if (is_bool($specification)) { |
|
185
|
82 |
|
$this->additionalItems = $specification; |
|
186
|
83 |
|
} elseif ($specification instanceof ValidatorAnnotation) { |
|
187
|
|
|
$this->additionalItems = $this->createValidatorInstanceFromAnnotation( |
|
188
|
|
|
$specification |
|
189
|
|
|
); |
|
190
|
|
|
} else { |
|
191
|
1 |
|
throw new UnspecifiedValidatorException( |
|
192
|
|
|
'The validator is being initialized without a valid validator Annotation' |
|
193
|
1 |
|
); |
|
194
|
|
|
} |
|
195
|
82 |
|
} |
|
196
|
|
|
|
|
197
|
|
|
/** |
|
198
|
|
|
* Validates an array against a specific validator. |
|
199
|
|
|
* |
|
200
|
|
|
* @param array $array |
|
201
|
|
|
* @param \Mcustiel\SimpleRequest\Interfaces\ValidatorInterface $validator |
|
202
|
|
|
*/ |
|
203
|
2 |
|
private function validateArray(array $array, ValidatorInterface $validator) |
|
204
|
|
|
{ |
|
205
|
2 |
|
foreach ($array as $item) { |
|
206
|
2 |
|
if (!$validator->validate($item)) { |
|
207
|
|
|
return false; |
|
208
|
|
|
} |
|
209
|
2 |
|
} |
|
210
|
|
|
|
|
211
|
2 |
|
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.