protected function saveCached(\Money\CurrencyPair $pair) { if ($this->cache) { $cacheKey = $this->getCacheKey($pair->getCounterCurrency(), $pair->getBaseCurrency()); $this->cache->save($cacheKey, $pair, self::CACHE_LIFETIME); } }
/** * @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); }
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); }
public function testConstructorWithMoney() { $price = new Price([Money::EUR(5), Money::USD(10), Money::GBP(10), 'TRY' => 120], [CurrencyPair::createFromIso('USD/CHF 1.5'), CurrencyPair::createFromIso('USD/AWG 1.0')]); $this->assertInstanceOf('Leaphly\\Price\\Price', $price); $this->assertEquals(15, $price->getAmount('CHF')); }
/** * @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); }
/** * @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); }
/** @test */ public function ParsesIso() { $pair = CurrencyPair::createFromIso('EUR/USD 1.2500'); $expected = new CurrencyPair(new Currency('EUR'), new Currency('USD'), 1.25); $this->assertEquals($expected, $pair); }
/** * @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); }