diff --git a/src/Library/Economy/AbstractMonetizable.php b/src/Library/Economy/AbstractMonetizable.php deleted file mode 100644 index 373002a..0000000 --- a/src/Library/Economy/AbstractMonetizable.php +++ /dev/null @@ -1,55 +0,0 @@ -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()); - } -} diff --git a/src/Library/Economy/Currency/EuropeanCentralBankExchange.php b/src/Library/Economy/Currency/EuropeanCentralBankExchange.php index 0ae152d..12dff5f 100644 --- a/src/Library/Economy/Currency/EuropeanCentralBankExchange.php +++ b/src/Library/Economy/Currency/EuropeanCentralBankExchange.php @@ -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()), diff --git a/src/Library/Economy/Currency/ExchangeInterface.php b/src/Library/Economy/Currency/ExchangeInterface.php index 4d363bc..9a5ec56 100644 --- a/src/Library/Economy/Currency/ExchangeInterface.php +++ b/src/Library/Economy/Currency/ExchangeInterface.php @@ -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 diff --git a/src/Library/Economy/Monetizable.php b/src/Library/Economy/Monetizable.php index a9cd1fa..2b4babc 100644 --- a/src/Library/Economy/Monetizable.php +++ b/src/Library/Economy/Monetizable.php @@ -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()) ); } }