public function convert(Money $amount, Currency $currency) { // Same currency? No conversion necessary. if ($amount->getCurrency()->getCode() === $currency->getCode()) { return $amount; } // Got a direct mapping? Do it. if (isset($this->mappings[$amount->getCurrency()->getCode()][$currency->getCode()])) { return new Money(bcmul($amount->getValue(), $this->mappings[$amount->getCurrency()->getCode()][$currency->getCode()], $amount->getPrecision()), $currency, $amount->getPrecision()); } // Find a multi-step conversion and recursively call `convert` on each step. $conversionPath = $this->getConversionPath($amount->getCurrency()->getCode(), $currency->getCode()); if ($conversionPath) { return array_reduce($conversionPath, function ($amount, $nextCode) { return $this->convert($amount, $this->mint->getCurrency($nextCode)); }, $amount); } // Still nothing? Error! throw new InvalidArgumentException("No way exists to convert {$fromCode} into {$toCode}."); }
public function testGetSetUnits() { $currency = new Currency('USD'); $currency->setUnits('dollars'); $this->assertEquals($currency->getUnits(), 'dollars'); }