CurrencyDataProvider.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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\Data\Provider;
  11. use Symfony\Component\Intl\Exception\MissingResourceException;
  12. use Symfony\Component\Intl\Locale;
  13. use Symfony\Component\Intl\Data\Bundle\Reader\BundleEntryReaderInterface;
  14. /**
  15. * Data provider for currency-related data.
  16. *
  17. * @author Bernhard Schussek <bschussek@gmail.com>
  18. *
  19. * @internal
  20. */
  21. class CurrencyDataProvider
  22. {
  23. const INDEX_SYMBOL = 0;
  24. const INDEX_NAME = 1;
  25. const INDEX_FRACTION_DIGITS = 0;
  26. const INDEX_ROUNDING_INCREMENT = 1;
  27. /**
  28. * @var string
  29. */
  30. private $path;
  31. /**
  32. * @var BundleEntryReaderInterface
  33. */
  34. private $reader;
  35. /**
  36. * Creates a data provider that reads currency-related data from a
  37. * resource bundle.
  38. *
  39. * @param string $path The path to the resource bundle
  40. * @param BundleEntryReaderInterface $reader The reader for reading the resource bundle
  41. */
  42. public function __construct($path, BundleEntryReaderInterface $reader)
  43. {
  44. $this->path = $path;
  45. $this->reader = $reader;
  46. }
  47. public function getCurrencies()
  48. {
  49. return $this->reader->readEntry($this->path, 'meta', array('Currencies'));
  50. }
  51. public function getSymbol($currency, $displayLocale = null)
  52. {
  53. if (null === $displayLocale) {
  54. $displayLocale = Locale::getDefault();
  55. }
  56. return $this->reader->readEntry($this->path, $displayLocale, array('Names', $currency, static::INDEX_SYMBOL));
  57. }
  58. public function getName($currency, $displayLocale = null)
  59. {
  60. if (null === $displayLocale) {
  61. $displayLocale = Locale::getDefault();
  62. }
  63. return $this->reader->readEntry($this->path, $displayLocale, array('Names', $currency, static::INDEX_NAME));
  64. }
  65. public function getNames($displayLocale = null)
  66. {
  67. if (null === $displayLocale) {
  68. $displayLocale = Locale::getDefault();
  69. }
  70. // ====================================================================
  71. // For reference: It is NOT possible to return names indexed by
  72. // numeric code here, because some numeric codes map to multiple
  73. // 3-letter codes (e.g. 32 => "ARA", "ARP", "ARS")
  74. // ====================================================================
  75. $names = $this->reader->readEntry($this->path, $displayLocale, array('Names'));
  76. if ($names instanceof \Traversable) {
  77. $names = iterator_to_array($names);
  78. }
  79. $index = static::INDEX_NAME;
  80. array_walk($names, function (&$value) use ($index) {
  81. $value = $value[$index];
  82. });
  83. // Sorting by value cannot be done during bundle generation, because
  84. // binary bundles are always sorted by keys
  85. $collator = new \Collator($displayLocale);
  86. $collator->asort($names);
  87. return $names;
  88. }
  89. /**
  90. * Data provider for {@link \Symfony\Component\Intl\Currency::getFractionDigits()}.
  91. */
  92. public function getFractionDigits($currency)
  93. {
  94. try {
  95. return $this->reader->readEntry($this->path, 'meta', array('Meta', $currency, static::INDEX_FRACTION_DIGITS));
  96. } catch (MissingResourceException $e) {
  97. return $this->reader->readEntry($this->path, 'meta', array('Meta', 'DEFAULT', static::INDEX_FRACTION_DIGITS));
  98. }
  99. }
  100. /**
  101. * Data provider for {@link \Symfony\Component\Intl\Currency::getRoundingIncrement()}.
  102. */
  103. public function getRoundingIncrement($currency)
  104. {
  105. try {
  106. return $this->reader->readEntry($this->path, 'meta', array('Meta', $currency, static::INDEX_ROUNDING_INCREMENT));
  107. } catch (MissingResourceException $e) {
  108. return $this->reader->readEntry($this->path, 'meta', array('Meta', 'DEFAULT', static::INDEX_ROUNDING_INCREMENT));
  109. }
  110. }
  111. /**
  112. * Data provider for {@link \Symfony\Component\Intl\Currency::getNumericCode()}.
  113. */
  114. public function getNumericCode($currency)
  115. {
  116. return $this->reader->readEntry($this->path, 'meta', array('Alpha3ToNumeric', $currency));
  117. }
  118. /**
  119. * Data provider for {@link \Symfony\Component\Intl\Currency::forNumericCode()}.
  120. */
  121. public function forNumericCode($numericCode)
  122. {
  123. return $this->reader->readEntry($this->path, 'meta', array('NumericToAlpha3', (string) $numericCode));
  124. }
  125. }