From f58cac4708ef5613b78d0ec4c068e298f5a0b1dd Mon Sep 17 00:00:00 2001 From: Vadym Hrechukha Date: Mon, 5 Aug 2024 23:49:54 +0300 Subject: [PATCH 01/14] HP-2003: moved Formula into php-billing --- src/formula/Formula.php | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 src/formula/Formula.php diff --git a/src/formula/Formula.php b/src/formula/Formula.php new file mode 100644 index 00000000..eef0979f --- /dev/null +++ b/src/formula/Formula.php @@ -0,0 +1,27 @@ +value)) { + $this->value = $this->formulaEngine->normalize($this->value); + $this->validate($this->value); + } + } + + private function validate(string $value): void + { + $error = $this->formulaEngine->validate($value); + if ($error !== null) { + throw new FormulaEngineException($error); + } + } + + public function value(): ?string + { + return $this->value; + } +} From ad881f84814fedafd98b8186495bec82cd8ecc25 Mon Sep 17 00:00:00 2001 From: Vadym Hrechukha Date: Tue, 6 Aug 2024 01:37:35 +0300 Subject: [PATCH 02/14] HP-2003: moved Sums into php-billing --- src/price/PriceInvalidArgumentException.php | 7 ++++ src/price/Sums.php | 37 +++++++++++++++++++++ 2 files changed, 44 insertions(+) create mode 100644 src/price/PriceInvalidArgumentException.php create mode 100644 src/price/Sums.php diff --git a/src/price/PriceInvalidArgumentException.php b/src/price/PriceInvalidArgumentException.php new file mode 100644 index 00000000..d7e91381 --- /dev/null +++ b/src/price/PriceInvalidArgumentException.php @@ -0,0 +1,7 @@ + total sum for the quantity + */ + public function __construct(private ?array $values) + { + if (!empty($this->values)) { + $this->validate($this->values); + } + } + + private function validate(array $sums): void + { + if ($sums) { + foreach ($sums as $value) { + if (!is_numeric($value)) { + throw new PriceInvalidArgumentException('Invalid value for sums parameter'); + } + } + } + } + + public function values(): ?array + { + return $this->values; + } + + public function getMinSum(): int|string + { + return min($this->values); + } +} From 66c2126364052480fbefb3b3f1ebed592309b0c2 Mon Sep 17 00:00:00 2001 From: Vadym Hrechukha Date: Tue, 6 Aug 2024 02:04:00 +0300 Subject: [PATCH 03/14] HP-2003: moved Sums into php-billing --- src/price/EnumPrice.php | 23 +++++++---------------- src/price/PriceFactory.php | 4 ++-- tests/support/plan/CertificatePlan.php | 5 +++-- tests/unit/price/PriceFactoryTest.php | 2 +- 4 files changed, 13 insertions(+), 21 deletions(-) diff --git a/src/price/EnumPrice.php b/src/price/EnumPrice.php index d7a5a5bc..97207072 100644 --- a/src/price/EnumPrice.php +++ b/src/price/EnumPrice.php @@ -28,29 +28,20 @@ */ class EnumPrice extends AbstractPrice { - /** - * @var UnitInterface - */ - protected $unit; + protected UnitInterface $unit; - /** - * @var Currency - */ - protected $currency; + protected Currency $currency; - /** - * @var array quantity => total sum for the quantity - */ - protected $sums; + protected Sums $sums; public function __construct( - $id, + $id, TypeInterface $type, TargetInterface $target, ?PlanInterface $plan, UnitInterface $unit, Currency $currency, - array $sums + Sums $sums, ) { parent::__construct($id, $type, $target, $plan); $this->unit = $unit; @@ -68,7 +59,7 @@ public function getCurrency() return $this->currency; } - public function getSums() + public function getSums(): Sums { return $this->sums; } @@ -80,7 +71,7 @@ public function calculateSum(QuantityInterface $quantity): ?Money { $usage = $this->calculateUsage($quantity)->getQuantity(); - foreach ($this->sums as $value => $price) { + foreach ($this->sums->values() as $value => $price) { if ((string) $value === (string) $usage) { return new Money($price, $this->currency); } diff --git a/src/price/PriceFactory.php b/src/price/PriceFactory.php index a771f685..02e794d8 100644 --- a/src/price/PriceFactory.php +++ b/src/price/PriceFactory.php @@ -77,9 +77,9 @@ public function findMethodForClass($class) throw new FailedCreatePriceException("unknown class: $class"); } - public function createEnumPrice(PriceCreationDto $dto) + public function createEnumPrice(PriceCreationDto $dto): EnumPrice { - return new EnumPrice($dto->id, $dto->type, $dto->target, $dto->plan, $dto->unit, $dto->currency, $dto->sums); + return new EnumPrice($dto->id, $dto->type, $dto->target, $dto->plan, $dto->unit, $dto->currency, new Sums($dto->sums)); } public function createRatePrice(PriceCreationDto $dto) diff --git a/tests/support/plan/CertificatePlan.php b/tests/support/plan/CertificatePlan.php index aa3b8513..278af07f 100644 --- a/tests/support/plan/CertificatePlan.php +++ b/tests/support/plan/CertificatePlan.php @@ -14,6 +14,7 @@ use hiqdev\php\billing\customer\Customer; use hiqdev\php\billing\plan\Plan; use hiqdev\php\billing\price\EnumPrice; +use hiqdev\php\billing\price\Sums; use hiqdev\php\billing\target\Target; use hiqdev\php\billing\type\Type; use hiqdev\php\units\Unit; @@ -110,9 +111,9 @@ public function getRawPrice($action) return $this->getRawPrices($action->getType(), $action->getTarget())[$years]; } - public function getRawPrices($type, $target) + public function getRawPrices($type, $target): Sums { - return $this->rawPrices[$this->getRawPriceKey($type, $target)]; + return new Sums($this->rawPrices[$this->getRawPriceKey($type, $target)]); } /** diff --git a/tests/unit/price/PriceFactoryTest.php b/tests/unit/price/PriceFactoryTest.php index 99bfdf10..b89a6bc0 100644 --- a/tests/unit/price/PriceFactoryTest.php +++ b/tests/unit/price/PriceFactoryTest.php @@ -57,7 +57,7 @@ public function testEnumPrice() $this->assertSame($this->enum, $price->getType()); $this->assertSame($this->target, $price->getTarget()); $this->assertSame($this->unit, $price->getUnit()); - $this->assertSame($this->sums, $price->getSums()); + $this->assertSame($this->sums, $price->getSums()->values()); $this->assertSame($this->price->getCurrency(), $price->getCurrency()); } From a427946ea76f5faac527441864f3e9a3fe088b51 Mon Sep 17 00:00:00 2001 From: Vadym Hrechukha Date: Wed, 7 Aug 2024 00:59:33 +0300 Subject: [PATCH 04/14] HP-2003: replaced Currency and Money classes with existing versions --- src/price/PriceInvalidArgumentException.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/price/PriceInvalidArgumentException.php b/src/price/PriceInvalidArgumentException.php index d7e91381..309e5725 100644 --- a/src/price/PriceInvalidArgumentException.php +++ b/src/price/PriceInvalidArgumentException.php @@ -2,6 +2,8 @@ namespace hiqdev\php\billing\price; -class PriceInvalidArgumentException extends \InvalidArgumentException +use hiapi\exceptions\HiapiException; + +class PriceInvalidArgumentException extends HiapiException { } From 735923d1763455fb8052dab12d5d936235692976 Mon Sep 17 00:00:00 2001 From: Vadym Hrechukha Date: Thu, 8 Aug 2024 08:35:46 +0300 Subject: [PATCH 05/14] HP-2003: Changed PriceService to works with Billing Price --- src/formula/Formula.php | 2 +- src/price/AbstractPrice.php | 2 +- src/price/EnumPrice.php | 6 +++--- src/price/PriceWIthQuantityInterface.php | 10 +++++++++ src/price/PriceWithCurrencyInterface.php | 10 +++++++++ src/price/PriceWithMoneyInterface.php | 10 +++++++++ src/price/PriceWithSumsInterface.php | 8 ++++++++ src/price/PriceWithThresholdsInterface.php | 8 ++++++++ src/price/PriceWithUnitInterface.php | 10 +++++++++ src/price/PricedWithRateInterface.php | 8 ++++++++ src/price/PricedWithSubpriceInterface.php | 12 +++++++++++ src/price/ProgressivePrice.php | 2 +- src/price/RatePrice.php | 2 +- src/price/SinglePrice.php | 8 ++++---- src/price/Sums.php | 9 +++++++- src/price/SumsHydrator.php | 24 ++++++++++++++++++++++ 16 files changed, 119 insertions(+), 12 deletions(-) create mode 100644 src/price/PriceWIthQuantityInterface.php create mode 100644 src/price/PriceWithCurrencyInterface.php create mode 100644 src/price/PriceWithMoneyInterface.php create mode 100644 src/price/PriceWithSumsInterface.php create mode 100644 src/price/PriceWithThresholdsInterface.php create mode 100644 src/price/PriceWithUnitInterface.php create mode 100644 src/price/PricedWithRateInterface.php create mode 100644 src/price/PricedWithSubpriceInterface.php create mode 100644 src/price/SumsHydrator.php diff --git a/src/formula/Formula.php b/src/formula/Formula.php index eef0979f..cc610681 100644 --- a/src/formula/Formula.php +++ b/src/formula/Formula.php @@ -4,7 +4,7 @@ class Formula { - public function __construct(private ?string $value, private readonly FormulaEngine $formulaEngine) + public function __construct(private ?string $value, private readonly FormulaEngineInterface $formulaEngine) { if (!empty($this->value)) { $this->value = $this->formulaEngine->normalize($this->value); diff --git a/src/price/AbstractPrice.php b/src/price/AbstractPrice.php index bba98577..7d4f4dc6 100755 --- a/src/price/AbstractPrice.php +++ b/src/price/AbstractPrice.php @@ -53,7 +53,7 @@ abstract class AbstractPrice implements PriceInterface, ChargeModifier protected $plan; public function __construct( - $id, + $id, TypeInterface $type, TargetInterface $target, PlanInterface $plan = null diff --git a/src/price/EnumPrice.php b/src/price/EnumPrice.php index 97207072..a2eabb45 100644 --- a/src/price/EnumPrice.php +++ b/src/price/EnumPrice.php @@ -26,7 +26,7 @@ * * @author Andrii Vasyliev */ -class EnumPrice extends AbstractPrice +class EnumPrice extends AbstractPrice implements PriceWithSumsInterface, PriceWithCurrencyInterface, PriceWithUnitInterface { protected UnitInterface $unit; @@ -49,12 +49,12 @@ public function __construct( $this->sums = $sums; } - public function getUnit() + public function getUnit(): UnitInterface { return $this->unit; } - public function getCurrency() + public function getCurrency(): Currency { return $this->currency; } diff --git a/src/price/PriceWIthQuantityInterface.php b/src/price/PriceWIthQuantityInterface.php new file mode 100644 index 00000000..c3aa551f --- /dev/null +++ b/src/price/PriceWIthQuantityInterface.php @@ -0,0 +1,10 @@ + */ -class RatePrice extends AbstractPrice +class RatePrice extends AbstractPrice implements PricedWithRateInterface { /** @var float */ protected $rate; diff --git a/src/price/SinglePrice.php b/src/price/SinglePrice.php index 6010eb7f..deffeb35 100644 --- a/src/price/SinglePrice.php +++ b/src/price/SinglePrice.php @@ -27,7 +27,7 @@ * * @author Andrii Vasyliev */ -class SinglePrice extends AbstractPrice +class SinglePrice extends AbstractPrice implements PriceWithMoneyInterface, PriceWIthQuantityInterface { /** * @var QuantityInterface prepaid quantity also implies Unit @@ -41,7 +41,7 @@ class SinglePrice extends AbstractPrice protected $price; public function __construct( - $id, + $id, TypeInterface $type, TargetInterface $target, PlanInterface $plan = null, @@ -53,12 +53,12 @@ public function __construct( $this->price = $price; } - public function getPrepaid() + public function getPrepaid(): QuantityInterface { return $this->prepaid; } - public function getPrice() + public function getPrice(): Money { return $this->price; } diff --git a/src/price/Sums.php b/src/price/Sums.php index c9498d73..2d10117a 100644 --- a/src/price/Sums.php +++ b/src/price/Sums.php @@ -2,7 +2,7 @@ namespace hiqdev\php\billing\price; -final readonly class Sums +final readonly class Sums implements \JsonSerializable { /** * @param int[]|null $values quantity => total sum for the quantity @@ -34,4 +34,11 @@ public function getMinSum(): int|string { return min($this->values); } + + public function jsonSerialize(): ?array + { + return [ + 'values' => $this->values, + ]; + } } diff --git a/src/price/SumsHydrator.php b/src/price/SumsHydrator.php new file mode 100644 index 00000000..54c624c8 --- /dev/null +++ b/src/price/SumsHydrator.php @@ -0,0 +1,24 @@ + $object->values(), + ]; + } +} From e6a091322e85da23a0aca42388039fbb3f64411f Mon Sep 17 00:00:00 2001 From: Vadym Hrechukha Date: Thu, 8 Aug 2024 10:46:23 +0300 Subject: [PATCH 06/14] HP-2003: modernized AbstractPrice::jsonSerialize() to handle all prices --- src/price/AbstractPrice.php | 23 +++++++++++++++---- ...terface.php => PriceWithRateInterface.php} | 2 +- ...ace.php => PriceWithSubpriceInterface.php} | 2 +- src/price/RatePrice.php | 2 +- 4 files changed, 22 insertions(+), 7 deletions(-) rename src/price/{PricedWithRateInterface.php => PriceWithRateInterface.php} (76%) rename src/price/{PricedWithSubpriceInterface.php => PriceWithSubpriceInterface.php} (85%) diff --git a/src/price/AbstractPrice.php b/src/price/AbstractPrice.php index 7d4f4dc6..23bc7ee2 100755 --- a/src/price/AbstractPrice.php +++ b/src/price/AbstractPrice.php @@ -137,10 +137,25 @@ public function calculateSum(QuantityInterface $quantity): ?Money public function jsonSerialize(): array { - $res = array_filter(get_object_vars($this)); - unset($res['plan']); - - return $res; + return [ + 'id' => $this->id, + 'class' => (new \ReflectionClass($this))->getShortName(), + 'type' => $this->getType()->getName(), + 'type_id' => $this->getType()->getId(), + 'object_id' => $this->getTarget()->getId(), + 'object' => [ + 'id' => $this->getTarget()->getId(), + 'name' => $this->getTarget()->getName(), + 'type' => $this->getTarget()->getType(), + ], + 'plan_id' => $this->getPlan()?->getId(), + 'subprices' => $this instanceof PriceWithSubpriceInterface ? $this->getSubprices()->values() : null, + 'rate' => $this instanceof PriceWithRateInterface ? $this->getRate() : null, + 'unit' => $this instanceof PriceWithUnitInterface ? $this->getUnit()->getName() : null, + 'currency' => $this instanceof PriceWithCurrencyInterface ? $this->getCurrency()->getCode() : null, + 'sums' => $this instanceof PriceWithSumsInterface ? $this->getSums()->values() : null, + 'quantity' => $this instanceof PriceWithQuantityInterface ? $this->getPrepaid()->getQuantity() : 0, + ]; } /** diff --git a/src/price/PricedWithRateInterface.php b/src/price/PriceWithRateInterface.php similarity index 76% rename from src/price/PricedWithRateInterface.php rename to src/price/PriceWithRateInterface.php index 82adf3da..d7ca87a6 100644 --- a/src/price/PricedWithRateInterface.php +++ b/src/price/PriceWithRateInterface.php @@ -2,7 +2,7 @@ namespace hiqdev\php\billing\price; -interface PricedWithRateInterface +interface PriceWithRateInterface { public function getRate(): float; } diff --git a/src/price/PricedWithSubpriceInterface.php b/src/price/PriceWithSubpriceInterface.php similarity index 85% rename from src/price/PricedWithSubpriceInterface.php rename to src/price/PriceWithSubpriceInterface.php index bf5dfd32..1faf9304 100644 --- a/src/price/PricedWithSubpriceInterface.php +++ b/src/price/PriceWithSubpriceInterface.php @@ -4,7 +4,7 @@ use hiqdev\billing\hiapi\price\SubPrices; -interface PricedWithSubpriceInterface +interface PriceWithSubpriceInterface { public function getSubprices(): SubPrices; diff --git a/src/price/RatePrice.php b/src/price/RatePrice.php index ca69240a..fadad116 100644 --- a/src/price/RatePrice.php +++ b/src/price/RatePrice.php @@ -24,7 +24,7 @@ * * @author Andrii Vasyliev */ -class RatePrice extends AbstractPrice implements PricedWithRateInterface +class RatePrice extends AbstractPrice implements PriceWithRateInterface { /** @var float */ protected $rate; From d637e3e5fb1cb5bfcb991c5af7ff17b8917e9a5c Mon Sep 17 00:00:00 2001 From: Vadym Hrechukha Date: Thu, 8 Aug 2024 10:53:23 +0300 Subject: [PATCH 07/14] HP-2003: fixed name of PriceWIthQuantityInterface --- ...WIthQuantityInterface.php => PriceWithQuantityInterface.php} | 2 +- src/price/ProgressivePrice.php | 2 +- src/price/SinglePrice.php | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) rename src/price/{PriceWIthQuantityInterface.php => PriceWithQuantityInterface.php} (81%) diff --git a/src/price/PriceWIthQuantityInterface.php b/src/price/PriceWithQuantityInterface.php similarity index 81% rename from src/price/PriceWIthQuantityInterface.php rename to src/price/PriceWithQuantityInterface.php index c3aa551f..8767ba41 100644 --- a/src/price/PriceWIthQuantityInterface.php +++ b/src/price/PriceWithQuantityInterface.php @@ -4,7 +4,7 @@ use hiqdev\php\units\QuantityInterface; -interface PriceWIthQuantityInterface +interface PriceWithQuantityInterface { public function getPrepaid(): QuantityInterface; } diff --git a/src/price/ProgressivePrice.php b/src/price/ProgressivePrice.php index 2278d3de..6b5340f2 100644 --- a/src/price/ProgressivePrice.php +++ b/src/price/ProgressivePrice.php @@ -14,7 +14,7 @@ use Money\Money; use Money\Parser\DecimalMoneyParser; -class ProgressivePrice extends AbstractPrice implements PriceWithThresholdsInterface, PriceWithMoneyInterface, PriceWIthQuantityInterface +class ProgressivePrice extends AbstractPrice implements PriceWithThresholdsInterface, PriceWithMoneyInterface, PriceWithQuantityInterface { protected ProgressivePriceThresholdList $thresholds; diff --git a/src/price/SinglePrice.php b/src/price/SinglePrice.php index deffeb35..a2c680e6 100644 --- a/src/price/SinglePrice.php +++ b/src/price/SinglePrice.php @@ -27,7 +27,7 @@ * * @author Andrii Vasyliev */ -class SinglePrice extends AbstractPrice implements PriceWithMoneyInterface, PriceWIthQuantityInterface +class SinglePrice extends AbstractPrice implements PriceWithMoneyInterface, PriceWithQuantityInterface { /** * @var QuantityInterface prepaid quantity also implies Unit From 5102c241ee91702ab806cdfb8edff114fb97c02d Mon Sep 17 00:00:00 2001 From: Vadym Hrechukha Date: Thu, 8 Aug 2024 11:01:20 +0300 Subject: [PATCH 08/14] HP-2003: The hydrate method should handle cases where the expected data keys are missing to prevent potential errors during object creation. --- src/price/SumsHydrator.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/price/SumsHydrator.php b/src/price/SumsHydrator.php index 54c624c8..6ce98fe9 100644 --- a/src/price/SumsHydrator.php +++ b/src/price/SumsHydrator.php @@ -8,7 +8,10 @@ class SumsHydrator extends GeneratedHydrator { public function hydrate(array $data, $object): object { - return new Sums($data['sums'] ?? null); + if (!isset($data['sums'])) { + throw new PriceInvalidArgumentException('Missing required key: sums'); + } + return new Sums($data['sums']); } /** From fb43d6515a0d00b5ff5cc3b24c623c404f90c782 Mon Sep 17 00:00:00 2001 From: Vadym Hrechukha Date: Thu, 8 Aug 2024 15:29:24 +0300 Subject: [PATCH 09/14] HP-2003: improved PriceSet API Unit test --- src/price/HasMoney.php | 21 +++++++++++++++++++++ src/price/ProgressivePrice.php | 17 ++++++++--------- src/price/SinglePrice.php | 16 +++++----------- 3 files changed, 34 insertions(+), 20 deletions(-) create mode 100644 src/price/HasMoney.php diff --git a/src/price/HasMoney.php b/src/price/HasMoney.php new file mode 100644 index 00000000..6ef65e4f --- /dev/null +++ b/src/price/HasMoney.php @@ -0,0 +1,21 @@ +price; + } + + public function getCurrency(): Currency + { + return $this->price->getCurrency(); + } +} \ No newline at end of file diff --git a/src/price/ProgressivePrice.php b/src/price/ProgressivePrice.php index 6b5340f2..65b83d8f 100644 --- a/src/price/ProgressivePrice.php +++ b/src/price/ProgressivePrice.php @@ -14,11 +14,14 @@ use Money\Money; use Money\Parser\DecimalMoneyParser; -class ProgressivePrice extends AbstractPrice implements PriceWithThresholdsInterface, PriceWithMoneyInterface, PriceWithQuantityInterface +class ProgressivePrice extends AbstractPrice implements PriceWithThresholdsInterface, + PriceWithMoneyInterface, + PriceWithQuantityInterface, + PriceWithCurrencyInterface { - protected ProgressivePriceThresholdList $thresholds; + use HasMoney; - protected Money $price; + protected ProgressivePriceThresholdList $thresholds; protected QuantityInterface $prepaid; @@ -30,7 +33,8 @@ public function __construct( Money $price, ProgressivePriceThresholdList $thresholds, ?PlanInterface $plan = null - ) { + ) + { parent::__construct($id, $type, $target, $plan); $this->thresholds = $thresholds; $this->price = $price; @@ -47,11 +51,6 @@ public function getPrepaid(): QuantityInterface return $this->prepaid; } - public function getPrice(): Money - { - return $this->price; - } - /** * @inheritDoc */ diff --git a/src/price/SinglePrice.php b/src/price/SinglePrice.php index a2c680e6..05f6de15 100644 --- a/src/price/SinglePrice.php +++ b/src/price/SinglePrice.php @@ -27,19 +27,18 @@ * * @author Andrii Vasyliev */ -class SinglePrice extends AbstractPrice implements PriceWithMoneyInterface, PriceWithQuantityInterface +class SinglePrice extends AbstractPrice implements PriceWithQuantityInterface, + PriceWithMoneyInterface, + PriceWithCurrencyInterface { + use HasMoney; + /** * @var QuantityInterface prepaid quantity also implies Unit * XXX cannot be null cause Unit is required */ protected $prepaid; - /** - * @var Money - */ - protected $price; - public function __construct( $id, TypeInterface $type, @@ -58,11 +57,6 @@ public function getPrepaid(): QuantityInterface return $this->prepaid; } - public function getPrice(): Money - { - return $this->price; - } - /** * {@inheritdoc} */ From 9afce09e5cc81968725471c367f1c2a36e7fc99a Mon Sep 17 00:00:00 2001 From: Vadym Hrechukha Date: Thu, 8 Aug 2024 19:11:54 +0300 Subject: [PATCH 10/14] HP-2003: fixed update float quantity in PriceSet API --- src/price/HasQuantity.php | 25 ++++++++++++++++++++++++ src/price/PriceWithMoneyInterface.php | 2 +- src/price/PriceWithQuantityInterface.php | 2 +- src/price/ProgressivePrice.php | 11 ++--------- src/price/SinglePrice.php | 16 ++------------- 5 files changed, 31 insertions(+), 25 deletions(-) create mode 100644 src/price/HasQuantity.php diff --git a/src/price/HasQuantity.php b/src/price/HasQuantity.php new file mode 100644 index 00000000..f407eb42 --- /dev/null +++ b/src/price/HasQuantity.php @@ -0,0 +1,25 @@ +prepaid; + } + + public function getUnit(): UnitInterface + { + return $this->prepaid->getUnit(); + } +} diff --git a/src/price/PriceWithMoneyInterface.php b/src/price/PriceWithMoneyInterface.php index e6918664..33d39b77 100644 --- a/src/price/PriceWithMoneyInterface.php +++ b/src/price/PriceWithMoneyInterface.php @@ -4,7 +4,7 @@ use Money\Money; -interface PriceWithMoneyInterface +interface PriceWithMoneyInterface extends PriceWithCurrencyInterface { public function getPrice(): Money; } diff --git a/src/price/PriceWithQuantityInterface.php b/src/price/PriceWithQuantityInterface.php index 8767ba41..5416c08f 100644 --- a/src/price/PriceWithQuantityInterface.php +++ b/src/price/PriceWithQuantityInterface.php @@ -4,7 +4,7 @@ use hiqdev\php\units\QuantityInterface; -interface PriceWithQuantityInterface +interface PriceWithQuantityInterface extends PriceWithUnitInterface { public function getPrepaid(): QuantityInterface; } diff --git a/src/price/ProgressivePrice.php b/src/price/ProgressivePrice.php index 65b83d8f..baabf712 100644 --- a/src/price/ProgressivePrice.php +++ b/src/price/ProgressivePrice.php @@ -16,15 +16,13 @@ class ProgressivePrice extends AbstractPrice implements PriceWithThresholdsInterface, PriceWithMoneyInterface, - PriceWithQuantityInterface, - PriceWithCurrencyInterface + PriceWithQuantityInterface { use HasMoney; + use HasQuantity; protected ProgressivePriceThresholdList $thresholds; - protected QuantityInterface $prepaid; - public function __construct( $id, TypeInterface $type, @@ -46,11 +44,6 @@ public function getThresholds(): ProgressivePriceThresholdList return $this->thresholds; } - public function getPrepaid(): QuantityInterface - { - return $this->prepaid; - } - /** * @inheritDoc */ diff --git a/src/price/SinglePrice.php b/src/price/SinglePrice.php index 05f6de15..320f6ab2 100644 --- a/src/price/SinglePrice.php +++ b/src/price/SinglePrice.php @@ -27,17 +27,10 @@ * * @author Andrii Vasyliev */ -class SinglePrice extends AbstractPrice implements PriceWithQuantityInterface, - PriceWithMoneyInterface, - PriceWithCurrencyInterface +class SinglePrice extends AbstractPrice implements PriceWithQuantityInterface, PriceWithMoneyInterface { use HasMoney; - - /** - * @var QuantityInterface prepaid quantity also implies Unit - * XXX cannot be null cause Unit is required - */ - protected $prepaid; + use HasQuantity; public function __construct( $id, @@ -52,11 +45,6 @@ public function __construct( $this->price = $price; } - public function getPrepaid(): QuantityInterface - { - return $this->prepaid; - } - /** * {@inheritdoc} */ From 733a35cf6878a6989d89556239ea89412d5da0ec Mon Sep 17 00:00:00 2001 From: Vadym Hrechukha Date: Thu, 8 Aug 2024 19:18:38 +0300 Subject: [PATCH 11/14] HP-2003: tiny --- src/price/HasMoney.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/price/HasMoney.php b/src/price/HasMoney.php index 6ef65e4f..e11b1ca1 100644 --- a/src/price/HasMoney.php +++ b/src/price/HasMoney.php @@ -18,4 +18,4 @@ public function getCurrency(): Currency { return $this->price->getCurrency(); } -} \ No newline at end of file +} From f6cee86e116183f467f43c29c9731ad01fa0dad9 Mon Sep 17 00:00:00 2001 From: Vadym Hrechukha Date: Fri, 9 Aug 2024 00:09:57 +0300 Subject: [PATCH 12/14] HP-2003: removed unused Formula class --- src/formula/Formula.php | 27 --------------------------- 1 file changed, 27 deletions(-) delete mode 100644 src/formula/Formula.php diff --git a/src/formula/Formula.php b/src/formula/Formula.php deleted file mode 100644 index cc610681..00000000 --- a/src/formula/Formula.php +++ /dev/null @@ -1,27 +0,0 @@ -value)) { - $this->value = $this->formulaEngine->normalize($this->value); - $this->validate($this->value); - } - } - - private function validate(string $value): void - { - $error = $this->formulaEngine->validate($value); - if ($error !== null) { - throw new FormulaEngineException($error); - } - } - - public function value(): ?string - { - return $this->value; - } -} From 5687bd6ddff9e4bd28d265549f5c7a9159e3bac9 Mon Sep 17 00:00:00 2001 From: Vadym Hrechukha Date: Sat, 31 Aug 2024 14:42:21 +0300 Subject: [PATCH 13/14] HP-2003: tiny --- src/price/AbstractPrice.php | 34 +++++++++++++++++++++++++++------- 1 file changed, 27 insertions(+), 7 deletions(-) diff --git a/src/price/AbstractPrice.php b/src/price/AbstractPrice.php index 23bc7ee2..0aefd2b4 100755 --- a/src/price/AbstractPrice.php +++ b/src/price/AbstractPrice.php @@ -137,7 +137,7 @@ public function calculateSum(QuantityInterface $quantity): ?Money public function jsonSerialize(): array { - return [ + $data = [ 'id' => $this->id, 'class' => (new \ReflectionClass($this))->getShortName(), 'type' => $this->getType()->getName(), @@ -149,13 +149,33 @@ public function jsonSerialize(): array 'type' => $this->getTarget()->getType(), ], 'plan_id' => $this->getPlan()?->getId(), - 'subprices' => $this instanceof PriceWithSubpriceInterface ? $this->getSubprices()->values() : null, - 'rate' => $this instanceof PriceWithRateInterface ? $this->getRate() : null, - 'unit' => $this instanceof PriceWithUnitInterface ? $this->getUnit()->getName() : null, - 'currency' => $this instanceof PriceWithCurrencyInterface ? $this->getCurrency()->getCode() : null, - 'sums' => $this instanceof PriceWithSumsInterface ? $this->getSums()->values() : null, - 'quantity' => $this instanceof PriceWithQuantityInterface ? $this->getPrepaid()->getQuantity() : 0, ]; + + if ($this instanceof PriceWithSubpriceInterface) { + $data['subprice'] = $this->getSubprices()->values(); + } + + if ($this instanceof PriceWithRateInterface) { + $data['rate'] = $this->getRate(); + } + + if ($this instanceof PriceWithUnitInterface) { + $data['unit'] = $this->getUnit()->getName(); + } + + if ($this instanceof PriceWithCurrencyInterface) { + $data['currency'] = $this->getCurrency()->getCode(); + } + + if ($this instanceof PriceWithSumsInterface) { + $data['sums'] = $this->getSums()->values(); + } + + if ($this instanceof PriceWithQuantityInterface) { + $data['quantity'] = $this->getPrepaid()->getQuantity(); + } + + return $data; } /** From acf673326bcfe16ba232a2c9f9a01282fbb953fa Mon Sep 17 00:00:00 2001 From: Vadym Hrechukha Date: Sat, 31 Aug 2024 14:46:22 +0300 Subject: [PATCH 14/14] HP-2003: tiny --- src/price/RatePrice.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/price/RatePrice.php b/src/price/RatePrice.php index fadad116..71fff5f2 100644 --- a/src/price/RatePrice.php +++ b/src/price/RatePrice.php @@ -26,8 +26,7 @@ */ class RatePrice extends AbstractPrice implements PriceWithRateInterface { - /** @var float */ - protected $rate; + protected float $rate; public function __construct( $id,