src/CurrencyConverter.php
public function convert(MoneyContainer $moneyContainer, $currency, int $roundingMode = RoundingMode::UNNECESSARY) : Money
    {
        if (! $currency instanceof Currency) {
            $currency = Currency::of($currency);
        }

        $currencyCode = $currency->getCurrencyCode();

        $total = BigRational::zero();

        foreach ($moneyContainer->getAmounts() as $sourceCurrencyCode => $amount) {
            if ($sourceCurrencyCode !== $currencyCode) {
                $exchangeRate = $this->exchangeRateProvider->getExchangeRate($sourceCurrencyCode, $currencyCode);
                $amount = $amount->toBigRational()->multipliedBy($exchangeRate);
            }

            $total = $total->plus($amount);
        }

        return Money::create($total, $currency, $this->context, $roundingMode);
    }
public function convert(MoneyContainer $moneyContainer, $currency, int $roundingMode = RoundingMode::UNNECESSARY) : Money
    {
        if (! $currency instanceof Currency) {
            $currency = Currency::of($currency);
        }

        $currencyCode = $currency->getCurrencyCode();

        $total = BigRational::zero();

        foreach ($moneyContainer->getAmounts() as $sourceCurrencyCode => $amount) {
            if ($sourceCurrencyCode !== $currencyCode) {
                $exchangeRate = $this->exchangeRateProvider->getExchangeRate($sourceCurrencyCode, $currencyCode);
                $amount = $amount->toBigRational()->multipliedBy($exchangeRate);
            }

            $total = $total->plus($amount);
        }

        return Money::create($total, $currency, $this->context, $roundingMode);
    }
public function convert ( MoneyContainer $ moneyContainer , $ currency , int $ roundingMode = RoundingMode :: UNNECESSARY ) : Money { if ( ! $ currency instanceof Currency ) { $ currency = Currency :: of ( $ currency ) ; } $ currencyCode = $ currency -> getCurrencyCode ( ) ; $ total = BigRational :: zero ( ) ; foreach ( $ moneyContainer -> getAmounts ( ) as $ sourceCurrencyCode => $ amount ) { if ( $ sourceCurrencyCode !== $ currencyCode ) { $ exchangeRate = $ this -> exchangeRateProvider -> getExchangeRate ( $ sourceCurrencyCode , $ currencyCode ) ; $ amount = $ amount -> toBigRational ( ) -> multipliedBy ( $ exchangeRate ) ; } $ total = $ total -> plus ( $ amount ) ; } return Money :: create ( $ total , $ currency , $ this -> context , $ roundingMode ) ; }
Converts the given money to the given currency.

@param MoneyContainer      $moneyContainer The Money, RationalMoney or MoneyBag to convert.
@param Currency|string|int $currency       The Currency instance, ISO currency code or ISO numeric currency code.
@param int                 $roundingMode   The rounding mode, if necessary.

@return Money

@throws CurrencyConversionException If the exchange rate is not available.
@throws RoundingNecessaryException  If rounding is necessary and RoundingMode::UNNECESSARY is used.
Converts the given money to the given currency .