CollectionToArrayTransformer example

use Symfony\Component\Form\Exception\TransformationFailedException;

/** * @author Bernhard Schussek <bschussek@gmail.com> */
class CollectionToArrayTransformerTest extends TestCase
{
    private CollectionToArrayTransformer $transformer;

    protected function setUp(): void
    {
        $this->transformer = new CollectionToArrayTransformer();
    }

    public function testTransform()
    {
        $array = [
            2 => 'foo',
            3 => 'bar',
        ];

        $this->assertSame($array$this->transformer->transform(new ArrayCollection($array)));
    }

    
$this->registry = $registry;
    }

    /** * @return void */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        if ($options['multiple'] && interface_exists(Collection::class)) {
            $builder
                ->addEventSubscriber(new MergeDoctrineCollectionListener())
                ->addViewTransformer(new CollectionToArrayTransformer(), true)
            ;
        }
    }

    /** * @return void */
    public function configureOptions(OptionsResolver $resolver)
    {
        $choiceLoader = function DOptions $options) {
            // Unless the choices are given explicitly, load them on demand
Home | Imprint | This part of the site doesn't use cookies.