/**
  * {@inheritdoc}
  */
 public function getExchangeRate($sourceCurrencyCode, $targetCurrencyCode)
 {
     if (isset($this->exchangeRates[$sourceCurrencyCode][$targetCurrencyCode])) {
         return $this->exchangeRates[$sourceCurrencyCode][$targetCurrencyCode];
     }
     $exchangeRate = $this->provider->getExchangeRate($sourceCurrencyCode, $targetCurrencyCode);
     $this->exchangeRates[$sourceCurrencyCode][$targetCurrencyCode] = $exchangeRate;
     return $exchangeRate;
 }
示例#2
0
 /**
  * @param Money    $money
  * @param Currency $currency
  *
  * @return Money
  *
  * @throws CurrencyConversionException If the exchange rate is not available.
  * @throws RoundingNecessaryException  If rounding was necessary but this converter uses RoundingMode::UNNECESSARY.
  */
 public function convert(Money $money, Currency $currency)
 {
     if ($money->getCurrency()->is($currency)) {
         $exchangeRate = 1;
     } else {
         $exchangeRate = $this->exchangeRateProvider->getExchangeRate($money->getCurrency()->getCode(), $currency->getCode());
     }
     return $money->convertedTo($currency, $exchangeRate, $this->context);
 }
 /**
  * {@inheritdoc}
  */
 public function getExchangeRate($sourceCurrencyCode, $targetCurrencyCode)
 {
     if ($sourceCurrencyCode == $this->baseCurrencyCode) {
         return $this->provider->getExchangeRate($sourceCurrencyCode, $targetCurrencyCode);
     }
     if ($targetCurrencyCode == $this->baseCurrencyCode) {
         $exchangeRate = $this->provider->getExchangeRate($targetCurrencyCode, $sourceCurrencyCode);
         return BigRational::of($exchangeRate)->reciprocal();
     }
     $baseToSource = $this->provider->getExchangeRate($this->baseCurrencyCode, $sourceCurrencyCode);
     $baseToTarget = $this->provider->getExchangeRate($this->baseCurrencyCode, $targetCurrencyCode);
     return BigRational::of($baseToTarget)->dividedBy($baseToSource);
 }
示例#4
0
 /**
  * Compares the given monies.
  *
  * The amount is not rounded before comparison, so the results are more relevant than when using
  * `convert($a, $b->getCurrency())->compareTo($b)`.
  *
  * Note that the comparison is performed by converting A into B's currency.
  * This order is important if the exchange rate provider uses different exchange rates
  * when converting back and forth two currencies.
  *
  * @param Money $a
  * @param Money $b
  *
  * @return int -1, 0 or 1.
  *
  * @throws CurrencyConversionException If the exchange rate is not available.
  */
 public function compare(Money $a, Money $b)
 {
     $aCurrencyCode = $a->getCurrency()->getCode();
     $bCurrencyCode = $b->getCurrency()->getCode();
     if ($aCurrencyCode === $bCurrencyCode) {
         return $a->compareTo($b);
     }
     $aAmount = $a->getAmount();
     $bAmount = $b->getAmount();
     $exchangeRate = $this->exchangeRateProvider->getExchangeRate($aCurrencyCode, $bCurrencyCode);
     $aAmount = $aAmount->toBigRational()->multipliedBy($exchangeRate);
     return $aAmount->compareTo($bAmount);
 }