From 6c8301f158fa44ce1b6fdcdb3d86b3668a66825a Mon Sep 17 00:00:00 2001 From: Corentin Huet Date: Mon, 21 Sep 2026 16:59:26 +0200 Subject: [PATCH] feat: :sparkles: create Coupon entity and associated VOs --- src/Domain/Enums/BarcodeFormat.php | 28 ++++ src/Domain/Models/Entities/Coupon.php | 132 ++++++++++++++++++ .../Coupon/CouponAvailabilityCalendar.php | 48 +++++++ .../ValueObjects/Coupon/CouponDiffusion.php | 19 +++ .../Models/ValueObjects/Coupon/CouponId.php | 16 +++ .../Coupon/CouponLegalInformation.php | 28 ++++ .../ValueObjects/Coupon/CouponProduct.php | 20 +++ .../ValueObjects/Coupon/CouponProductInfo.php | 34 +++++ .../ValueObjects/Coupon/CouponValidity.php | 26 ++++ src/Domain/Models/ValueObjects/CouponName.php | 18 +++ .../Models/ValueObjects/CouponPicture.php | 23 +++ .../Models/ValueObjects/CouponProductName.php | 18 +++ src/Domain/Models/ValueObjects/GS1.php | 33 +++++ src/Domain/Models/ValueObjects/IncoCode.php | 27 ++++ .../Models/ValueObjects/ProcessingCode.php | 31 ++++ 15 files changed, 501 insertions(+) create mode 100644 src/Domain/Enums/BarcodeFormat.php create mode 100644 src/Domain/Models/Entities/Coupon.php create mode 100644 src/Domain/Models/ValueObjects/Coupon/CouponAvailabilityCalendar.php create mode 100644 src/Domain/Models/ValueObjects/Coupon/CouponDiffusion.php create mode 100644 src/Domain/Models/ValueObjects/Coupon/CouponId.php create mode 100644 src/Domain/Models/ValueObjects/Coupon/CouponLegalInformation.php create mode 100644 src/Domain/Models/ValueObjects/Coupon/CouponProduct.php create mode 100644 src/Domain/Models/ValueObjects/Coupon/CouponProductInfo.php create mode 100644 src/Domain/Models/ValueObjects/Coupon/CouponValidity.php create mode 100644 src/Domain/Models/ValueObjects/CouponName.php create mode 100644 src/Domain/Models/ValueObjects/CouponPicture.php create mode 100644 src/Domain/Models/ValueObjects/CouponProductName.php create mode 100644 src/Domain/Models/ValueObjects/GS1.php create mode 100644 src/Domain/Models/ValueObjects/IncoCode.php create mode 100644 src/Domain/Models/ValueObjects/ProcessingCode.php diff --git a/src/Domain/Enums/BarcodeFormat.php b/src/Domain/Enums/BarcodeFormat.php new file mode 100644 index 0000000..db2dc18 --- /dev/null +++ b/src/Domain/Enums/BarcodeFormat.php @@ -0,0 +1,28 @@ + */ + use BaseEnum; + + case EAN13 = 1; + case CODE128 = 2; + case GS1_128 = 3; + + /** + * Optional labels definition + * + * @phpstan-ignore-next-line + */ + private const LABELS = [ + self::EAN13->name => 'EAN', + self::CODE128->name => 'CODE 128', + self::GS1_128->name => 'GS1_128', + ]; +} diff --git a/src/Domain/Models/Entities/Coupon.php b/src/Domain/Models/Entities/Coupon.php new file mode 100644 index 0000000..fdd285b --- /dev/null +++ b/src/Domain/Models/Entities/Coupon.php @@ -0,0 +1,132 @@ + + */ +final readonly class Coupon implements AggregateRoot, HasTimestampIdentifier +{ + private function __construct( + public CouponId $id, + public CouponName $name, + public bool $status, + public CouponProductName $productName, + public MoneyAmount $amount, + public CouponPicture $picture, + public CouponProduct $product, + public CouponDiffusion $diffusion, + public CouponAvailabilityCalendar $availability, + public CouponValidity $validity, + public CouponLegalInformation $legal, + public GS1 $gtin, + public ProcessingCode $code, + public BrandCode $brandCode, + public ?\DateTimeInterface $updatedAt = null, + ) { + } + + public function id(): CouponId + { + return $this->id; + } + + public function createdAt(): \DateTimeInterface + { + return $this->id->toDateTime(); + } + + public function updatedAt(): ?\DateTimeInterface + { + return $this->updatedAt; + } + + public static function create( + CouponName $name, + bool $status, + CouponProductName $productName, + MoneyAmount $amount, + CouponPicture $picture, + CouponProduct $product, + CouponDiffusion $diffusion, + CouponAvailabilityCalendar $availability, + CouponValidity $validity, + CouponLegalInformation $legal, + GS1 $gtin, + ProcessingCode $code, + BrandCode $brandCode, + ?\DateTimeInterface $updatedAt = null, + ): Coupon { + return new self( + id: CouponId::generate(), + name: $name, + status: $status, + productName: $productName, + amount: $amount, + picture: $picture, + product: $product, + diffusion: $diffusion, + availability: $availability, + validity: $validity, + legal: $legal, + gtin: $gtin, + code: $code, + brandCode: $brandCode, + updatedAt: $updatedAt, + ); + } + + public static function hydrate( + CouponId $id, + CouponName $name, + bool $status, + CouponProductName $productName, + MoneyAmount $amount, + CouponPicture $picture, + CouponProduct $product, + CouponDiffusion $diffusion, + CouponAvailabilityCalendar $availability, + CouponValidity $validity, + CouponLegalInformation $legal, + GS1 $gtin, + ProcessingCode $code, + BrandCode $brandCode, + ?\DateTimeInterface $updatedAt = null, + ): self { + return new self( + id: $id, + name: $name, + status: $status, + productName: $productName, + amount: $amount, + picture: $picture, + product: $product, + diffusion: $diffusion, + availability: $availability, + validity: $validity, + legal: $legal, + gtin: $gtin, + code: $code, + brandCode: $brandCode, + updatedAt: $updatedAt, + ); + } +} diff --git a/src/Domain/Models/ValueObjects/Coupon/CouponAvailabilityCalendar.php b/src/Domain/Models/ValueObjects/Coupon/CouponAvailabilityCalendar.php new file mode 100644 index 0000000..9dd9395 --- /dev/null +++ b/src/Domain/Models/ValueObjects/Coupon/CouponAvailabilityCalendar.php @@ -0,0 +1,48 @@ +periods === []) { + throw new \DomainException('Diffusion requires at least one period.'); + } + } + + public function isActiveAt(DateTime $in): bool + { + foreach ($this->periods as $period) { + if ($in >= $period->start && $in <= $period->end) { + return true; + } + } + + return false; + } + + public function beginsAt(): DateTime + { + return min(array_map(fn (Period $period) => $period->start, $this->periods)); + } + + public function endsAt(): DateTime + { + return max(array_map(fn (Period $period) => $period->end, $this->periods)); + } + + #[\Override] + public function equals(mixed $other): bool + { + return $other instanceof self && $this->periods == $other->periods; + } +} diff --git a/src/Domain/Models/ValueObjects/Coupon/CouponDiffusion.php b/src/Domain/Models/ValueObjects/Coupon/CouponDiffusion.php new file mode 100644 index 0000000..1a0e4e0 --- /dev/null +++ b/src/Domain/Models/ValueObjects/Coupon/CouponDiffusion.php @@ -0,0 +1,19 @@ +periods === []) { + throw new \DomainException('Diffusion requires at least one period.'); + } + } +} diff --git a/src/Domain/Models/ValueObjects/Coupon/CouponId.php b/src/Domain/Models/ValueObjects/Coupon/CouponId.php new file mode 100644 index 0000000..f8b9734 --- /dev/null +++ b/src/Domain/Models/ValueObjects/Coupon/CouponId.php @@ -0,0 +1,16 @@ +legalText); + + if ($trimmed === '') { + throw new \DomainException('Legal text cannot be empty.'); + } + } + + #[\Override] + public function equals(mixed $other): bool + { + return $other instanceof self && $this->legalText === $other->legalText && $this->forLocale->equals($other->forLocale); + } +} diff --git a/src/Domain/Models/ValueObjects/Coupon/CouponProduct.php b/src/Domain/Models/ValueObjects/Coupon/CouponProduct.php new file mode 100644 index 0000000..5df5bfd --- /dev/null +++ b/src/Domain/Models/ValueObjects/Coupon/CouponProduct.php @@ -0,0 +1,20 @@ +name); + + if ($trimmed === '') { + throw new \DomainException('Product Info name cannot be empty.'); + } + } + + #[\Override] + public function equals(mixed $other): bool + { + return $other instanceof self + && $this->name === $other->name + && $this->range === $other->range + && $this->validityText === $other->validityText + && $this->forLocale->equals($other->forLocale); + } +} diff --git a/src/Domain/Models/ValueObjects/Coupon/CouponValidity.php b/src/Domain/Models/ValueObjects/Coupon/CouponValidity.php new file mode 100644 index 0000000..a5304c9 --- /dev/null +++ b/src/Domain/Models/ValueObjects/Coupon/CouponValidity.php @@ -0,0 +1,26 @@ +days <= 0) { + throw new \DomainException('Coupon Validity days must be positive.'); + } + } + + #[\Override] + public function equals(mixed $other): bool + { + return $other instanceof self && $this->days === $other->days && $this->until == $other->until; + } +} diff --git a/src/Domain/Models/ValueObjects/CouponName.php b/src/Domain/Models/ValueObjects/CouponName.php new file mode 100644 index 0000000..764da54 --- /dev/null +++ b/src/Domain/Models/ValueObjects/CouponName.php @@ -0,0 +1,18 @@ +product->equals($other->product) && $this->thumbnail->equals($other->thumbnail); + } +} diff --git a/src/Domain/Models/ValueObjects/CouponProductName.php b/src/Domain/Models/ValueObjects/CouponProductName.php new file mode 100644 index 0000000..3812226 --- /dev/null +++ b/src/Domain/Models/ValueObjects/CouponProductName.php @@ -0,0 +1,18 @@ +value); + + if ($trimmed === '') { + throw new \DomainException('GS1 value cannot be empty.'); + } + } + + #[\Override] + public function equals(mixed $other): bool + { + return $other instanceof self && $this->value === $other->value && $this->format === $other->format; + } + + public function __toString(): string + { + return $this->value; + } +} diff --git a/src/Domain/Models/ValueObjects/IncoCode.php b/src/Domain/Models/ValueObjects/IncoCode.php new file mode 100644 index 0000000..e830ad6 --- /dev/null +++ b/src/Domain/Models/ValueObjects/IncoCode.php @@ -0,0 +1,27 @@ +value <= 0) { + throw new \DomainException('Inco Code must be a positive integer.'); + } + } + + #[\Override] + public function equals(mixed $other): bool + { + return $other instanceof self && $this->value === $other->value; + } +} diff --git a/src/Domain/Models/ValueObjects/ProcessingCode.php b/src/Domain/Models/ValueObjects/ProcessingCode.php new file mode 100644 index 0000000..8a55717 --- /dev/null +++ b/src/Domain/Models/ValueObjects/ProcessingCode.php @@ -0,0 +1,31 @@ +value); + + if ($trimmed === '') { + throw new \DomainException('GS1 value cannot be empty.'); + } + } + + #[\Override] + public function equals(mixed $other): bool + { + return $other instanceof self && $this->value === $other->value; + } + + public function __toString(): string + { + return $this->value; + } +}