コード例 #1
0
ファイル: ExchangeRate.php プロジェクト: matmar10/lib-money
 /**
  * {inheritDoc}
  */
 public function convert(MoneyInterface $amount)
 {
     if ($amount->getCurrency()->equals($this->getFromCurrency())) {
         $totalPrecision = $this->fromCurrency->getPrecision() + $this->toCurrency->getPrecision();
         $targetPrecision = $this->toCurrency->getPrecision();
         $newAmount = bcmul((string) $amount->getAmountFloat(), (string) $this->getMultiplier(), $totalPrecision);
         $roundedAmount = round($newAmount, $targetPrecision);
         $newMoney = new Money($this->toCurrency);
         $newMoney->setAmountFloat($roundedAmount);
         return $newMoney;
     }
     // rate represents inverse, so treat "from" and "to" reversed
     if ($amount->getCurrency()->equals($this->getToCurrency())) {
         $totalPrecision = $this->fromCurrency->getPrecision() + $this->toCurrency->getPrecision();
         $targetPrecision = $this->fromCurrency->getPrecision();
         $newAmount = bcdiv((string) $amount->getAmountFloat(), (string) $this->getMultiplier(), $totalPrecision);
         $roundedAmount = round($newAmount, $targetPrecision);
         $newMoney = new Money($this->fromCurrency);
         $newMoney->setAmountFloat($roundedAmount);
         return $newMoney;
     }
     throw new InvalidArgumentException("Cannot convert from " . $amount->getCurrency()->getCurrencyCode() . " using CurrencyRate of " . $this->getFromCurrency()->getCurrencyCode() . " to " . $this->getToCurrency()->getCurrencyCode() . ": CurrencyRate must include the base currency " . $amount->getCurrency()->getCurrencyCode());
 }