src/Form/App/Type/CountryType.php line 34

Open in your IDE?
  1. <?php
  2. namespace App\Form\App\Type;
  3. use Symfony\Component\Form\ChoiceList\ArrayChoiceList;
  4. use Symfony\Component\HttpFoundation\RequestStack;
  5. use Symfony\Component\Intl\Intl;
  6. use Symfony\Component\Form\Extension\Core\Type\CountryType as BaseCountryType;
  7. class CountryType extends BaseCountryType
  8. {
  9. /**
  10. * Country loaded choice list.
  11. *
  12. * The choices are lazy loaded and generated from the Intl component.
  13. *
  14. * {@link \Symfony\Component\Intl\Intl::getRegionBundle()}.
  15. *
  16. * @var ArrayChoiceList
  17. */
  18. protected $choiceList;
  19. /**
  20. * @var string
  21. */
  22. private $locale;
  23. /**
  24. * CountryType constructor.
  25. * @param RequestStack $requestStack
  26. */
  27. public function __construct(RequestStack $requestStack)
  28. {
  29. $this->locale = $requestStack->getMasterRequest()->getLocale();
  30. }
  31. /**
  32. * {@inheritdoc}
  33. */
  34. public function loadChoiceList($value = null)
  35. {
  36. if (null !== $this->choiceList) {
  37. return $this->choiceList;
  38. }
  39. $locale = 'sr' !== $this->locale ? $this->locale : 'sr_Latn_YU';
  40. return $this->choiceList = new ArrayChoiceList(array_flip(Intl::getRegionBundle()->getCountryNames($locale)), $value);
  41. }
  42. }