private function exchangeMoney($source, $target) { if (is_string($source)) { $source = new Money($source); } elseif (!$source instanceof Money) { throw new InvalidArgumentException("ExchangeManager::exchange() method expects Money as first parameter"); } if (is_string($target)) { $target = new Currency($target); } elseif (!$target instanceof Currency) { throw new InvalidArgumentException("ExchangeManager::exchange() method expects Currency as second parameter"); } if (!($exRate = $this->findExRate($source->getCurrency(), $target))) { throw new ExchangeRateException("Exchange rate between {$source} and {$target} is unknown"); } return $exRate->exchange($source); }
/** * Tests for exchangeTo() method. */ public function testExchange() { $exManager = ExchangeManager::getInstance(); $exManager->add(new ExchangeRate("BGN/EUR 0.511280836")); $money = new Money("100 BGN"); // with string $this->assertEquals("51.13 EUR", $money->exchangeTo("EUR")->__toString()); // with Currency object $this->assertEquals("51.13 EUR", $money->exchangeTo(new Currency("EUR"))->__toString()); }
/** * Checks the Money are equal to other. * * @param Money $other * * @return boolean Return TRUE if the Currency and the amount are same. */ public function isEqualTo(Money $other) { return $this->currency->isEqualTo($other->getCurrency()) && $other->getAmount() == $this->amount; }