IcuCurrencyBundle.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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\Icu;
  11. use Symfony\Component\Intl\ResourceBundle\CurrencyBundle;
  12. use Symfony\Component\Intl\ResourceBundle\Reader\StructuredBundleReaderInterface;
  13. /**
  14. * An ICU-specific implementation of {@link \Symfony\Component\Intl\ResourceBundle\CurrencyBundleInterface}.
  15. *
  16. * This class normalizes the data of the ICU .res files to satisfy the contract
  17. * defined in {@link \Symfony\Component\Intl\ResourceBundle\CurrencyBundleInterface}.
  18. *
  19. * @author Bernhard Schussek <bschussek@gmail.com>
  20. */
  21. class IcuCurrencyBundle extends CurrencyBundle
  22. {
  23. const INDEX_SYMBOL = 0;
  24. const INDEX_NAME = 1;
  25. const INDEX_FRACTION_DIGITS = 0;
  26. const INDEX_ROUNDING_INCREMENT = 1;
  27. public function __construct(StructuredBundleReaderInterface $reader)
  28. {
  29. parent::__construct(realpath(IcuData::getResourceDirectory() . '/curr'), $reader);
  30. }
  31. /**
  32. * {@inheritdoc}
  33. */
  34. public function getCurrencyNames($locale = null)
  35. {
  36. if (null === $locale) {
  37. $locale = \Locale::getDefault();
  38. }
  39. $names = parent::getCurrencyNames($locale);
  40. $collator = new \Collator($locale);
  41. $collator->asort($names);
  42. return $names;
  43. }
  44. /**
  45. * {@inheritdoc}
  46. */
  47. public function getFractionDigits($currency)
  48. {
  49. $entry = $this->readEntry('supplementaldata', array('CurrencyMeta'));
  50. if (!isset($entry[$currency][self::INDEX_FRACTION_DIGITS])) {
  51. // The 'DEFAULT' key contains the fraction digits and the rounding
  52. // increment that are common for a lot of currencies.
  53. // Only currencies with different values are added to the icu-data
  54. // (e.g: CHF and JPY)
  55. return $entry['DEFAULT'][self::INDEX_FRACTION_DIGITS];
  56. }
  57. return $entry[$currency][self::INDEX_FRACTION_DIGITS];
  58. }
  59. /**
  60. * {@inheritdoc}
  61. */
  62. public function getRoundingIncrement($currency)
  63. {
  64. $entry = $this->readEntry('supplementaldata', array('CurrencyMeta'));
  65. if (!isset($entry[$currency][self::INDEX_ROUNDING_INCREMENT])) {
  66. // The 'DEFAULT' key contains the fraction digits and the rounding
  67. // increment that are common for a lot of currencies.
  68. // Only currencies with different values are added to the icu-data
  69. // (e.g: CHF and JPY)
  70. return $entry['DEFAULT'][self::INDEX_ROUNDING_INCREMENT];
  71. }
  72. return $entry[$currency][self::INDEX_ROUNDING_INCREMENT];
  73. }
  74. }