AggregateCurrencies.php
4 years ago
BitcoinCurrencies.php
4 years ago
CachedCurrencies.php
4 years ago
CurrencyList.php
4 years ago
ISOCurrencies.php
4 years ago
AggregateCurrencies.php
77 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Money\Currencies; |
| 4 | |
| 5 | use Money\Currencies; |
| 6 | use Money\Currency; |
| 7 | use Money\Exception\UnknownCurrencyException; |
| 8 | |
| 9 | /** |
| 10 | * Aggregates several currency repositories. |
| 11 | * |
| 12 | * @author Márk Sági-Kazár <mark.sagikazar@gmail.com> |
| 13 | */ |
| 14 | final class AggregateCurrencies implements Currencies |
| 15 | { |
| 16 | /** |
| 17 | * @var Currencies[] |
| 18 | */ |
| 19 | private $currencies; |
| 20 | |
| 21 | /** |
| 22 | * @param Currencies[] $currencies |
| 23 | */ |
| 24 | public function __construct(array $currencies) |
| 25 | { |
| 26 | foreach ($currencies as $c) { |
| 27 | if (false === $c instanceof Currencies) { |
| 28 | throw new \InvalidArgumentException('All currency repositories must implement '.Currencies::class); |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | $this->currencies = $currencies; |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * {@inheritdoc} |
| 37 | */ |
| 38 | public function contains(Currency $currency) |
| 39 | { |
| 40 | foreach ($this->currencies as $currencies) { |
| 41 | if ($currencies->contains($currency)) { |
| 42 | return true; |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | return false; |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * {@inheritdoc} |
| 51 | */ |
| 52 | public function subunitFor(Currency $currency) |
| 53 | { |
| 54 | foreach ($this->currencies as $currencies) { |
| 55 | if ($currencies->contains($currency)) { |
| 56 | return $currencies->subunitFor($currency); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | throw new UnknownCurrencyException('Cannot find currency '.$currency->getCode()); |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * {@inheritdoc} |
| 65 | */ |
| 66 | public function getIterator() |
| 67 | { |
| 68 | $iterator = new \AppendIterator(); |
| 69 | |
| 70 | foreach ($this->currencies as $currencies) { |
| 71 | $iterator->append($currencies->getIterator()); |
| 72 | } |
| 73 | |
| 74 | return $iterator; |
| 75 | } |
| 76 | } |
| 77 |