vendor/sylius/sylius/src/Sylius/Bundle/AddressingBundle/Form/Type/CountryChoiceType.php line 25

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Sylius package.
  4.  *
  5.  * (c) Paweł Jędrzejewski
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. declare(strict_types=1);
  11. namespace Sylius\Bundle\AddressingBundle\Form\Type;
  12. use Sylius\Component\Addressing\Model\CountryInterface;
  13. use Sylius\Component\Resource\Repository\RepositoryInterface;
  14. use Symfony\Bridge\Doctrine\Form\DataTransformer\CollectionToArrayTransformer;
  15. use Symfony\Component\Form\AbstractType;
  16. use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
  17. use Symfony\Component\Form\FormBuilderInterface;
  18. use Symfony\Component\OptionsResolver\Options;
  19. use Symfony\Component\OptionsResolver\OptionsResolver;
  20. final class CountryChoiceType extends AbstractType
  21. {
  22.     public function __construct(private RepositoryInterface $countryRepository)
  23.     {
  24.     }
  25.     public function buildForm(FormBuilderInterface $builder, array $options): void
  26.     {
  27.         if ($options['multiple']) {
  28.             $builder->addModelTransformer(new CollectionToArrayTransformer());
  29.         }
  30.     }
  31.     public function configureOptions(OptionsResolver $resolver): void
  32.     {
  33.         $resolver
  34.             ->setDefaults([
  35.                 'choice_filter' => null,
  36.                 'choices' => function (Options $options): iterable {
  37.                     if ($options['enabled'] === true) {
  38.                         return  $this->countryRepository->findBy(['enabled' => $options['enabled']]);
  39.                     }
  40.                     return $this->countryRepository->findAll();
  41.                 },
  42.                 'choice_value' => 'code',
  43.                 'choice_label' => 'name',
  44.                 'choice_translation_domain' => false,
  45.                 'enabled' => true,
  46.                 'label' => 'sylius.form.address.country',
  47.                 'placeholder' => 'sylius.form.country.select',
  48.             ])
  49.             ->setAllowedTypes('choice_filter', ['null''callable'])
  50.             ->setNormalizer('choices', static function (Options $options, array $countries): array {
  51.                 if ($options['choice_filter']) {
  52.                     $countries array_filter($countries$options['choice_filter']);
  53.                 }
  54.                 usort($countries, static fn (CountryInterface $firstCountryCountryInterface $secondCountry): int => $firstCountry->getName() <=> $secondCountry->getName());
  55.                 return $countries;
  56.             })
  57.         ;
  58.     }
  59.     public function getParent(): string
  60.     {
  61.         return ChoiceType::class;
  62.     }
  63.     public function getBlockPrefix(): string
  64.     {
  65.         return 'sylius_country_choice';
  66.     }
  67. }