StubLocale.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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\Locale\Stub;
  11. use Symfony\Component\Icu\IcuData;
  12. use Symfony\Component\Intl\Intl;
  13. use Symfony\Component\Intl\Locale\Locale;
  14. /**
  15. * Alias of {@link \Symfony\Component\Intl\Locale\Locale}.
  16. *
  17. * @author Bernhard Schussek <bschussek@gmail.com>
  18. *
  19. * @deprecated Deprecated since version 2.3, to be removed in 3.0. Use
  20. * {@link \Symfony\Component\Intl\Locale\Locale} and
  21. * {@link \Symfony\Component\Intl\Intl} instead.
  22. */
  23. class StubLocale extends Locale
  24. {
  25. /**
  26. * Caches the currencies
  27. *
  28. * @var array
  29. */
  30. protected static $currencies;
  31. /**
  32. * Caches the currencies names
  33. *
  34. * @var array
  35. */
  36. protected static $currenciesNames;
  37. /**
  38. * Returns the currencies data
  39. *
  40. * @param string $locale
  41. *
  42. * @return array The currencies data
  43. */
  44. public static function getCurrenciesData($locale)
  45. {
  46. if (null === self::$currencies) {
  47. self::prepareCurrencies($locale);
  48. }
  49. return self::$currencies;
  50. }
  51. /**
  52. * Returns the currencies names for a locale
  53. *
  54. * @param string $locale The locale to use for the currencies names
  55. *
  56. * @return array The currencies names with their codes as keys
  57. *
  58. * @throws \InvalidArgumentException When the locale is different than 'en'
  59. */
  60. public static function getDisplayCurrencies($locale)
  61. {
  62. if (null === self::$currenciesNames) {
  63. self::prepareCurrencies($locale);
  64. }
  65. return self::$currenciesNames;
  66. }
  67. /**
  68. * Returns all available currencies codes
  69. *
  70. * @return array The currencies codes
  71. */
  72. public static function getCurrencies()
  73. {
  74. return array_keys(self::getCurrenciesData(self::getDefault()));
  75. }
  76. public static function getDataDirectory()
  77. {
  78. return IcuData::getResourceDirectory();
  79. }
  80. private static function prepareCurrencies($locale)
  81. {
  82. self::$currencies = array();
  83. self::$currenciesNames = array();
  84. $bundle = Intl::getCurrencyBundle();
  85. foreach ($bundle->getCurrencyNames($locale) as $currency => $name) {
  86. self::$currencies[$currency] = array(
  87. 'name' => $name,
  88. 'symbol' => $bundle->getCurrencySymbol($currency, $locale),
  89. 'fractionDigits' => $bundle->getFractionDigits($currency),
  90. 'roundingIncrement' => $bundle->getRoundingIncrement($currency)
  91. );
  92. self::$currenciesNames[$currency] = $name;
  93. }
  94. }
  95. }