Unify Monetizable and AbstractMonetizable
This commit is contained in:
@@ -1,55 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Library\Economy;
|
||||
|
||||
use ApiPlatform\Metadata as API;
|
||||
|
||||
abstract class AbstractMonetizable
|
||||
{
|
||||
abstract public function getAmount(): int;
|
||||
|
||||
abstract public function setAmount(int $amount): static;
|
||||
|
||||
abstract public function getCurrency(): string;
|
||||
|
||||
abstract public function setCurrency(string $currency): static;
|
||||
|
||||
public function hasCurrencyOf(AbstractMonetizable $money): bool
|
||||
{
|
||||
return $this->getCurrency() === $money->getCurrency();
|
||||
}
|
||||
|
||||
protected function toBrickMoney(): \Brick\Money\Money
|
||||
{
|
||||
return \Brick\Money\Money::ofMinor(
|
||||
$this->getAmount(),
|
||||
$this->getCurrency()
|
||||
);
|
||||
}
|
||||
|
||||
#[API\ApiProperty(readable: false)]
|
||||
public function isZero(): bool
|
||||
{
|
||||
return $this->toBrickMoney()->isZero();
|
||||
}
|
||||
|
||||
public function isLessThan(AbstractMonetizable $money): bool
|
||||
{
|
||||
return $this->toBrickMoney()->isLessThan($money->toBrickMoney());
|
||||
}
|
||||
|
||||
public function isLessThanOrEqualTo(AbstractMonetizable $money): bool
|
||||
{
|
||||
return $this->toBrickMoney()->isLessThanOrEqualTo($money->toBrickMoney());
|
||||
}
|
||||
|
||||
public function isGreaterThan(AbstractMonetizable $money): bool
|
||||
{
|
||||
return $this->toBrickMoney()->isGreaterThan($money->toBrickMoney());
|
||||
}
|
||||
|
||||
public function isGreaterThanOrEqualTo(AbstractMonetizable $money): bool
|
||||
{
|
||||
return $this->toBrickMoney()->isGreaterThanOrEqualTo($money->toBrickMoney());
|
||||
}
|
||||
}
|
||||
@@ -2,7 +2,6 @@
|
||||
|
||||
namespace App\Library\Economy\Currency;
|
||||
|
||||
use App\Library\Economy\AbstractMonetizable;
|
||||
use App\Library\Economy\Monetizable;
|
||||
use Brick\Math\RoundingMode;
|
||||
use Brick\Money\CurrencyConverter;
|
||||
@@ -55,7 +54,7 @@ class EuropeanCentralBankExchange implements ExchangeInterface
|
||||
return 100;
|
||||
}
|
||||
|
||||
public function getConversion(AbstractMonetizable $money, string $currency): Monetizable
|
||||
public function getConversion(Monetizable $money, string $currency): Monetizable
|
||||
{
|
||||
return Monetizable::ofBrickMoney($this->converter->convert(
|
||||
Money::ofMinor($money->getAmount(), $money->getCurrency()),
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
namespace App\Library\Economy\Currency;
|
||||
|
||||
use App\Library\Economy\AbstractMonetizable;
|
||||
use App\Library\Economy\Monetizable;
|
||||
use Brick\Money\Exception\CurrencyConversionException;
|
||||
|
||||
interface ExchangeInterface
|
||||
@@ -18,12 +18,12 @@ interface ExchangeInterface
|
||||
public function getWeight(): int;
|
||||
|
||||
/**
|
||||
* @param AbstractMonetizable $money The money to be converted
|
||||
* @param Monetizable $money The money to be converted
|
||||
* @param string $currency The currency to convert to
|
||||
* @return AbstractMonetizable The converted Money
|
||||
* @return Monetizable The converted Money
|
||||
* @throws CurrencyConversionException If the exchange rate is not available
|
||||
*/
|
||||
public function getConversion(AbstractMonetizable $money, string $currency): AbstractMonetizable;
|
||||
public function getConversion(Monetizable $money, string $currency): Monetizable;
|
||||
|
||||
/**
|
||||
* @param string $source The currency to convert from
|
||||
|
||||
@@ -2,12 +2,14 @@
|
||||
|
||||
namespace App\Library\Economy;
|
||||
|
||||
use ApiPlatform\Metadata as API;
|
||||
use Brick\Money\AbstractMoney;
|
||||
use Brick\Money\Money;
|
||||
|
||||
/**
|
||||
* Monetizables are able to be operated as basic Moneys.
|
||||
*/
|
||||
class Monetizable extends AbstractMonetizable
|
||||
class Monetizable
|
||||
{
|
||||
/**
|
||||
* An amount of currency, expressed in the minor unit (cents, pennies, etc).
|
||||
@@ -57,7 +59,12 @@ class Monetizable extends AbstractMonetizable
|
||||
return $this;
|
||||
}
|
||||
|
||||
private static function toMinorAmount(AbstractMoney $money): int
|
||||
public function hasCurrencyOf(Monetizable $money): bool
|
||||
{
|
||||
return $this->getCurrency() === $money->getCurrency();
|
||||
}
|
||||
|
||||
protected static function getBrickMoneyMinorAmount(AbstractMoney $money): int
|
||||
{
|
||||
return $money
|
||||
->getAmount()
|
||||
@@ -66,35 +73,89 @@ class Monetizable extends AbstractMonetizable
|
||||
->toInt();
|
||||
}
|
||||
|
||||
protected function toBrickMoney(): Money
|
||||
{
|
||||
return Money::ofMinor(
|
||||
$this->getAmount(),
|
||||
$this->getCurrency()
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Mutably update this instance to have the same currency and amount of a `Brick\Money\AbstractMoney` instance
|
||||
* @param AbstractMoney $money
|
||||
*/
|
||||
public function fromBrickMoney(AbstractMoney $money): static
|
||||
{
|
||||
$this->setCurrency($money->getCurrency());
|
||||
$this->setAmount(self::getBrickMoneyMinorAmount($money));
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new instance with the same currency and amount of a `Brick\Money\AbstractMoney` instance
|
||||
* @param AbstractMoney $money
|
||||
*/
|
||||
public static function ofBrickMoney(AbstractMoney $money): Monetizable
|
||||
{
|
||||
$result = new Monetizable();
|
||||
|
||||
$result->setCurrency($money->getCurrency());
|
||||
$result->setAmount(self::toMinorAmount($money));
|
||||
$result->setAmount(self::getBrickMoneyMinorAmount($money));
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function fromBrickMoney(AbstractMoney $money): static
|
||||
#[API\ApiProperty(readable: false)]
|
||||
public function isZero(): bool
|
||||
{
|
||||
$this->setCurrency($money->getCurrency());
|
||||
$this->setAmount(self::toMinorAmount($money));
|
||||
return $this->toBrickMoney()->isZero();
|
||||
}
|
||||
|
||||
return $this;
|
||||
public function isLessThan(Monetizable $money): bool
|
||||
{
|
||||
return $this
|
||||
->toBrickMoney()
|
||||
->isLessThan($money->toBrickMoney());
|
||||
}
|
||||
|
||||
public function isLessThanOrEqualTo(Monetizable $money): bool
|
||||
{
|
||||
return $this
|
||||
->toBrickMoney()
|
||||
->isLessThanOrEqualTo($money->toBrickMoney());
|
||||
}
|
||||
|
||||
public function isGreaterThan(Monetizable $money): bool
|
||||
{
|
||||
return $this
|
||||
->toBrickMoney()
|
||||
->isGreaterThan($money->toBrickMoney());
|
||||
}
|
||||
|
||||
public function isGreaterThanOrEqualTo(Monetizable $money): bool
|
||||
{
|
||||
return $this
|
||||
->toBrickMoney()
|
||||
->isGreaterThanOrEqualTo($money->toBrickMoney());
|
||||
}
|
||||
|
||||
public function plus(Monetizable $money): static
|
||||
{
|
||||
return $this->fromBrickMoney(
|
||||
$this->toBrickMoney()->plus($money->toBrickMoney())
|
||||
$this
|
||||
->toBrickMoney()
|
||||
->plus($money->toBrickMoney())
|
||||
);
|
||||
}
|
||||
|
||||
public function minus(Monetizable $money): static
|
||||
{
|
||||
return $this->fromBrickMoney(
|
||||
$this->toBrickMoney()->minus($money->toBrickMoney())
|
||||
$this
|
||||
->toBrickMoney()
|
||||
->minus($money->toBrickMoney())
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user