IcuRegionBundle.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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\Icu;
  11. use Symfony\Component\Intl\ResourceBundle\Reader\StructuredBundleReaderInterface;
  12. use Symfony\Component\Intl\ResourceBundle\RegionBundle;
  13. /**
  14. * An ICU-specific implementation of {@link \Symfony\Component\Intl\ResourceBundle\RegionBundleInterface}.
  15. *
  16. * This class normalizes the data of the ICU .res files to satisfy the contract
  17. * defined in {@link \Symfony\Component\Intl\ResourceBundle\RegionBundleInterface}.
  18. *
  19. * @author Bernhard Schussek <bschussek@gmail.com>
  20. */
  21. class IcuRegionBundle extends RegionBundle
  22. {
  23. public function __construct(StructuredBundleReaderInterface $reader)
  24. {
  25. parent::__construct(realpath(IcuData::getResourceDirectory() . '/region'), $reader);
  26. }
  27. /**
  28. * {@inheritdoc}
  29. */
  30. public function getCountryName($country, $locale = null)
  31. {
  32. if ('ZZ' === $country || ctype_digit((string) $country)) {
  33. return null;
  34. }
  35. return parent::getCountryName($country, $locale);
  36. }
  37. /**
  38. * {@inheritdoc}
  39. */
  40. public function getCountryNames($locale = null)
  41. {
  42. if (null === $locale) {
  43. $locale = \Locale::getDefault();
  44. }
  45. $countries = parent::getCountryNames($locale);
  46. // "ZZ" is the code for unknown country
  47. unset($countries['ZZ']);
  48. // Global countries (f.i. "America") have numeric codes
  49. // Countries have alphabetic codes
  50. foreach ($countries as $code => $name) {
  51. // is_int() does not work, since some numbers start with '0' and
  52. // thus are stored as strings.
  53. // The (string) cast is necessary since ctype_digit() returns false
  54. // for integers.
  55. if (ctype_digit((string) $code)) {
  56. unset($countries[$code]);
  57. }
  58. }
  59. $collator = new \Collator($locale);
  60. $collator->asort($countries);
  61. return $countries;
  62. }
  63. }