예제 #1
0
 /**
  * @param Money\Money $money
  * @return Mone\Money
  */
 public function convert(Money\Money $money)
 {
     if (!isset($this->currencyPair)) {
         $from = new Money\Currency($this->from == 'XBT' ? 'BTC' : $this->from);
         $to = new Money\Currency($this->to == 'XBT' ? 'BTC' : $this->to);
         $this->currencyPair = new Money\CurrencyPair($from, $to, $this->getRatio());
     }
     return $this->currencyPair->convert($money);
 }
 /** @test */
 public function ConvertsEurToUsdAndBack()
 {
     $eur = Money::EUR(100);
     $pair = new CurrencyPair(new Currency('EUR'), new Currency('USD'), 1.25);
     $usd = $pair->convert($eur);
     $this->assertEquals(Money::USD(125), $usd);
     $pair = new CurrencyPair(new Currency('USD'), new Currency('EUR'), 0.8);
     $eur = $pair->convert($usd);
     $this->assertEquals(Money::EUR(100), $eur);
 }
 public function convert(Money $money, $currency)
 {
     if (!$currency instanceof Currency) {
         $currency = new Currency($currency);
     }
     if ($money->getCurrency()->equals($currency)) {
         return $money;
     }
     $rates = $this->getRates($money->getCurrency());
     if (!isset($rates[$currency->getName()])) {
         throw new RuntimeException(sprintf('Could not get rate for currency %s', $currency->getName()));
     }
     $pair = new CurrencyPair($money->getCurrency(), $currency, $rates[$currency->getName()]);
     return $pair->convert($money);
 }
예제 #4
0
 /**
  * @inheritdoc
  */
 public function convert(Money $amount, $currencyCode)
 {
     $ratio = $this->getRelativeRatio($amount->getCurrency()->getName(), $currencyCode);
     $pair = new CurrencyPair($amount->getCurrency(), new Currency($currencyCode), $ratio);
     return $pair->convert($amount);
 }
예제 #5
0
 /**
  * @expectedException InvalidArgumentException
  * @expectedExceptionMessage The Money has the wrong currency
  */
 public function testConvertWithInvalidCurrency()
 {
     $money = new Money(100, CurrencyProxy::determine('JPY'));
     $pair = new CurrencyPair(CurrencyProxy::determine('EUR'), CurrencyProxy::determine('USD'), 1.25);
     $pair->convert($money);
 }
예제 #6
0
 /**
  * @expectedException \Money\InvalidArgumentException
  * @expectedExceptionMessage The Money has the wrong currency
  */
 public function testConvertWithInvalidCurrency()
 {
     $money = new Money(100, new Currency('JPY'));
     $pair = new CurrencyPair(new Currency('EUR'), new Currency('USD'), 1.25);
     $pair->convert($money);
 }