<?php
namespace App\Form\App\Type;
use Symfony\Component\Form\ChoiceList\ArrayChoiceList;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Intl\Intl;
use Symfony\Component\Form\Extension\Core\Type\CountryType as BaseCountryType;
class CountryType extends BaseCountryType
{
/**
* Country loaded choice list.
*
* The choices are lazy loaded and generated from the Intl component.
*
* {@link \Symfony\Component\Intl\Intl::getRegionBundle()}.
*
* @var ArrayChoiceList
*/
protected $choiceList;
/**
* @var string
*/
private $locale;
/**
* CountryType constructor.
* @param RequestStack $requestStack
*/
public function __construct(RequestStack $requestStack)
{
$this->locale = $requestStack->getMasterRequest()->getLocale();
}
/**
* {@inheritdoc}
*/
public function loadChoiceList($value = null)
{
if (null !== $this->choiceList) {
return $this->choiceList;
}
$locale = 'sr' !== $this->locale ? $this->locale : 'sr_Latn_YU';
return $this->choiceList = new ArrayChoiceList(array_flip(Intl::getRegionBundle()->getCountryNames($locale)), $value);
}
}