Open Monograph Press  3.3.0
ISOCurrencies.php
1 <?php
2 
3 namespace Money\Currencies;
4 
8 
14 final class ISOCurrencies implements Currencies
15 {
21  private static $currencies;
22 
26  public function contains(Currency $currency)
27  {
28  return isset($this->getCurrencies()[$currency->getCode()]);
29  }
30 
34  public function subunitFor(Currency $currency)
35  {
36  if (!$this->contains($currency)) {
37  throw new UnknownCurrencyException('Cannot find ISO currency '.$currency->getCode());
38  }
39 
40  return $this->getCurrencies()[$currency->getCode()]['minorUnit'];
41  }
42 
52  public function numericCodeFor(Currency $currency)
53  {
54  if (!$this->contains($currency)) {
55  throw new UnknownCurrencyException('Cannot find ISO currency '.$currency->getCode());
56  }
57 
58  return $this->getCurrencies()[$currency->getCode()]['numericCode'];
59  }
60 
64  public function getIterator()
65  {
66  return new \ArrayIterator(
67  array_map(
68  function ($code) {
69  return new Currency($code);
70  },
71  array_keys($this->getCurrencies())
72  )
73  );
74  }
75 
81  private function getCurrencies()
82  {
83  if (null === self::$currencies) {
84  self::$currencies = $this->loadCurrencies();
85  }
86 
87  return self::$currencies;
88  }
89 
93  private function loadCurrencies()
94  {
95  $file = __DIR__.'/../../resources/currency.php';
96 
97  if (file_exists($file)) {
98  return require $file;
99  }
100 
101  throw new \RuntimeException('Failed to load currency ISO codes.');
102  }
103 }
Money\Currencies\ISOCurrencies
Definition: ISOCurrencies.php:14
Money\Currencies
Definition: AggregateCurrencies.php:3
Money\Currencies\ISOCurrencies\numericCodeFor
numericCodeFor(Currency $currency)
Definition: ISOCurrencies.php:52
Money\Currency
Definition: vendor/moneyphp/money/src/Currency.php:14
Money\Currencies\ISOCurrencies\contains
contains(Currency $currency)
Definition: ISOCurrencies.php:26
Money\Currency\getCode
getCode()
Definition: vendor/moneyphp/money/src/Currency.php:47
Money\Currencies\ISOCurrencies\getIterator
getIterator()
Definition: ISOCurrencies.php:64
Money\Currencies
Definition: Currencies.php:12
Money\Currencies\ISOCurrencies\subunitFor
subunitFor(Currency $currency)
Definition: ISOCurrencies.php:34
Money\Exception\UnknownCurrencyException
Definition: UnknownCurrencyException.php:12