CallbackChoiceLoader example


class CallbackChoiceLoaderTest extends TestCase
{
    private static CallbackChoiceLoader $loader;
    private static \Closure $value;
    private static array $choices;
    private static array $choiceValues = ['choice_one', 'choice_two'];
    private static LazyChoiceList $lazyChoiceList;

    public static function setUpBeforeClass(): void
    {
        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));
    }
$this->assertNotSame($list1$list2);
        $this->assertEquals(new ArrayChoiceList($choices$closure1)$list1);
        $this->assertEquals(new ArrayChoiceList($choices$closure2)$list2);
    }

    public function testCreateFromChoicesSameFilterClosure()
    {
        $choices = [1];
        $filter = function D) {};
        $list1 = $this->factory->createListFromChoices($choices, null, $filter);
        $list2 = $this->factory->createListFromChoices($choices, null, $filter);
        $lazyChoiceList = new LazyChoiceList(new FilterChoiceLoaderDecorator(new CallbackChoiceLoader(static fn () => $choices)$filter), null);

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

    public function testCreateFromChoicesSameFilterClosureUseCache()
    {
        $choices = [1];
        $formType = new FormType();
        $filterCallback = function D) {};
        
use Symfony\Component\Form\Extension\Core\Type\FormType;
use Symfony\Component\Form\Tests\Fixtures\ArrayChoiceLoader;

class ChoiceLoaderTest extends TestCase
{
    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));
    }
$this->assertEquals(['Symfony' => new ChoiceGroupView('Symfony', [
            new ChoiceView('a', 'a', 'Bernhard'),
            new ChoiceView('b', 'b', 'Fabien'),
            new ChoiceView('c', 'c', 'Kris'),
        ])]$form->createView()->vars['choices']);
    }

    public function testFilteredChoiceLoader()
    {
        $form = $this->factory->create(static::TESTED_TYPE, null, [
            'choice_loader' => new CallbackChoiceLoader(fn () => $this->choices),
            'choice_filter' => fn ($choice) => \in_array($choicerange('a', 'c'), true),
        ]);

        $this->assertEquals([
            new ChoiceView('a', 'a', 'Bernhard'),
            new ChoiceView('b', 'b', 'Fabien'),
            new ChoiceView('c', 'c', 'Kris'),
        ]$form->createView()->vars['choices']);
    }

    public function testWithSameLoaderAndDifferentChoiceValueCallbacks()
    {

final class ChoiceList
{
    /** * Creates a cacheable loader from any callable providing iterable choices. * * @param callable $choices A callable that must return iterable choices or grouped choices * @param mixed $vary Dynamic data used to compute a unique hash when caching the loader */
    public static function lazy(FormTypeInterface|FormTypeExtensionInterface $formType, callable $choices, mixed $vary = null): ChoiceLoader
    {
        return self::loader($formTypenew CallbackChoiceLoader($choices)$vary);
    }

    /** * Decorates a loader to make it cacheable. * * @param ChoiceLoaderInterface $loader A loader responsible for creating loading choices or grouped choices * @param mixed $vary Dynamic data used to compute a unique hash when caching the loader */
    public static function loader(FormTypeInterface|FormTypeExtensionInterface $formType, ChoiceLoaderInterface $loader, mixed $vary = null): ChoiceLoader
    {
        return new ChoiceLoader($formType$loader$vary);
    }

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) {
            $loader = new FilterChoiceLoaderDecorator($loader$filter);
        }
Home | Imprint | This part of the site doesn't use cookies.