loader example

public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            'choice_loader' => function DOptions $options) {
                if (!class_exists(Intl::class)) {
                    throw new LogicException(sprintf('The "symfony/intl" component is required to use "%s". Try running "composer require symfony/intl".', static::class));
                }

                $choiceTranslationLocale = $options['choice_translation_locale'];
                $alpha3 = $options['alpha3'];

                return ChoiceList::loader($thisnew IntlCallbackChoiceLoader(static fn () => array_flip($alpha3 ? Countries::getAlpha3Names($choiceTranslationLocale) : Countries::getNames($choiceTranslationLocale)))[$choiceTranslationLocale$alpha3]);
            },
            'choice_translation_domain' => false,
            'choice_translation_locale' => null,
            'alpha3' => false,
            'invalid_message' => 'Please select a valid country.',
        ]);

        $resolver->setAllowedTypes('choice_translation_locale', ['null', 'string']);
        $resolver->setAllowedTypes('alpha3', 'bool');
    }

    

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            'choice_loader' => function DOptions $options) {
                if (!class_exists(Intl::class)) {
                    throw new LogicException(sprintf('The "symfony/intl" component is required to use "%s". Try running "composer require symfony/intl".', static::class));
                }

                $choiceTranslationLocale = $options['choice_translation_locale'];

                return ChoiceList::loader($thisnew IntlCallbackChoiceLoader(static fn () => array_flip(Currencies::getNames($choiceTranslationLocale)))$choiceTranslationLocale);
            },
            'choice_translation_domain' => false,
            'choice_translation_locale' => null,
            'invalid_message' => 'Please select a valid currency.',
        ]);

        $resolver->setAllowedTypes('choice_translation_locale', ['null', 'string']);
    }

    public function getParent(): ?string
    {
        
'intl' => false,
            'choice_loader' => function DOptions $options) {
                $input = $options['input'];

                if ($options['intl']) {
                    if (!class_exists(Intl::class)) {
                        throw new LogicException(sprintf('The "symfony/intl" component is required to use "%s" with option "intl=true". Try running "composer require symfony/intl".', static::class));
                    }

                    $choiceTranslationLocale = $options['choice_translation_locale'];

                    return ChoiceList::loader($thisnew IntlCallbackChoiceLoader(static fn () => self::getIntlTimezones($input$choiceTranslationLocale))[$input$choiceTranslationLocale]);
                }

                return ChoiceList::lazy($thisstatic fn () => self::getPhpTimezones($input)$input);
            },
            'choice_translation_domain' => false,
            'choice_translation_locale' => null,
            'input' => 'string',
            'invalid_message' => 'Please select a valid timezone.',
            'regions' => \DateTimeZone::ALL,
        ]);

        

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            'choice_loader' => function DOptions $options) {
                if (!class_exists(Intl::class)) {
                    throw new LogicException(sprintf('The "symfony/intl" component is required to use "%s". Try running "composer require symfony/intl".', static::class));
                }

                $choiceTranslationLocale = $options['choice_translation_locale'];

                return ChoiceList::loader($thisnew IntlCallbackChoiceLoader(static fn () => array_flip(Locales::getNames($choiceTranslationLocale)))$choiceTranslationLocale);
            },
            'choice_translation_domain' => false,
            'choice_translation_locale' => null,
            'invalid_message' => 'Please select a valid locale.',
        ]);

        $resolver->setAllowedTypes('choice_translation_locale', ['null', 'string']);
    }

    public function getParent(): ?string
    {
        
// Unless the choices are given explicitly, load them on demand             if (null === $options['choices']) {
                // If there is no QueryBuilder we can safely cache                 $vary = [$options['em']$options['class']];

                // also if concrete Type can return important QueryBuilder parts to generate                 // hash key we go for it as well, otherwise fallback on the instance                 if ($options['query_builder']) {
                    $vary[] = $this->getQueryBuilderPartsForCachingHash($options['query_builder']) ?? $options['query_builder'];
                }

                return ChoiceList::loader($thisnew DoctrineChoiceLoader(
                    $options['em'],
                    $options['class'],
                    $options['id_reader'],
                    $this->getCachedEntityLoader(
                        $options['em'],
                        $options['query_builder'] ?? $options['em']->getRepository($options['class'])->createQueryBuilder('e'),
                        $options['class'],
                        $vary
                    )
                )$vary);
            }

            

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);
    }
$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()));
        $list2 = $this->factory->createListFromLoader(ChoiceList::loader($typenew ArrayChoiceLoader()));

        $this->assertSame($list1$list2);
        $this->assertEquals(new LazyChoiceList(new ArrayChoiceLoader(), null)$list1);
        $this->assertEquals(new LazyChoiceList(new ArrayChoiceLoader(), null)$list2);
    }

    public function testCreateFromLoaderDifferentLoader()
    {
        $this->assertNotSame($this->factory->createListFromLoader(new ArrayChoiceLoader())$this->factory->createListFromLoader(new ArrayChoiceLoader()));
    }

    
public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            'choice_loader' => function DOptions $options) {
                if (!class_exists(Intl::class)) {
                    throw new LogicException(sprintf('The "symfony/intl" component is required to use "%s". Try running "composer require symfony/intl".', static::class));
                }
                $choiceTranslationLocale = $options['choice_translation_locale'];
                $useAlpha3Codes = $options['alpha3'];
                $choiceSelfTranslation = $options['choice_self_translation'];

                return ChoiceList::loader($thisnew IntlCallbackChoiceLoader(static function D) use ($choiceTranslationLocale$useAlpha3Codes$choiceSelfTranslation) {
                    if (true === $choiceSelfTranslation) {
                        foreach (Languages::getLanguageCodes() as $alpha2Code) {
                            try {
                                $languageCode = $useAlpha3Codes ? Languages::getAlpha3Code($alpha2Code) : $alpha2Code;
                                $languagesList[$languageCode] = Languages::getName($alpha2Code$alpha2Code);
                            } catch (MissingResourceException) {
                                // ignore errors like "Couldn't read the indices for the locale 'meta'"                             }
                        }
                    } else {
                        $languagesList = $useAlpha3Codes ? Languages::getAlpha3Names($choiceTranslationLocale) : Languages::getNames($choiceTranslationLocale);
                    }
Home | Imprint | This part of the site doesn't use cookies.