Currencies.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 currency-related ICU data.
  14. *
  15. * @author Bernhard Schussek <bschussek@gmail.com>
  16. * @author Roland Franssen <franssen.roland@gmail.com>
  17. */
  18. final class Currencies extends ResourceBundle
  19. {
  20. private const INDEX_SYMBOL = 0;
  21. private const INDEX_NAME = 1;
  22. private const INDEX_FRACTION_DIGITS = 0;
  23. private const INDEX_ROUNDING_INCREMENT = 1;
  24. /**
  25. * @return string[]
  26. */
  27. public static function getCurrencyCodes(): array
  28. {
  29. return self::readEntry(['Currencies'], 'meta');
  30. }
  31. public static function exists(string $currency): bool
  32. {
  33. try {
  34. self::readEntry(['Names', $currency, self::INDEX_NAME]);
  35. return true;
  36. } catch (MissingResourceException $e) {
  37. return false;
  38. }
  39. }
  40. /**
  41. * @throws MissingResourceException if the currency code does not exist
  42. */
  43. public static function getName(string $currency, string $displayLocale = null): string
  44. {
  45. return self::readEntry(['Names', $currency, self::INDEX_NAME], $displayLocale);
  46. }
  47. /**
  48. * @return string[]
  49. */
  50. public static function getNames(string $displayLocale = null): array
  51. {
  52. // ====================================================================
  53. // For reference: It is NOT possible to return names indexed by
  54. // numeric code here, because some numeric codes map to multiple
  55. // 3-letter codes (e.g. 32 => "ARA", "ARP", "ARS")
  56. // ====================================================================
  57. $names = self::readEntry(['Names'], $displayLocale);
  58. if ($names instanceof \Traversable) {
  59. $names = iterator_to_array($names);
  60. }
  61. array_walk($names, function (&$value) {
  62. $value = $value[self::INDEX_NAME];
  63. });
  64. return self::asort($names, $displayLocale);
  65. }
  66. /**
  67. * @throws MissingResourceException if the currency code does not exist
  68. */
  69. public static function getSymbol(string $currency, string $displayLocale = null): string
  70. {
  71. return self::readEntry(['Names', $currency, self::INDEX_SYMBOL], $displayLocale);
  72. }
  73. public static function getFractionDigits(string $currency): int
  74. {
  75. try {
  76. return self::readEntry(['Meta', $currency, self::INDEX_FRACTION_DIGITS], 'meta');
  77. } catch (MissingResourceException $e) {
  78. return self::readEntry(['Meta', 'DEFAULT', self::INDEX_FRACTION_DIGITS], 'meta');
  79. }
  80. }
  81. /**
  82. * @return float|int
  83. */
  84. public static function getRoundingIncrement(string $currency)
  85. {
  86. try {
  87. return self::readEntry(['Meta', $currency, self::INDEX_ROUNDING_INCREMENT], 'meta');
  88. } catch (MissingResourceException $e) {
  89. return self::readEntry(['Meta', 'DEFAULT', self::INDEX_ROUNDING_INCREMENT], 'meta');
  90. }
  91. }
  92. /**
  93. * @throws MissingResourceException if the currency code has no numeric code
  94. */
  95. public static function getNumericCode(string $currency): int
  96. {
  97. return self::readEntry(['Alpha3ToNumeric', $currency], 'meta');
  98. }
  99. /**
  100. * @throws MissingResourceException if the numeric code does not exist
  101. */
  102. public static function forNumericCode(int $numericCode): array
  103. {
  104. return self::readEntry(['NumericToAlpha3', (string) $numericCode], 'meta');
  105. }
  106. protected static function getPath(): string
  107. {
  108. return Intl::getDataDirectory().'/'.Intl::CURRENCY_DIR;
  109. }
  110. }