vendor/commerceguys/addressing/src/Repository/CountryRepository.php line 30

Open in your IDE?
  1. <?php
  2. namespace CommerceGuys\Addressing\Repository;
  3. /**
  4.  * Provides the country list, sourced from commerceguys/intl or symfony/intl.
  5.  *
  6.  * Choosing the source at runtime allows integrations (such as the symfony
  7.  * bundle) to stay agnostic about the intl library they need.
  8.  */
  9. class CountryRepository implements CountryRepositoryInterface
  10. {
  11.     /**
  12.      * The country repository, if commerceguys/intl is used.
  13.      *
  14.      * @var \CommerceGuys\Intl\Country\CountryRepository
  15.      */
  16.     protected $countryRepository;
  17.     /**
  18.      * The region bundle, if symfony/intl is used.
  19.      *
  20.      * @var \Symfony\Component\Intl\ResourceBundle\RegionBundle
  21.      */
  22.     protected $regionBundle;
  23.     /**
  24.      * Creates a CountryRepository instance.
  25.      */
  26.     public function __construct()
  27.     {
  28.         if (class_exists('\CommerceGuys\Intl\Country\CountryRepository')) {
  29.             $this->countryRepository = new \CommerceGuys\Intl\Country\CountryRepository();
  30.         } elseif (class_exists('\Symfony\Component\Intl\Intl')) {
  31.             $this->regionBundle = \Symfony\Component\Intl\Intl::getRegionBundle();
  32.         } else {
  33.             throw new \RuntimeException('No source of country data found: symfony/intl or commerceguys/intl must be installed.');
  34.         }
  35.     }
  36.     /**
  37.      * {@inheritdoc}
  38.      */
  39.     public function getList($locale null)
  40.     {
  41.         if ($this->countryRepository) {
  42.             $countryNames $this->countryRepository->getList($locale);
  43.         } else {
  44.             $locale $this->canonicalizeLocale($locale);
  45.             $countryNames $this->regionBundle->getCountryNames($locale);
  46.         }
  47.         return $countryNames;
  48.     }
  49.     /**
  50.      * Canonicalize the given locale.
  51.      *
  52.      * Note: commerceguys/intl does this internally, so this method only
  53.      * needs to be invoked when using symfony/intl.
  54.      *
  55.      * @param string $locale The locale.
  56.      *
  57.      * @return string The canonicalized locale.
  58.      */
  59.     protected function canonicalizeLocale($locale null)
  60.     {
  61.         if (is_null($locale)) {
  62.             return $locale;
  63.         }
  64.         $locale str_replace('-''_'strtolower($locale));
  65.         $localeParts explode('_'$locale);
  66.         foreach ($localeParts as $index => $part) {
  67.             if ($index === 0) {
  68.                 // The language code should stay lowercase.
  69.                 continue;
  70.             }
  71.             if (strlen($part) == 4) {
  72.                 // Script code.
  73.                 $localeParts[$index] = ucfirst($part);
  74.             } else {
  75.                 // Country or variant code.
  76.                 $localeParts[$index] = strtoupper($part);
  77.             }
  78.         }
  79.         return implode('_'$localeParts);
  80.     }
  81. }