1 | <?php |
||
31 | class Properties extends AbstractIterableValidator |
||
32 | { |
||
33 | const ITEMS_INDEX = 'properties'; |
||
34 | const ADDITIONAL_ITEMS_INDEX = 'additionalProperties'; |
||
35 | |||
36 | /** |
||
37 | * @var bool|\Mcustiel\SimpleRequest\Interfaces\ValidatorInterface |
||
38 | */ |
||
39 | private $additionalItems = true; |
||
40 | |||
41 | /** |
||
42 | * {@inheritdoc} |
||
43 | * |
||
44 | * @see \Mcustiel\SimpleRequest\Validator\AbstractIterableValidator::setSpecification() |
||
45 | */ |
||
46 | 79 | public function setSpecification($specification = null) |
|
47 | { |
||
48 | 79 | $this->checkSpecificationIsArray($specification); |
|
49 | |||
50 | 79 | if (isset($specification[self::ITEMS_INDEX])) { |
|
51 | 79 | $this->setItems($specification[self::ITEMS_INDEX]); |
|
52 | 79 | } |
|
53 | 79 | if (isset($specification[self::ADDITIONAL_ITEMS_INDEX])) { |
|
54 | 79 | $this->setAdditionalItems($specification[self::ADDITIONAL_ITEMS_INDEX]); |
|
55 | 79 | } |
|
56 | 79 | } |
|
57 | |||
58 | /** |
||
59 | * {@inheritdoc} |
||
60 | * |
||
61 | * @see \Mcustiel\SimpleRequest\Validator\AbstractAnnotationSpecifiedValidator::validate() |
||
62 | */ |
||
63 | 80 | public function validate($value) |
|
77 | |||
78 | 80 | private function executePropertiesValidation($value) |
|
96 | |||
97 | 80 | private function convertToArray($value) |
|
98 | { |
||
99 | 80 | if (!is_array($value)) { |
|
100 | return json_decode(json_encode($value), true); |
||
101 | } |
||
102 | 80 | return $value; |
|
103 | } |
||
104 | |||
105 | |||
106 | /** |
||
107 | * Checks all properties against a validator. |
||
108 | * |
||
109 | * @param array $array |
||
110 | * |
||
111 | * @return bool |
||
112 | */ |
||
113 | private function validateWithoutAdditionalItemsConcern(array $array) |
||
114 | { |
||
115 | foreach ($array as $value) { |
||
116 | if (!$this->items->validate($value)) { |
||
|
|||
117 | return false; |
||
118 | } |
||
119 | } |
||
120 | |||
121 | return true; |
||
122 | } |
||
123 | |||
124 | /** |
||
125 | * Validates each element against its validator and if additionalItems is a |
||
126 | * validator, validates the rest of the elements against it. |
||
127 | * |
||
128 | * @param array $list |
||
129 | * |
||
130 | * @return bool |
||
131 | */ |
||
132 | private function validateList(array $list) |
||
145 | |||
146 | private function validateListItems($array) |
||
147 | { |
||
148 | foreach ($array as $item) { |
||
155 | |||
156 | /** |
||
157 | * Validate each element of the array against its corresponding validator. |
||
158 | * |
||
159 | * @param array $tuple |
||
160 | * |
||
161 | * @return bool |
||
162 | */ |
||
163 | 79 | private function validateTuple(array $tuple) |
|
173 | |||
174 | /** |
||
175 | * Checks and sets items specification. |
||
176 | * |
||
177 | * @param array|\Mcustiel\SimpleRequest\Interfaces\ValidatorInterface $specification |
||
178 | */ |
||
179 | 79 | private function setItems($specification) |
|
191 | |||
192 | /** |
||
193 | * Sets the specified additionalItems. |
||
194 | * |
||
195 | * @param bool|\Mcustiel\SimpleRequest\Interfaces\ValidatorInterface $specification |
||
196 | */ |
||
197 | 79 | private function setAdditionalItems($specification) |
|
207 | } |
||
208 |
Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.