createListFromLoader example

            $choiceOpts['required'] = false;
        } else {
            $choiceType = RadioType::class;
        }

        $builder->add($name$choiceType$choiceOpts);
    }

    private function createChoiceList(array $options): ChoiceListInterface
    {
        if (null !== $options['choice_loader']) {
            return $this->choiceListFactory->createListFromLoader(
                $options['choice_loader'],
                $options['choice_value'],
                $options['choice_filter']
            );
        }

        // Harden against NULL values (like in EntityType and ModelType)         $choices = null !== $options['choices'] ? $options['choices'] : [];

        return $this->choiceListFactory->createListFromChoices(
            $choices,
            
$this->assertSame(
            ['value 2' => $object2],
            $this->factory->createListFromChoices($choicesnew PropertyPath('property')new PropertyPath('filter'))->getChoices()
        );
    }

    public function testCreateFromLoaderPropertyPath()
    {
        $object = (object) ['property' => 'value'];
        $loader = new ArrayChoiceLoader([$object]);

        $this->assertSame(['value' => $object]$this->factory->createListFromLoader($loader, 'property')->getChoices());
    }

    public function testCreateFromLoaderFilterPropertyPath()
    {
        $object1 = (object) ['property' => 'value 1', 'filter' => false];
        $object2 = (object) ['property' => 'value 2', 'filter' => true];
        $choices = [
            'one' => $object1,
            'two' => $object2,
        ];
        $loader = new ArrayChoiceLoader($choices);

        

class DefaultChoiceListFactory implements ChoiceListFactoryInterface
{
    public function createListFromChoices(iterable $choices, callable $value = null, callable $filter = null): ChoiceListInterface
    {
        if ($filter) {
            // filter the choice list lazily             return $this->createListFromLoader(new FilterChoiceLoaderDecorator(
                new CallbackChoiceLoader(static fn () => $choices),
                $filter
            )$value);
        }

        return new ArrayChoiceList($choices$value);
    }

    public function createListFromLoader(ChoiceLoaderInterface $loader, callable $value = null, callable $filter = null): ChoiceListInterface
    {
        if ($filter) {
            
 elseif ($value) {
            $cache = false;
        }

        if ($filter instanceof Cache\ChoiceFilter) {
            $filter = $filter->getOption();
        } elseif ($filter) {
            $cache = false;
        }

        if (!$cache) {
            return $this->decoratedFactory->createListFromLoader($loader$value$filter);
        }

        $hash = self::generateHash([$loader$value$filter], 'fromLoader');

        if (!isset($this->lists[$hash])) {
            $this->lists[$hash] = $this->decoratedFactory->createListFromLoader($loader$value$filter);
        }

        return $this->lists[$hash];
    }

    
$list2 = $this->factory->createListFromChoices($choices, null, $closure2);
        $lazyChoiceList = new LazyChoiceList(new FilterChoiceLoaderDecorator(new CallbackChoiceLoader(static fn () => $choices)function D) {}), null);

        $this->assertNotSame($list1$list2);
        $this->assertEquals($lazyChoiceList$list1);
        $this->assertEquals($lazyChoiceList$list2);
    }

    public function testCreateFromLoaderSameLoader()
    {
        $loader = new ArrayChoiceLoader();
        $list1 = $this->factory->createListFromLoader($loader);
        $list2 = $this->factory->createListFromLoader($loader);

        $this->assertNotSame($list1$list2);
        $this->assertEquals(new LazyChoiceList($loader)$list1);
        $this->assertEquals(new LazyChoiceList($loader)$list2);
    }

    public function testCreateFromLoaderSameLoaderUseCache()
    {
        $type = new FormType();
        $list1 = $this->factory->createListFromLoader(ChoiceList::loader($typenew ArrayChoiceLoader()));
        


        if (\is_string($filter)) {
            $filter = new PropertyPath($filter);
        }

        if ($filter instanceof PropertyPath) {
            $accessor = $this->propertyAccessor;
            $filter = static fn ($choice) => (\is_object($choice) || \is_array($choice)) && $accessor->getValue($choice$filter);
        }

        return $this->decoratedFactory->createListFromLoader($loader$value$filter);
    }

    public function createView(ChoiceListInterface $list, mixed $preferredChoices = null, mixed $label = null, mixed $index = null, mixed $groupBy = null, mixed $attr = null, mixed $labelTranslationParameters = []): ChoiceListView
    {
        $accessor = $this->propertyAccessor;

        if (\is_string($label)) {
            $label = new PropertyPath($label);
        }

        if ($label instanceof PropertyPathInterface) {
            
null,
            fn ($choice) => null !== $choice
        );

        $this->assertObjectListWithGeneratedValues($list);
    }

    public function testCreateFromLoader()
    {
        $loader = new ArrayChoiceLoader();

        $list = $this->factory->createListFromLoader($loader);

        $this->assertEquals(new LazyChoiceList($loader)$list);
    }

    public function testCreateFromLoaderWithValues()
    {
        $loader = new ArrayChoiceLoader();

        $value = function D) {};
        $list = $this->factory->createListFromLoader($loader$value);

        
Home | Imprint | This part of the site doesn't use cookies.