isGreaterThan($money)) { $min = $money; } } return $min; } /** * Returns the maximum of the given monies. * * If several monies are equal to the maximum value, the first one is returned. * * @param RationalMoney $money The first money. * @param RationalMoney ...$monies The subsequent monies. * * @throws CurrencyMismatchException If the monies do not share the same currency. * * @pure */ public static function max(RationalMoney $money, RationalMoney ...$monies): RationalMoney { $max = $money; foreach ($monies as $money) { if ($max->isLessThan($money)) { $max = $money; } } return $max; } /** * Returns the sum of the given monies. * * @param RationalMoney $money The first money. * @param RationalMoney ...$monies The subsequent monies. * * @throws CurrencyMismatchException If the monies do not share the same currency. * * @pure */ public static function sum(RationalMoney $money, RationalMoney ...$monies): RationalMoney { $sum = $money; foreach ($monies as $money) { $sum = $sum->plus($money); } return $sum; } #[Override] public function getAmount(): BigRational { return $this->amount; } #[Override] public function getCurrency(): Currency { return $this->currency; } /** * Returns the sum of this RationalMoney and the given amount. * * @param AbstractMoney|BigNumber|int|string $that The money or amount to add. * * @throws MathException If the argument is an invalid number. * @throws CurrencyMismatchException If the argument is a money in another currency. * * @pure */ public function plus(AbstractMoney|BigNumber|int|string $that): RationalMoney { $that = $this->getAmountOf($that); $amount = $this->amount->plus($that); return new self($amount, $this->currency); } /** * Returns the difference of this RationalMoney and the given amount. * * @param AbstractMoney|BigNumber|int|string $that The money or amount to subtract. * * @throws MathException If the argument is an invalid number. * @throws CurrencyMismatchException If the argument is a money in another currency. * * @pure */ public function minus(AbstractMoney|BigNumber|int|string $that): RationalMoney { $that = $this->getAmountOf($that); $amount = $this->amount->minus($that); return new self($amount, $this->currency); } /** * Returns the product of this RationalMoney and the given number. * * @param BigNumber|int|string $that The multiplier. * * @throws MathException If the argument is an invalid number. * * @pure */ public function multipliedBy(BigNumber|int|string $that): RationalMoney { $amount = $this->amount->multipliedBy($that); return new self($amount, $this->currency); } /** * Returns the result of the division of this RationalMoney by the given number. * * @param BigNumber|int|string $that The divisor. * * @throws MathException If the argument is an invalid number. * @throws DivisionByZeroException If the argument is zero. * * @pure */ public function dividedBy(BigNumber|int|string $that): RationalMoney { $amount = $this->amount->dividedBy($that); return new self($amount, $this->currency); } /** * Returns a RationalMoney whose value is the absolute value of this RationalMoney. */ #[Override] public function abs(): static { return new self($this->amount->abs(), $this->currency); } /** * Returns a RationalMoney whose value is the negated value of this RationalMoney. */ #[Override] public function negated(): static { return new self($this->amount->negated(), $this->currency); } /** * Converts this RationalMoney to another currency, using an exchange rate. * * @param Currency|string $currency The Currency instance or ISO currency code. * @param BigNumber|int|string $exchangeRate The exchange rate to multiply by. * * @throws UnknownCurrencyException If an unknown currency code is given. * @throws MathException If the exchange rate is an invalid number. * @throws InvalidArgumentException If the exchange rate is not strictly positive, or if same-currency conversion * is attempted with an exchange rate other than 1. * * @pure */ public function convertedTo(Currency|string $currency, BigNumber|int|string $exchangeRate): RationalMoney { if (! $currency instanceof Currency) { $currency = Currency::of($currency); } $exchangeRate = BigNumber::of($exchangeRate); if ($currency->isEqualTo($this->currency)) { if (! $exchangeRate->isEqualTo(1)) { throw InvalidArgumentException::sameCurrencyRateNotOne(); } return new self($this->amount, $currency); } if ($exchangeRate->isNegativeOrZero()) { throw InvalidArgumentException::nonPositiveExchangeRate(); } return new self($this->amount->multipliedBy($exchangeRate), $currency); } #[Override] public function toRational(): RationalMoney { return $this; } /** * @pure */ #[Override] public function __toString(): string { return $this->currency . ' ' . $this->amount; } }