/** * Converts Money from base to counter currency. * * @param Money $money * @param int $roundingMode * * @return Money * * @throws \InvalidArgumentException If $money's currency is not equal to base currency */ public function convert(Money $money, $roundingMode = Money::ROUND_HALF_UP) { if (!$money->getCurrency()->equals($this->baseCurrency)) { throw new \InvalidArgumentException('The Money has the wrong currency'); } return $money->convert($this->counterCurrency, $this->conversionRatio, $roundingMode); }
/** * @param money $amount * @return money */ public function add(Money $amount) { if (!$this->currency->equals($amount->getCurrency())) { throw new \InvalidArgumentException('Currency mismatch'); } return new Money($this->currency, $this->amount + $amount->getValue()); }
/** @return Money */ public function convert(Money $money) { if (!$money->getCurrency()->equals($this->counterCurrency)) { throw new InvalidArgumentException("The Money has the wrong currency"); } // @todo add rounding mode? return new Money((int) round($money->getAmount() * $this->ratio), $this->baseCurrency); }
/** * @param Money $money * @param Currency $counterCurrency * @param int $roundingMode * * @return Money */ public function convert(Money $money, Currency $counterCurrency, $roundingMode = Money::ROUND_HALF_UP) { $baseCurrency = $money->getCurrency(); $ratio = $this->exchange->quote($baseCurrency, $counterCurrency)->getConversionRatio(); $baseCurrencySubunit = $this->currencies->subunitFor($baseCurrency); $counterCurrencySubunit = $this->currencies->subunitFor($counterCurrency); $subunitDifference = $baseCurrencySubunit - $counterCurrencySubunit; $ratio = $ratio / pow(10, $subunitDifference); $counterValue = $money->multiply($ratio, $roundingMode); return new Money($counterValue->getAmount(), $counterCurrency); }
/** * Subtratcs the given money from this one (immutable) and returns the result. * * @param Money $money the money to subtract * * @return Money * * @throws CurrencyMismatchException if the Currencies do not match */ public function subtract(Money $money) { if (!$this->currency->equals($money->getCurrency())) { throw new CurrencyMismatchException($this . ' does not match ' . $money); } $amount = bcsub($this->amount, $money->getAmount(), self::BCSCALE); return self::valueOf($amount, $this->currency); }
/** * @param Money $otherMoney * * @throws CurrencyMismatchException */ private function compareCurrency(Money $otherMoney) { if ($this->currency->compareTo($otherMoney->getCurrency()) !== 0) { throw CurrencyMismatchException::currenciesNotMatching($this->currency->getCode(), $otherMoney->getCurrency()->getCode()); } }
function testSetValueAsMoney() { $m1 = new Money(); $m1->setValue(array('Currency' => 'EUR', 'Amount' => 3.44)); $m2 = new Money(); $m2->setValue($m1); $this->assertEquals($m2->getCurrency(), 'EUR'); $this->assertEquals($m2->getAmount(), 3.44); }
public function testCurrencyCanBeReturned() { $currency = new USD(); $money = new Money($currency, 1); $this->assertEquals($currency, $money->getCurrency()); }
/** * Compares this instance with an other instance. * * @param Money $b The instance to which this instance is to be compared. * * @return int -1, 0 or 1 as this instance is less than, equal to, or greater than $b * * @throws \InvalidArgumentException When the currencies do not match */ public function compareTo(Money $b) { if ($this->getCurrency() != $b->getCurrency()) { throw new \InvalidArgumentException('Can not compare different currencies'); } if ($this->getAmount() < $b->getAmount()) { return -1; } elseif ($this->getAmount() == $b->getAmount()) { return 0; } else { return 1; } }
/** * Returns the currency. * * @return string */ public function getCurrency() { $currency = parent::getCurrency(); $this->extend('updateCurrency', $currency); return $currency; }
/** * @param Money $money * @return bool */ public function isSameCurrencyAs(Money $money) { return $this->currency->equals($money->getCurrency()); }
/** * compare * * Compares two instances for equality. Returns a value suitable for a sort * callback. i.e. 1 if $first is larger than $second, 0 if equal, and -1 if * $first is smaller than $second. * * @param Money $first * @param Money $second * @return int -1, 0, or 1 */ public function compare(Money $first, Money $second) { $secondValue = $first->getCurrency()->getCode() === $second->getCurrency()->getCode() ? $second->getValue() : $this->converter->convert($second, $first->getCurrency())->getValue(); return bccomp($first->getValue(), $secondValue, max([$first->getPrecision(), $second->getPrecision()])); }
/** * Compare if the amount is equal to the given amount. * * @param Money $money * @return bool * @throws \InvalidArgumentException */ public function equalTo(Money $money) { if ($this->getCurrency() !== $money->getCurrency()) { throw new \InvalidArgumentException("Argument one must be of same currency"); } return $this->getAmount() === $money->getAmount(); }
/** * 等価性確認 * * @param Money $money マネーオブジェクト * @return bool 等価性(trueで等価) */ public function equals(Money $money) { return $this->_amount === $money->getAmount() && $this->_currency === $money->getCurrency(); }
/** * @param \SebastianBergmann\Money\Money $a * @param \SebastianBergmann\Money\Money $b * @throws \SebastianBergmann\Money\CurrencyMismatchException */ private function assertSameCurrency(Money $a, Money $b) { if ($a->getCurrency() != $b->getCurrency()) { throw new CurrencyMismatchException(); } }
/** * @deprecated */ public function convert(Money $money, ICurrency $to) { $amount = $money->toFloat() * $this->calculateExchangeRate($money->getCurrency(), $to); return new $money($to->scaleAmount($amount), $to); }
/** * @covers \SebastianBergmann\Money\Money::getCurrency * @uses \SebastianBergmann\Money\Currency * @depends testObjectCanBeConstructedForValidConstructorArguments */ public function testCurrencyCanBeRetrieved(Money $m) { $this->assertEquals(new Currency('EUR'), $m->getCurrency()); }
/** * normalizeMoneyValue * * Returns the value of $second in terms of $first's currency. * * @param Money $first * @param Money $second * @return string */ private function normalizeMoneyValue(Money $first, Money $second) { return $first->getCurrency()->getCode() === $second->getCurrency()->getCode() ? $second->getValue() : $this->converter->convert($second, $first->getCurrency())->getValue(); }