¨4.0.1¨

This commit is contained in:
¨NW¨
2023-12-03 14:07:47 +00:00
parent c08b36d1b6
commit f35052522d
1112 changed files with 43019 additions and 24987 deletions

View File

@@ -13,49 +13,44 @@ class Money implements JsonSerializable
private $amount;
private $currency;
public function __construct($amount, $currency)
{
$this->amount = $amount;
$this->currency = $currency;
}
public static function inDefaultCurrency($amount)
{
return new self($amount, setting('default_currency'));
}
public static function inCurrentCurrency($amount)
{
return new self($amount, currency());
}
private function newInstance($amount)
{
return new self($amount, $this->currency);
}
public function amount()
{
return $this->amount;
}
public function subunit()
{
$fraction = 10 ** Currency::subunit($this->currency);
return (int) round($this->amount * $fraction);
}
public function currency()
{
return $this->currency;
}
public function isZero()
{
return $this->amount == 0;
}
public function add($addend)
{
$addend = $this->convertToSameCurrency($addend);
@@ -63,70 +58,18 @@ class Money implements JsonSerializable
return $this->newInstance($this->amount + $addend->amount);
}
public function subtract($subtrahend)
{
$subtrahend = $this->convertToSameCurrency($subtrahend);
return $this->newInstance($this->amount - $subtrahend->amount);
public function isNotSameCurrency($other)
{
return !$this->isSameCurrency($other);
}
public function multiply($multiplier)
{
return $this->newInstance($this->amount * $multiplier);
}
public function divide($divisor)
{
return $this->newInstance($this->amount / $divisor);
}
public function lessThan($other)
{
return $this->amount < $other->amount;
}
public function lessThanOrEqual($other)
{
return $this->amount <= $other->amount;
}
public function greaterThan($other)
{
return $this->amount > $other->amount;
}
public function greaterThanOrEqual($other)
{
return $this->amount >= $other->amount;
}
private function convertToSameCurrency($other)
{
if ($this->isNotSameCurrency($other)) {
$other = $other->convertToDefaultCurrency();
}
$this->assertSameCurrency($other);
return $other;
}
public function isSameCurrency($other)
{
return $this->currency === $other->currency;
}
public function isNotSameCurrency($other)
{
return ! $this->isSameCurrency($other);
}
private function assertSameCurrency($other)
{
if ($this->isNotSameCurrency($other)) {
throw new InvalidArgumentException('Mismatch money currency.');
}
}
public function convertToDefaultCurrency()
{
@@ -139,22 +82,51 @@ class Money implements JsonSerializable
return new self($this->amount / $currencyRate, setting('default_currency'));
}
public function convertToCurrentCurrency($currencyRate = null)
public function subtract($subtrahend)
{
return $this->convert(currency(), $currencyRate);
$subtrahend = $this->convertToSameCurrency($subtrahend);
return $this->newInstance($this->amount - $subtrahend->amount);
}
public function convert($currency, $currencyRate = null)
public function multiply($multiplier)
{
$currencyRate = $currencyRate ?: CurrencyRate::for($currency);
if (is_null($currencyRate)) {
throw new InvalidArgumentException("Cannot convert the money to currency [$currency].");
}
return new self($this->amount * $currencyRate, $currency);
return $this->newInstance($this->amount * $multiplier);
}
public function divide($divisor)
{
return $this->newInstance($this->amount / $divisor);
}
public function lessThan($other)
{
return $this->amount < $other->amount;
}
public function lessThanOrEqual($other)
{
return $this->amount <= $other->amount;
}
public function greaterThan($other)
{
return $this->amount > $other->amount;
}
public function greaterThanOrEqual($other)
{
return $this->amount >= $other->amount;
}
public function round($precision = null, $mode = null)
{
if (is_null($precision)) {
@@ -166,16 +138,45 @@ class Money implements JsonSerializable
return $this->newInstance($amount);
}
public function subunit()
{
$fraction = 10 ** Currency::subunit($this->currency);
return (int)round($this->amount * $fraction);
}
public function ceil()
{
return $this->newInstance(ceil($this->amount));
}
public function floor()
{
return $this->newInstance(floor($this->amount));
}
public function jsonSerialize(): mixed
{
return array_merge($this->toArray(), [
'inCurrentCurrency' => $this->convertToCurrentCurrency()->toArray(),
]);
}
public function toArray()
{
return [
'amount' => $this->amount,
'formatted' => $this->format(),
'currency' => $this->currency,
];
}
public function format($currency = null, $locale = null)
{
$currency = $currency ?: currency();
@@ -197,24 +198,53 @@ class Money implements JsonSerializable
return $amount;
}
public function toArray()
public function convertToCurrentCurrency($currencyRate = null)
{
return [
'amount' => $this->amount,
'formatted' => $this->format(),
'currency' => $this->currency,
];
return $this->convert(currency(), $currencyRate);
}
public function jsonSerialize()
public function convert($currency, $currencyRate = null)
{
return array_merge($this->toArray(), [
'inCurrentCurrency' => $this->convertToCurrentCurrency()->toArray(),
]);
$currencyRate = $currencyRate ?: CurrencyRate::for($currency);
if (is_null($currencyRate)) {
throw new InvalidArgumentException("Cannot convert the money to currency [$currency].");
}
return new self($this->amount * $currencyRate, $currency);
}
public function __toString()
{
return (string) $this->amount;
return (string)$this->amount;
}
private function convertToSameCurrency($other)
{
if ($this->isNotSameCurrency($other)) {
$other = $other->convertToDefaultCurrency();
}
$this->assertSameCurrency($other);
return $other;
}
private function assertSameCurrency($other)
{
if ($this->isNotSameCurrency($other)) {
throw new InvalidArgumentException('Mismatch money currency.');
}
}
private function newInstance($amount)
{
return new self($amount, $this->currency);
}
}