Countries.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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\Intl;
  11. use Symfony\Component\Intl\Exception\MissingResourceException;
  12. /**
  13. * Gives access to region-related ICU data.
  14. *
  15. * @author Bernhard Schussek <bschussek@gmail.com>
  16. * @author Roland Franssen <franssen.roland@gmail.com>
  17. */
  18. final class Countries extends ResourceBundle
  19. {
  20. /**
  21. * Returns all available countries.
  22. *
  23. * Countries are returned as uppercase ISO 3166 two-letter country codes.
  24. *
  25. * A full table of ISO 3166 country codes can be found here:
  26. * https://en.wikipedia.org/wiki/List_of_ISO_3166_country_codes
  27. *
  28. * This list only contains "officially assigned ISO 3166-1 alpha-2" country codes.
  29. *
  30. * @return string[] an array of canonical ISO 3166 alpha-2 country codes
  31. */
  32. public static function getCountryCodes(): array
  33. {
  34. return self::readEntry(['Regions'], 'meta');
  35. }
  36. /**
  37. * Returns all available countries (3 letters).
  38. *
  39. * Countries are returned as uppercase ISO 3166 three-letter country codes.
  40. *
  41. * This list only contains "officially assigned ISO 3166-1 alpha-3" country codes.
  42. *
  43. * @return string[] an array of canonical ISO 3166 alpha-3 country codes
  44. */
  45. public static function getAlpha3Codes(): array
  46. {
  47. return self::readEntry(['Alpha2ToAlpha3'], 'meta');
  48. }
  49. public static function getAlpha3Code(string $alpha2Code): string
  50. {
  51. return self::readEntry(['Alpha2ToAlpha3', $alpha2Code], 'meta');
  52. }
  53. public static function getAlpha2Code(string $alpha3Code): string
  54. {
  55. return self::readEntry(['Alpha3ToAlpha2', $alpha3Code], 'meta');
  56. }
  57. public static function exists(string $alpha2Code): bool
  58. {
  59. try {
  60. self::readEntry(['Names', $alpha2Code]);
  61. return true;
  62. } catch (MissingResourceException $e) {
  63. return false;
  64. }
  65. }
  66. public static function alpha3CodeExists(string $alpha3Code): bool
  67. {
  68. try {
  69. self::getAlpha2Code($alpha3Code);
  70. return true;
  71. } catch (MissingResourceException $e) {
  72. return false;
  73. }
  74. }
  75. /**
  76. * Gets the country name from its alpha2 code.
  77. *
  78. * @throws MissingResourceException if the country code does not exist
  79. */
  80. public static function getName(string $country, string $displayLocale = null): string
  81. {
  82. return self::readEntry(['Names', $country], $displayLocale);
  83. }
  84. /**
  85. * Gets the country name from its alpha3 code.
  86. *
  87. * @throws MissingResourceException if the country code does not exist
  88. */
  89. public static function getAlpha3Name(string $alpha3Code, string $displayLocale = null): string
  90. {
  91. return self::getName(self::getAlpha2Code($alpha3Code), $displayLocale);
  92. }
  93. /**
  94. * Gets the list of country names indexed with alpha2 codes as keys.
  95. *
  96. * @return string[]
  97. */
  98. public static function getNames($displayLocale = null): array
  99. {
  100. return self::asort(self::readEntry(['Names'], $displayLocale), $displayLocale);
  101. }
  102. /**
  103. * Gets the list of country names indexed with alpha3 codes as keys.
  104. *
  105. * Same as method getNames, but with alpha3 codes instead of alpha2 codes as keys.
  106. *
  107. * @return string[]
  108. */
  109. public static function getAlpha3Names($displayLocale = null): array
  110. {
  111. $alpha2Names = self::getNames($displayLocale);
  112. $alpha3Names = [];
  113. foreach ($alpha2Names as $alpha2Code => $name) {
  114. $alpha3Names[self::getAlpha3Code($alpha2Code)] = $name;
  115. }
  116. return $alpha3Names;
  117. }
  118. protected static function getPath(): string
  119. {
  120. return Intl::getDataDirectory().'/'.Intl::REGION_DIR;
  121. }
  122. }