diff --git a/src/Form/FormVisitor.php b/src/Form/FormVisitor.php index 17ab830..2096c09 100644 --- a/src/Form/FormVisitor.php +++ b/src/Form/FormVisitor.php @@ -13,6 +13,7 @@ use Laminas\Form\FieldsetInterface; use Laminas\Form\FormInterface; use Laminas\InputFilter\CollectionInputFilter; +use Laminas\InputFilter\InputFilter; use Laminas\InputFilter\InputFilterInterface; use Laminas\InputFilter\InputInterface; use Psalm\Type\Union; @@ -88,29 +89,34 @@ private function convertCollectionFilters( FieldsetInterface $fieldset, InputFilterInterface $inputFilter ): InputFilterInterface { - foreach ($fieldset->getFieldsets() as $childFieldset) { - $name = (string) $childFieldset->getName(); + $newFilter = new InputFilter(); + + foreach ($fieldset->getIterator() as $elementOrFieldset) { + $name = (string) $elementOrFieldset->getName(); if (! $inputFilter->has($name)) { continue; } $inputOrFilter = $inputFilter->get($name); - if (! $inputOrFilter instanceof InputFilterInterface) { + if (! ($inputOrFilter instanceof InputFilterInterface && $elementOrFieldset instanceof FieldsetInterface)) { + $newFilter->add($inputOrFilter, $name); continue; } - $childFilter = $this->convertCollectionFilters($childFieldset, $inputOrFilter); - if (! $childFieldset instanceof Collection || $childFilter instanceof CollectionInputFilter) { + $childFilter = $this->convertCollectionFilters($elementOrFieldset, $inputOrFilter); + if (! $elementOrFieldset instanceof Collection || $childFilter instanceof CollectionInputFilter) { + $newFilter->add($childFilter, $name); continue; } if (! $childFilter->has(0)) { + $newFilter->add($childFilter, $name); continue; } $target = $childFilter->get(0); - $required = ! $childFieldset->allowRemove(); - $count = $required ? $childFieldset->getCount() : 0; + $required = ! $elementOrFieldset->allowRemove(); + $count = $required ? $elementOrFieldset->getCount() : 0; if ($target instanceof InputInterface) { $inputOrFilter = CollectionInput::fromInput($target, $count, ! $required); @@ -122,11 +128,11 @@ private function convertCollectionFilters( } } - $inputFilter->remove($name); - $inputFilter->add($inputOrFilter, $name); + $newFilter->add($inputOrFilter, $name); } - return $inputFilter; + $newFilter->setData($inputFilter->getRawValues()); + return $newFilter; } /** diff --git a/test/Form/Asset/InputFilterFieldset.php b/test/Form/Asset/InputFilterFieldset.php index d8f2b7e..64feb28 100644 --- a/test/Form/Asset/InputFilterFieldset.php +++ b/test/Form/Asset/InputFilterFieldset.php @@ -10,6 +10,9 @@ final class InputFilterFieldset extends Fieldset implements InputFilterProviderInterface { + /** + * @param string|null $name + */ public function __construct($name = null, array $options = []) { parent::__construct($name, $options); @@ -26,4 +29,4 @@ public function getInputFilterSpecification(): array ], ]; } -} \ No newline at end of file +} diff --git a/test/Form/FormVisitorTest.php b/test/Form/FormVisitorTest.php index d4617f7..6be1113 100644 --- a/test/Form/FormVisitorTest.php +++ b/test/Form/FormVisitorTest.php @@ -12,7 +12,6 @@ use KynxTest\Laminas\FormShape\Form\Asset\InputFilterFieldset; use Laminas\Form\Element\Collection; use Laminas\Form\Element\Email; -use Laminas\Form\Element\Hidden; use Laminas\Form\Element\Text; use Laminas\Form\Fieldset; use Laminas\Form\Form; @@ -28,6 +27,8 @@ use Psalm\Type\Atomic\TTypeAlias; use Psalm\Type\Union; +use function array_keys; + #[CoversClass(FormVisitor::class)] final class FormVisitorTest extends TestCase { @@ -255,6 +256,7 @@ public function testVisitPreservesInputOrderWhenInputIsRequired(): void $formArray = $this->visitor->visit($form, [])->getSingleAtomic(); self::assertInstanceOf(TKeyedArray::class, $formArray); $foo = $formArray->properties['foo'] ?? null; + self::assertInstanceOf(Union::class, $foo); $fooArray = $foo->getSingleAtomic(); self::assertInstanceOf(TKeyedArray::class, $fooArray);