src/Schema.php
protected function validateArray($value, ValidationField $field) {
        if ((!is_array($value) || (count($value) > 0 && !array_key_exists(0, $value))) && !$value instanceof \Traversable) {
            $field->addTypeError($value, 'array');
            return Invalid::value();
        } else {
            if ((null !== $minItems = $field->val('minItems')) && count($value) < $minItems) {
                $field->addError(
                    'minItems',
                    [
                        'messageCode' => 'This must contain at least {minItems} {minItems,plural,item,items}.',
                        'minItems' => $minItems,
                    ]
                );
            }
            if ((null !== $maxItems = $field->val('maxItems')) && count($value) > $maxItems) {
                $field->addError(
                    'maxItems',
                    [
                        'messageCode' => 'This must contain no more than {maxItems} {maxItems,plural,item,items}.',
                        'maxItems' => $maxItems,
                    ]
                );
            }

            if ($field->val('uniqueItems') && count($value) > count(array_unique($value))) {
                $field->addError(
                    'uniqueItems',
                    [
                        'messageCode' => 'The array must contain unique items.',
                    ]
                );
            }

            if ($field->val('items') !== null) {
                list ($items, $schemaPath) = $this->lookupSchema($field->val('items'), $field->getSchemaPath().'/items');

                // Validate each of the types.
                $itemValidation = new ValidationField(
                    $field->getValidation(),
                    $items,
                    '',
                    $schemaPath,
                    $field->getOptions()
                );

                $result = [];
                $count = 0;
                foreach ($value as $i => $item) {
                    $itemValidation->setName($field->getName()."/$i");
                    $validItem = $this->validateField($item, $itemValidation);
                    if (Invalid::isValid($validItem)) {
                        $result[] = $validItem;
                    }
                    $count++;
                }

                return empty($result) && $count > 0 ? Invalid::value() : $result;
            } else {
                // Cast the items into a proper numeric array.
                $result = is_array($value) ? array_values($value) : iterator_to_array($value);
                return $result;
            }
        }
    }
protected function validateArray($value, ValidationField $field) {
        if ((!is_array($value) || (count($value) > 0 && !array_key_exists(0, $value))) && !$value instanceof \Traversable) {
            $field->addTypeError($value, 'array');
            return Invalid::value();
        } else {
            if ((null !== $minItems = $field->val('minItems')) && count($value) < $minItems) {
                $field->addError(
                    'minItems',
                    [
                        'messageCode' => 'This must contain at least {minItems} {minItems,plural,item,items}.',
                        'minItems' => $minItems,
                    ]
                );
            }
            if ((null !== $maxItems = $field->val('maxItems')) && count($value) > $maxItems) {
                $field->addError(
                    'maxItems',
                    [
                        'messageCode' => 'This must contain no more than {maxItems} {maxItems,plural,item,items}.',
                        'maxItems' => $maxItems,
                    ]
                );
            }

            if ($field->val('uniqueItems') && count($value) > count(array_unique($value))) {
                $field->addError(
                    'uniqueItems',
                    [
                        'messageCode' => 'The array must contain unique items.',
                    ]
                );
            }

            if ($field->val('items') !== null) {
                list ($items, $schemaPath) = $this->lookupSchema($field->val('items'), $field->getSchemaPath().'/items');

                // Validate each of the types.
                $itemValidation = new ValidationField(
                    $field->getValidation(),
                    $items,
                    '',
                    $schemaPath,
                    $field->getOptions()
                );

                $result = [];
                $count = 0;
                foreach ($value as $i => $item) {
                    $itemValidation->setName($field->getName()."/$i");
                    $validItem = $this->validateField($item, $itemValidation);
                    if (Invalid::isValid($validItem)) {
                        $result[] = $validItem;
                    }
                    $count++;
                }

                return empty($result) && $count > 0 ? Invalid::value() : $result;
            } else {
                // Cast the items into a proper numeric array.
                $result = is_array($value) ? array_values($value) : iterator_to_array($value);
                return $result;
            }
        }
    }
protected function validateArray ( $ value , ValidationField $ field ) { if ( ( ! is_array ( $ value ) || ( count ( $ value ) > 0 && ! array_key_exists ( 0 , $ value ) ) ) && ! $ value instanceof \ Traversable ) { $ field -> addTypeError ( $ value , 'array' ) ; return Invalid :: value ( ) ; } else { if ( ( null !== $ minItems = $ field -> val ( 'minItems' ) ) && count ( $ value ) < $ minItems ) { $ field -> addError ( 'minItems' , [ 'messageCode' => 'This must contain at least {minItems} {minItems,plural,item,items}.' , 'minItems' => $ minItems , ] ) ; } if ( ( null !== $ maxItems = $ field -> val ( 'maxItems' ) ) && count ( $ value ) > $ maxItems ) { $ field -> addError ( 'maxItems' , [ 'messageCode' => 'This must contain no more than {maxItems} {maxItems,plural,item,items}.' , 'maxItems' => $ maxItems , ] ) ; } if ( $ field -> val ( 'uniqueItems' ) && count ( $ value ) > count ( array_unique ( $ value ) ) ) { $ field -> addError ( 'uniqueItems' , [ 'messageCode' => 'The array must contain unique items.' , ] ) ; } if ( $ field -> val ( 'items' ) !== null ) { list ( $ items , $ schemaPath ) = $ this -> lookupSchema ( $ field -> val ( 'items' ) , $ field -> getSchemaPath ( ) . '/items' ) ; // Validate each of the types. $ itemValidation = new ValidationField ( $ field -> getValidation ( ) , $ items , '' , $ schemaPath , $ field -> getOptions ( ) ) ; $ result = [ ] ; $ count = 0 ; foreach ( $ value as $ i => $ item ) { $ itemValidation -> setName ( $ field -> getName ( ) . "/$i" ) ; $ validItem = $ this -> validateField ( $ item , $ itemValidation ) ; if ( Invalid :: isValid ( $ validItem ) ) { $ result [ ] = $ validItem ; } $ count ++ ; } return empty ( $ result ) && $ count > 0 ? Invalid :: value ( ) : $ result ; } else { // Cast the items into a proper numeric array. $ result = is_array ( $ value ) ? array_values ( $ value ) : iterator_to_array ( $ value ) ; return $ result ; } } }
Validate an array.

@param mixed $value The value to validate.
@param ValidationField $field The validation results to add.
@return array|Invalid Returns an array or invalid if validation fails.
@throws RefNotFoundException Throws an exception if the array has an items `$ref` that cannot be found.
Validate an array .