| 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 <[email protected]> |
| 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 |
#[\ReturnTypeWillChange] |
| 67 |
public function getIterator() |
| 68 |
{ |
| 69 |
$iterator = new \AppendIterator(); |
| 70 |
|
| 71 |
foreach ($this->currencies as $currencies) { |
| 72 |
$iterator->append($currencies->getIterator()); |
| 73 |
} |
| 74 |
|
| 75 |
return $iterator; |
| 76 |
} |
| 77 |
} |
| 78 |
|