Skip to content

Commit

Permalink
Preserve order of inputs in generated array shape
Browse files Browse the repository at this point in the history
  • Loading branch information
matt committed Feb 24, 2024
1 parent 2062293 commit b030a87
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 12 deletions.
26 changes: 16 additions & 10 deletions src/Form/FormVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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;
}

/**
Expand Down
5 changes: 4 additions & 1 deletion test/Form/Asset/InputFilterFieldset.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -26,4 +29,4 @@ public function getInputFilterSpecification(): array
],
];
}
}
}
4 changes: 3 additions & 1 deletion test/Form/FormVisitorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
{
Expand Down Expand Up @@ -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);

Expand Down

0 comments on commit b030a87

Please sign in to comment.