vendor/moneyphp/money/src/Currencies/ISOCurrencies.php line 14

Open in your IDE?
  1. <?php
  2. namespace Money\Currencies;
  3. use Money\Currencies;
  4. use Money\Currency;
  5. use Money\Exception\UnknownCurrencyException;
  6. /**
  7.  * List of supported ISO 4217 currency codes and names.
  8.  *
  9.  * @author Mathias Verraes
  10.  */
  11. final class ISOCurrencies implements Currencies
  12. {
  13.     /**
  14.      * Map of known currencies indexed by code.
  15.      *
  16.      * @var array
  17.      */
  18.     private static $currencies;
  19.     /**
  20.      * {@inheritdoc}
  21.      */
  22.     public function contains(Currency $currency)
  23.     {
  24.         return isset($this->getCurrencies()[$currency->getCode()]);
  25.     }
  26.     /**
  27.      * {@inheritdoc}
  28.      */
  29.     public function subunitFor(Currency $currency)
  30.     {
  31.         if (!$this->contains($currency)) {
  32.             throw new UnknownCurrencyException('Cannot find ISO currency '.$currency->getCode());
  33.         }
  34.         return $this->getCurrencies()[$currency->getCode()]['minorUnit'];
  35.     }
  36.     /**
  37.      * Returns the numeric code for a currency.
  38.      *
  39.      * @param Currency $currency
  40.      *
  41.      * @return int
  42.      *
  43.      * @throws UnknownCurrencyException If currency is not available in the current context
  44.      */
  45.     public function numericCodeFor(Currency $currency)
  46.     {
  47.         if (!$this->contains($currency)) {
  48.             throw new UnknownCurrencyException('Cannot find ISO currency '.$currency->getCode());
  49.         }
  50.         return $this->getCurrencies()[$currency->getCode()]['numericCode'];
  51.     }
  52.     /**
  53.      * @return \Traversable
  54.      */
  55.     public function getIterator()
  56.     {
  57.         return new \ArrayIterator(
  58.             array_map(
  59.                 function ($code) {
  60.                     return new Currency($code);
  61.                 },
  62.                 array_keys($this->getCurrencies())
  63.             )
  64.         );
  65.     }
  66.     /**
  67.      * Returns a map of known currencies indexed by code.
  68.      *
  69.      * @return array
  70.      */
  71.     private function getCurrencies()
  72.     {
  73.         if (null === self::$currencies) {
  74.             self::$currencies $this->loadCurrencies();
  75.         }
  76.         return self::$currencies;
  77.     }
  78.     /**
  79.      * @return array
  80.      */
  81.     private function loadCurrencies()
  82.     {
  83.         $file __DIR__.'/../../resources/currency.php';
  84.         if (file_exists($file)) {
  85.             return require $file;
  86.         }
  87.         throw new \RuntimeException('Failed to load currency ISO codes.');
  88.     }
  89. }