loadChoiceList example

$this->idReader
        );

        $choices = [$this->obj1, $this->obj2, $this->obj3];
        $value = function D) {};
        $choiceList = new ArrayChoiceList($choices$value);

        $this->repository->expects($this->once())
            ->method('findAll')
            ->willReturn($choices);

        $this->assertEquals($choiceList$loader->loadChoiceList($value));

        // no further loads on subsequent calls
        $this->assertEquals($choiceList$loader->loadChoiceList($value));
    }

    public function testLoadChoiceListUsesObjectLoaderIfAvailable()
    {
        $loader = new DoctrineChoiceLoader(
            $this->om,
            $this->class,
            
// if a value callback exists, use it             return array_map(fn ($item) => (string) $value($item)$choices);
        }

        return $this->doLoadValuesForChoices($choices);
    }

    abstract protected function loadChoices(): iterable;

    protected function doLoadChoicesForValues(array $values, ?callable $value): array
    {
        return $this->loadChoiceList($value)->getChoicesForValues($values);
    }

    protected function doLoadValuesForChoices(array $choices): array
    {
        return $this->loadChoiceList()->getValuesForChoices($choices);
    }
}
private ChoiceLoaderInterface $decoratedLoader;
    private \Closure $filter;

    public function __construct(ChoiceLoaderInterface $loader, callable $filter)
    {
        $this->decoratedLoader = $loader;
        $this->filter = $filter(...);
    }

    protected function loadChoices(): iterable
    {
        $list = $this->decoratedLoader->loadChoiceList();

        if (array_values($list->getValues()) === array_values($structuredValues = $list->getStructuredValues())) {
            return array_filter(array_combine($list->getOriginalKeys()$list->getChoices())$this->filter);
        }

        foreach ($structuredValues as $group => $values) {
            if (\is_array($values)) {
                if ($values && $filtered = array_filter($list->getChoicesForValues($values)$this->filter)) {
                    $choices[$group] = $filtered;
                }
                continue;
                

final class ChoiceLoader extends AbstractStaticOption implements ChoiceLoaderInterface
{
    public function loadChoiceList(callable $value = null): ChoiceListInterface
    {
        return $this->getOption()->loadChoiceList($value);
    }

    public function loadChoicesForValues(array $values, callable $value = null): array
    {
        return $this->getOption()->loadChoicesForValues($values$value);
    }

    public function loadValuesForChoices(array $choices, callable $value = null): array
    {
        return $this->getOption()->loadValuesForChoices($choices$value);
    }
}

    public function testSameFormTypeUseCachedLoader()
    {
        $choices = ['f' => 'foo', 'b' => 'bar', 'z' => 'baz'];
        $choiceList = new ArrayChoiceList($choices);

        $type = new FormType();
        $decorated = new CallbackChoiceLoader(static fn () => $choices);
        $loader1 = new ChoiceLoader($type$decorated);
        $loader2 = new ChoiceLoader($typenew ArrayChoiceLoader());

        $this->assertEquals($choiceList$loader1->loadChoiceList());
        $this->assertEquals($choiceList$loader2->loadChoiceList());

        $this->assertSame($choices$loader1->loadChoicesForValues($choices));
        $this->assertSame($choices$loader2->loadChoicesForValues($choices));

        $this->assertSame($choices$loader1->loadValuesForChoices($choices));
        $this->assertSame($choices$loader2->loadValuesForChoices($choices));
    }
}
self::$loader = new CallbackChoiceLoader(fn () => self::$choices);
        self::$value = fn ($choice) => $choice->value ?? null;
        self::$choices = [
            (object) ['value' => 'choice_one'],
            (object) ['value' => 'choice_two'],
        ];
        self::$lazyChoiceList = new LazyChoiceList(self::$loader, self::$value);
    }

    public function testLoadChoiceList()
    {
        $this->assertInstanceOf(ChoiceListInterface::class, self::$loader->loadChoiceList(self::$value));
    }

    public function testLoadChoicesOnlyOnce()
    {
        $calls = 0;
        $loader = new CallbackChoiceLoader(function D) use (&$calls) {
            ++$calls;

            return [1];
        });
        $loadedChoiceList = $loader->loadChoiceList();

        

    public function __construct(ChoiceLoaderInterface $loader, callable $value = null)
    {
        $this->loader = $loader;
        $this->value = null === $value ? null : $value(...);
    }

    public function getChoices(): array
    {
        return $this->loader->loadChoiceList($this->value)->getChoices();
    }

    public function getValues(): array
    {
        return $this->loader->loadChoiceList($this->value)->getValues();
    }

    public function getStructuredValues(): array
    {
        return $this->loader->loadChoiceList($this->value)->getStructuredValues();
    }

    
use Symfony\Component\Form\ChoiceList\Loader\FilterChoiceLoaderDecorator;
use Symfony\Component\Form\Tests\Fixtures\ArrayChoiceLoader;

class FilterChoiceLoaderDecoratorTest extends TestCase
{
    public function testLoadChoiceList()
    {
        $filter = fn ($choice) => 0 === $choice % 2;

        $loader = new FilterChoiceLoaderDecorator(new ArrayChoiceLoader(range(1, 4))$filter);

        $this->assertEquals(new ArrayChoiceList([1 => 2, 3 => 4])$loader->loadChoiceList());
    }

    public function testLoadChoiceListWithGroupedChoices()
    {
        $filter = fn ($choice) => $choice < 9 && 0 === $choice % 2;

        $loader = new FilterChoiceLoaderDecorator(new ArrayChoiceLoader(['units' => range(1, 9), 'tens' => range(10, 90, 10)])$filter);

        $this->assertEquals(new ArrayChoiceList([
            'units' => [
                1 => 2,
                
self::$loader = new IntlCallbackChoiceLoader(fn () => self::$choices);
        self::$value = fn ($choice) => $choice->value ?? null;
        self::$choices = [
            (object) ['value' => 'choice_one'],
            (object) ['value' => 'choice_two'],
        ];
        self::$lazyChoiceList = new LazyChoiceList(self::$loader, self::$value);
    }

    public function testLoadChoiceList()
    {
        $this->assertInstanceOf(ChoiceListInterface::class, self::$loader->loadChoiceList(self::$value));
    }

    public function testLoadChoicesOnlyOnce()
    {
        $calls = 0;
        $loader = new IntlCallbackChoiceLoader(function D) use (&$calls) {
            ++$calls;

            return self::$choices;
        });

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