vendor/symfony/symfony/src/Symfony/Component/Form/Extension/Core/Type/CountryType.php line 21

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <fabien@symfony.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Symfony\Component\Form\Extension\Core\Type;
  11. use Symfony\Component\Form\AbstractType;
  12. use Symfony\Component\Form\ChoiceList\ArrayChoiceList;
  13. use Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface;
  14. use Symfony\Component\Intl\Intl;
  15. use Symfony\Component\OptionsResolver\Options;
  16. use Symfony\Component\OptionsResolver\OptionsResolver;
  17. class CountryType extends AbstractType implements ChoiceLoaderInterface
  18. {
  19.     /**
  20.      * Country loaded choice list.
  21.      *
  22.      * The choices are lazy loaded and generated from the Intl component.
  23.      *
  24.      * {@link \Symfony\Component\Intl\Intl::getRegionBundle()}.
  25.      *
  26.      * @var ArrayChoiceList
  27.      */
  28.     private $choiceList;
  29.     /**
  30.      * {@inheritdoc}
  31.      */
  32.     public function configureOptions(OptionsResolver $resolver)
  33.     {
  34.         $resolver->setDefaults([
  35.             'choice_loader' => function (Options $options) {
  36.                 if ($options['choices']) {
  37.                     @trigger_error(sprintf('Using the "choices" option in %s has been deprecated since Symfony 3.3 and will be ignored in 4.0. Override the "choice_loader" option instead or set it to null.'__CLASS__), E_USER_DEPRECATED);
  38.                     return null;
  39.                 }
  40.                 return $this;
  41.             },
  42.             'choice_translation_domain' => false,
  43.         ]);
  44.     }
  45.     /**
  46.      * {@inheritdoc}
  47.      */
  48.     public function getParent()
  49.     {
  50.         return __NAMESPACE__.'\ChoiceType';
  51.     }
  52.     /**
  53.      * {@inheritdoc}
  54.      */
  55.     public function getBlockPrefix()
  56.     {
  57.         return 'country';
  58.     }
  59.     /**
  60.      * {@inheritdoc}
  61.      */
  62.     public function loadChoiceList($value null)
  63.     {
  64.         if (null !== $this->choiceList) {
  65.             return $this->choiceList;
  66.         }
  67.         return $this->choiceList = new ArrayChoiceList(array_flip(Intl::getRegionBundle()->getCountryNames()), $value);
  68.     }
  69.     /**
  70.      * {@inheritdoc}
  71.      */
  72.     public function loadChoicesForValues(array $values$value null)
  73.     {
  74.         // Optimize
  75.         $values array_filter($values);
  76.         if (empty($values)) {
  77.             return [];
  78.         }
  79.         return $this->loadChoiceList($value)->getChoicesForValues($values);
  80.     }
  81.     /**
  82.      * {@inheritdoc}
  83.      */
  84.     public function loadValuesForChoices(array $choices$value null)
  85.     {
  86.         // Optimize
  87.         $choices array_filter($choices);
  88.         if (empty($choices)) {
  89.             return [];
  90.         }
  91.         // If no callable is set, choices are the same as values
  92.         if (null === $value) {
  93.             return $choices;
  94.         }
  95.         return $this->loadChoiceList($value)->getValuesForChoices($choices);
  96.     }
  97. }