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; + } +}