|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Component\Intl; |
| 13 | + |
| 14 | +use Symfony\Component\Intl\Exception\MissingResourceException; |
| 15 | + |
| 16 | +/** |
| 17 | + * Gives access to currency-related ICU data. |
| 18 | + * |
| 19 | + * @author Bernhard Schussek <bschussek@gmail.com> |
| 20 | + * @author Roland Franssen <franssen.roland@gmail.com> |
| 21 | + */ |
| 22 | +final class Currencies extends ResourceBundle |
| 23 | +{ |
| 24 | + private const INDEX_SYMBOL = 0; |
| 25 | + private const INDEX_NAME = 1; |
| 26 | + private const INDEX_FRACTION_DIGITS = 0; |
| 27 | + private const INDEX_ROUNDING_INCREMENT = 1; |
| 28 | + |
| 29 | + /** |
| 30 | + * @return string[] |
| 31 | + */ |
| 32 | + public static function getCurrencyCodes(): array |
| 33 | + { |
| 34 | + return self::readEntry(['Currencies'], 'meta'); |
| 35 | + } |
| 36 | + |
| 37 | + public static function exists(string $currency): bool |
| 38 | + { |
| 39 | + try { |
| 40 | + self::readEntry(['Names', $currency, self::INDEX_NAME]); |
| 41 | + |
| 42 | + return true; |
| 43 | + } catch (MissingResourceException $e) { |
| 44 | + return false; |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + public static function getName(string $currency, string $displayLocale = null): string |
| 49 | + { |
| 50 | + return self::readEntry(['Names', $currency, self::INDEX_NAME], $displayLocale); |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * @return string[] |
| 55 | + */ |
| 56 | + public static function getNames(string $displayLocale = null): array |
| 57 | + { |
| 58 | + // ==================================================================== |
| 59 | + // For reference: It is NOT possible to return names indexed by |
| 60 | + // numeric code here, because some numeric codes map to multiple |
| 61 | + // 3-letter codes (e.g. 32 => "ARA", "ARP", "ARS") |
| 62 | + // ==================================================================== |
| 63 | + |
| 64 | + $names = self::readEntry(['Names'], $displayLocale); |
| 65 | + |
| 66 | + if ($names instanceof \Traversable) { |
| 67 | + $names = iterator_to_array($names); |
| 68 | + } |
| 69 | + |
| 70 | + array_walk($names, function (&$value) { |
| 71 | + $value = $value[self::INDEX_NAME]; |
| 72 | + }); |
| 73 | + |
| 74 | + return self::asort($names, $displayLocale); |
| 75 | + } |
| 76 | + |
| 77 | + public static function getSymbol(string $currency, string $displayLocale = null): string |
| 78 | + { |
| 79 | + return self::readEntry(['Names', $currency, self::INDEX_SYMBOL], $displayLocale); |
| 80 | + } |
| 81 | + |
| 82 | + public static function getFractionDigits(string $currency): int |
| 83 | + { |
| 84 | + try { |
| 85 | + return self::readEntry(['Meta', $currency, self::INDEX_FRACTION_DIGITS], 'meta'); |
| 86 | + } catch (MissingResourceException $e) { |
| 87 | + return self::readEntry(['Meta', 'DEFAULT', self::INDEX_FRACTION_DIGITS], 'meta'); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * @return float|int |
| 93 | + */ |
| 94 | + public static function getRoundingIncrement(string $currency) |
| 95 | + { |
| 96 | + try { |
| 97 | + return self::readEntry(['Meta', $currency, self::INDEX_ROUNDING_INCREMENT], 'meta'); |
| 98 | + } catch (MissingResourceException $e) { |
| 99 | + return self::readEntry(['Meta', 'DEFAULT', self::INDEX_ROUNDING_INCREMENT], 'meta'); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + public static function getNumericCode(string $currency): int |
| 104 | + { |
| 105 | + return self::readEntry(['Alpha3ToNumeric', $currency], 'meta'); |
| 106 | + } |
| 107 | + |
| 108 | + public static function forNumericCode(int $numericCode): array |
| 109 | + { |
| 110 | + return self::readEntry(['NumericToAlpha3', (string) $numericCode], 'meta'); |
| 111 | + } |
| 112 | + |
| 113 | + protected static function getPath(): string |
| 114 | + { |
| 115 | + return Intl::getDataDirectory().'/'.Intl::CURRENCY_DIR; |
| 116 | + } |
| 117 | +} |
0 commit comments