CurrencyBundle.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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\ResourceBundle;
  11. /**
  12. * Default implementation of {@link CurrencyBundleInterface}.
  13. *
  14. * @author Bernhard Schussek <bschussek@gmail.com>
  15. */
  16. class CurrencyBundle extends AbstractBundle implements CurrencyBundleInterface
  17. {
  18. const INDEX_NAME = 0;
  19. const INDEX_SYMBOL = 1;
  20. const INDEX_FRACTION_DIGITS = 2;
  21. const INDEX_ROUNDING_INCREMENT = 3;
  22. /**
  23. * {@inheritdoc}
  24. */
  25. public function getCurrencySymbol($currency, $locale = null)
  26. {
  27. if (null === $locale) {
  28. $locale = \Locale::getDefault();
  29. }
  30. return $this->readEntry($locale, array('Currencies', $currency, static::INDEX_SYMBOL));
  31. }
  32. /**
  33. * {@inheritdoc}
  34. */
  35. public function getCurrencyName($currency, $locale = null)
  36. {
  37. if (null === $locale) {
  38. $locale = \Locale::getDefault();
  39. }
  40. return $this->readEntry($locale, array('Currencies', $currency, static::INDEX_NAME));
  41. }
  42. /**
  43. * {@inheritdoc}
  44. */
  45. public function getCurrencyNames($locale = null)
  46. {
  47. if (null === $locale) {
  48. $locale = \Locale::getDefault();
  49. }
  50. if (null === ($currencies = $this->readEntry($locale, array('Currencies')))) {
  51. return array();
  52. }
  53. if ($currencies instanceof \Traversable) {
  54. $currencies = iterator_to_array($currencies);
  55. }
  56. $index = static::INDEX_NAME;
  57. array_walk($currencies, function (&$value) use ($index) {
  58. $value = $value[$index];
  59. });
  60. return $currencies;
  61. }
  62. /**
  63. * {@inheritdoc}
  64. */
  65. public function getFractionDigits($currency)
  66. {
  67. return $this->readEntry('en', array('Currencies', $currency, static::INDEX_FRACTION_DIGITS));
  68. }
  69. /**
  70. * {@inheritdoc}
  71. */
  72. public function getRoundingIncrement($currency)
  73. {
  74. return $this->readEntry('en', array('Currencies', $currency, static::INDEX_ROUNDING_INCREMENT));
  75. }
  76. }