99use PHPMicroTemplate \Exception \UndefinedSymbolException ;
1010use PHPModelGenerator \Exception \SchemaException ;
1111use PHPModelGenerator \Model \Property \PropertyInterface ;
12+ use PHPModelGenerator \Model \Validator \AdditionalItemsValidator ;
1213use PHPModelGenerator \Model \Validator \ArrayTupleValidator ;
1314use PHPModelGenerator \Model \Validator \PropertyTemplateValidator ;
1415use PHPModelGenerator \Model \Validator \PropertyValidator ;
@@ -34,9 +35,12 @@ class ArrayProcessor extends AbstractTypedValueProcessor
3435
3536 /**
3637 * @param PropertyInterface $property
37- * @param array $propertyData
38+ * @param array $propertyData
3839 *
40+ * @throws FileSystemException
3941 * @throws SchemaException
42+ * @throws SyntaxErrorException
43+ * @throws UndefinedSymbolException
4044 */
4145 protected function generateValidators (PropertyInterface $ property , array $ propertyData ): void
4246 {
@@ -103,7 +107,10 @@ private function addUniqueItemsValidation(PropertyInterface $property, array $pr
103107 * @param PropertyInterface $property
104108 * @param array $propertyData
105109 *
110+ * @throws FileSystemException
106111 * @throws SchemaException
112+ * @throws SyntaxErrorException
113+ * @throws UndefinedSymbolException
107114 */
108115 private function addItemsValidation (PropertyInterface $ property , array $ propertyData ): void
109116 {
@@ -142,18 +149,18 @@ private function addItemsValidation(PropertyInterface $property, array $property
142149 */
143150 private function addTupleValidator (PropertyInterface $ property , array $ propertyData ): void
144151 {
145- if (isset ($ propertyData ['additionalItems ' ]) && $ propertyData ['additionalItems ' ] !== false ) {
146- // TODO: add validator by reusing the AdditionalPropertiesValidator
152+ if (isset ($ propertyData ['additionalItems ' ]) && $ propertyData ['additionalItems ' ] !== true ) {
153+ $ this -> addAdditionalItemsValidator ( $ property , $ propertyData );
147154 }
148155
149156 $ tupleCount = count ($ propertyData [self ::JSON_FIELD_ITEMS ]);
150157
151158 $ property
152159 ->addValidator (
153160 new PropertyValidator (
154- 'is_array($value) && count($value) < ' . $ tupleCount ,
161+ 'is_array($value) && ($amount = count($value) ) < ' . $ tupleCount ,
155162 sprintf (
156- 'Missing tuple item in array %s. Requires %s items, got " . count($value) . " ' ,
163+ 'Missing tuple item in array %s. Requires %s items, got $amount ' ,
157164 $ property ->getName (),
158165 $ tupleCount
159166 )
@@ -169,6 +176,44 @@ private function addTupleValidator(PropertyInterface $property, array $propertyD
169176 );
170177 }
171178
179+ /**
180+ * @param PropertyInterface $property
181+ * @param array $propertyData
182+ *
183+ * @throws FileSystemException
184+ * @throws SchemaException
185+ * @throws SyntaxErrorException
186+ * @throws UndefinedSymbolException
187+ */
188+ private function addAdditionalItemsValidator (PropertyInterface $ property , array $ propertyData ): void
189+ {
190+ if (!is_bool ($ propertyData ['additionalItems ' ])) {
191+ $ property ->addValidator (
192+ new AdditionalItemsValidator (
193+ $ this ->schemaProcessor ,
194+ $ this ->schema ,
195+ $ propertyData ,
196+ $ property ->getName ()
197+ )
198+ );
199+
200+ return ;
201+ }
202+
203+ $ expectedAmount = count ($ propertyData [self ::JSON_FIELD_ITEMS ]);
204+
205+ $ property ->addValidator (
206+ new PropertyValidator (
207+ '($amount = count($value)) > ' . $ expectedAmount ,
208+ sprintf (
209+ 'Tuple array %s contains not allowed additional items. Expected %s items, got $amount ' ,
210+ $ property ->getName (),
211+ $ expectedAmount
212+ )
213+ )
214+ );
215+ }
216+
172217 /**
173218 * Add the validator to check for constraints required for at least one item
174219 *
0 commit comments