diff --git a/src/Domain/Enums/BarcodeFormat.php b/src/Domain/Enums/BarcodeFormat.php index db2dc18..5c625c6 100644 --- a/src/Domain/Enums/BarcodeFormat.php +++ b/src/Domain/Enums/BarcodeFormat.php @@ -4,25 +4,9 @@ declare(strict_types=1); namespace Domain\Enums; -use Shared\Enums\BaseEnum; - -enum BarcodeFormat: int +enum BarcodeFormat: string { - /** @use BaseEnum */ - 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', - ]; + case EAN13 = "ean"; + case CODE128 = "code128"; + case GS1_128 = "gs1"; } diff --git a/src/Domain/Enums/Campaign/CampaignStatus.php b/src/Domain/Enums/Campaign/CampaignStatus.php index 96142de..c617a78 100644 --- a/src/Domain/Enums/Campaign/CampaignStatus.php +++ b/src/Domain/Enums/Campaign/CampaignStatus.php @@ -4,25 +4,9 @@ declare(strict_types=1); namespace Domain\Enums\Campaign; -use Shared\Enums\BaseEnum; - -enum CampaignStatus: int +enum CampaignStatus: string { - /** @use \Shared\Enums\BaseEnum */ - use BaseEnum; - - case CREATED = 1; - case TRANSFORMED = 2; - case REJECTED = 3; - - /** - * Optional labels definition - * - * @phpstan-ignore-next-line - */ - private const LABELS = [ - self::CREATED->name => 'Crée', - self::TRANSFORMED->name => 'Transformé', - self::REJECTED->name => 'Rejeté' - ]; + case CREATED = "created"; + case TRANSFORMED = "transformed"; + case REJECTED = "rejected"; } diff --git a/src/Domain/Enums/Campaign/CampaignType.php b/src/Domain/Enums/Campaign/CampaignType.php index 5728cb3..93cde5f 100644 --- a/src/Domain/Enums/Campaign/CampaignType.php +++ b/src/Domain/Enums/Campaign/CampaignType.php @@ -4,23 +4,8 @@ declare(strict_types=1); namespace Domain\Enums\Campaign; -use Shared\Enums\BaseEnum; - -enum CampaignType: int +enum CampaignType: string { - /** @use \Shared\Enums\BaseEnum */ - use BaseEnum; - - case CLUB = 1; - case GAME = 2; - - /** - * Optional labels definition - * - * @phpstan-ignore-next-line - */ - private const LABELS = [ - self::CLUB->name => 'CLUB', - self::GAME->name => 'GAME', - ]; + case CLUB = "club"; + case GAME = "game"; } diff --git a/src/Domain/Enums/Country.php b/src/Domain/Enums/Country.php index a75b0e4..969ac5e 100644 --- a/src/Domain/Enums/Country.php +++ b/src/Domain/Enums/Country.php @@ -4,25 +4,9 @@ declare(strict_types=1); namespace Domain\Enums; -use Shared\Enums\BaseEnum; - -enum Country: int +enum Country: string { - /** @use \Shared\Enums\BaseEnum */ - use BaseEnum; - - case FR = 1; - case BE = 2; - case LU = 3; - - /** - * Optional labels definition - * - * @phpstan-ignore-next-line - */ - private const LABELS = [ - self::FR->name => 'FR', - self::BE->name => 'BE', - self::LU->name => 'LU', - ]; + case FR = "fr"; + case BE = "be"; + case LU = "lu"; } diff --git a/src/Domain/Models/ValueObjects/Coupon/CouponLegalInformation.php b/src/Domain/Models/ValueObjects/Coupon/CouponLegalInformation.php index 3f4fbd5..8a83105 100644 --- a/src/Domain/Models/ValueObjects/Coupon/CouponLegalInformation.php +++ b/src/Domain/Models/ValueObjects/Coupon/CouponLegalInformation.php @@ -23,6 +23,6 @@ final readonly class CouponLegalInformation implements Equatable #[\Override] public function equals(mixed $other): bool { - return $other instanceof self && $this->legalText === $other->legalText && $this->forLocale->equals($other->forLocale); + return $other instanceof self && $this->legalText === $other->legalText && $this->forLocale === $other->forLocale; } } diff --git a/src/Domain/Models/ValueObjects/Coupon/CouponProductInfo.php b/src/Domain/Models/ValueObjects/Coupon/CouponProductInfo.php index 97928fc..8d2a70a 100644 --- a/src/Domain/Models/ValueObjects/Coupon/CouponProductInfo.php +++ b/src/Domain/Models/ValueObjects/Coupon/CouponProductInfo.php @@ -41,6 +41,6 @@ final readonly class CouponProductInfo implements Equatable && $this->name === $other->name && $this->range === $other->range && $this->validityText === $other->validityText - && $this->forLocale->equals($other->forLocale); + && $this->forLocale === $other->forLocale; } } diff --git a/src/Shared/Enums/BaseEnum.php b/src/Shared/Enums/BaseEnum.php deleted file mode 100644 index cb4bee0..0000000 --- a/src/Shared/Enums/BaseEnum.php +++ /dev/null @@ -1,133 +0,0 @@ -value; - } - - public function label(): string - { - return self::toLabels()[$this->name] ?? ucfirst(strtolower($this->name)); - } - - public function name(): string - { - return $this->name; - } - - /** - * @return array - */ - public static function toLabels(): array - { - /** @var array|null */ - static $cache = null; - - if ($cache !== null) { - return $cache; - } - - $labels = defined(self::class . '::' . self::LABELS_CONSTANT_NAME) - ? constant(self::class . '::' . self::LABELS_CONSTANT_NAME) - : null; - - $result = []; - foreach (self::cases() as $enum) { - if ($labels !== null && ! isset($labels[$enum->name])) { - throw new \Error('Missing definition label ' . self::class . " for '{$enum->name}'"); - } - $result[$enum->name] = $labels[$enum->name] ?? ucfirst(strtolower($enum->name)); - } - - return $cache = $result; - } - - /** - * @return array - */ - public static function toValues(): array - { - /** @var array|null */ - static $cache = null; - - if ($cache !== null) { - return $cache; - } - - return $cache = array_column( - self::cases(), - 'value', - 'name' - ); - } - - /** - * @param self|V $value - */ - public static function make(self | int | string $value, string $field = 'value'): ?self - { - if ($value instanceof self) { - return $value; - } - - if ($field === 'value') { - $tryValue = null; - try { - $tryValue = self::tryFrom(intval($value)); - } catch (\ValueError) { - $tryValue = self::tryFrom(strval($value)); - } finally { - if ($tryValue !== null) { - return $tryValue; - } - } - } - - if ($field === 'label') { - $normalizedSearch = preg_replace('/[\W_]+/u', '', mb_strtolower((string) $value)); - - foreach (self::cases() as $enum) { - $normalizedLabel = preg_replace('/[\W_]+/u', '', mb_strtolower($enum->label())); - if ($normalizedLabel === $normalizedSearch) { - return $enum; - } - } - - return null; - } - - foreach (self::cases() as $enum) { - if ($enum->{$field} === $value) { - return $enum; - } - } - - return null; - } - - public function equals(mixed $other): bool - { - return $this === $other; - } - - public function jsonSerialize(): mixed - { - return $this->value; - } -} diff --git a/src/Shared/Enums/Currency.php b/src/Shared/Enums/Currency.php index ac68c2d..93b8181 100644 --- a/src/Shared/Enums/Currency.php +++ b/src/Shared/Enums/Currency.php @@ -4,19 +4,7 @@ declare(strict_types=1); namespace Shared\Enums; -enum Currency: int +enum Currency: string { - /** @use BaseEnum */ - use BaseEnum; - - case EUR = 1; - - /** - * Optional labels definition - * - * @phpstan-ignore-next-line - */ - private const LABELS = [ - self::EUR->name => 'Euro', - ]; + case EUR = "euro"; } diff --git a/src/Shared/ValueObjects/Money.php b/src/Shared/ValueObjects/Money.php index 08529cb..f70521b 100644 --- a/src/Shared/ValueObjects/Money.php +++ b/src/Shared/ValueObjects/Money.php @@ -31,12 +31,12 @@ final readonly class Money implements \Stringable, Equatable public function equals(mixed $other): bool { return $other instanceof self - && $this->currency->equals($other->currency) + && $this->currency->value === $other->currency->value && $this->value === $other->value; } public function __toString(): string { - return sprintf('%d %s', $this->value, $this->currency->name()); + return sprintf('%d %s', $this->value, $this->currency->name); } } diff --git a/tests/Unit/Domain/Enums/BarcodeFormatTest.php b/tests/Unit/Domain/Enums/BarcodeFormatTest.php index b9c9882..e91bf1d 100644 --- a/tests/Unit/Domain/Enums/BarcodeFormatTest.php +++ b/tests/Unit/Domain/Enums/BarcodeFormatTest.php @@ -11,71 +11,13 @@ covers(BarcodeFormat::class); describe('barcode format', function (): void { it('has the expected cases with their backing values', function (): void { // THEN - expect(BarcodeFormat::EAN13->value())->toBe(1) - ->and(BarcodeFormat::CODE128->value())->toBe(2) - ->and(BarcodeFormat::GS1_128->value())->toBe(3); + expect(BarcodeFormat::EAN13->value)->toBe("ean") + ->and(BarcodeFormat::CODE128->value)->toBe("code128") + ->and(BarcodeFormat::GS1_128->value)->toBe("gs1"); }); it('exposes its case name', function (): void { // THEN - expect(BarcodeFormat::EAN13->name())->toBe('EAN13'); - }); - - it('returns the defined label', function (): void { - // THEN - expect(BarcodeFormat::EAN13->label())->toBe('EAN') - ->and(BarcodeFormat::CODE128->label())->toBe('CODE 128') - ->and(BarcodeFormat::GS1_128->label())->toBe('GS1_128'); - }); - - it('lists all labels keyed by case name', function (): void { - // THEN - expect(BarcodeFormat::toLabels())->toBe([ - 'EAN13' => 'EAN', - 'CODE128' => 'CODE 128', - 'GS1_128' => 'GS1_128', - ]); - }); - - it('lists all values keyed by case name', function (): void { - // THEN - expect(BarcodeFormat::toValues())->toBe([ - 'EAN13' => 1, - 'CODE128' => 2, - 'GS1_128' => 3, - ]); - }); - - it('makes an instance from its backing value', function (): void { - // THEN - expect(BarcodeFormat::make(1))->toBe(BarcodeFormat::EAN13); - }); - - it('makes an instance from a label', function (): void { - // THEN - expect(BarcodeFormat::make('CODE 128', 'label'))->toBe(BarcodeFormat::CODE128); - }); - - it('returns the same instance when already an enum case', function (): void { - // THEN - expect(BarcodeFormat::make(BarcodeFormat::GS1_128))->toBe(BarcodeFormat::GS1_128); - }); - - it('returns null for an unknown value', function (): void { - // THEN - expect(BarcodeFormat::make(99))->toBeNull(); - }); - - it('implements equals correctly', function (): void { - // THEN - expect(BarcodeFormat::EAN13->equals(BarcodeFormat::EAN13))->toBeTrue() - ->and(BarcodeFormat::EAN13->equals(BarcodeFormat::CODE128))->toBeFalse() - ->and(BarcodeFormat::EAN13->equals(null))->toBeFalse(); - }); - - it('serializes to json as its backing value', function (): void { - // THEN - expect(BarcodeFormat::EAN13->jsonSerialize())->toBe(1) - ->and(json_encode(BarcodeFormat::EAN13))->toBe('1'); + expect(BarcodeFormat::EAN13->name)->toBe('EAN13'); }); }); diff --git a/tests/Unit/Domain/Enums/Campaign/CampaignStatusTest.php b/tests/Unit/Domain/Enums/Campaign/CampaignStatusTest.php index 6f9df27..259264f 100644 --- a/tests/Unit/Domain/Enums/Campaign/CampaignStatusTest.php +++ b/tests/Unit/Domain/Enums/Campaign/CampaignStatusTest.php @@ -11,71 +11,13 @@ covers(CampaignStatus::class); describe('campaign status', function (): void { it('has the expected cases with their backing values', function (): void { // THEN - expect(CampaignStatus::CREATED->value())->toBe(1) - ->and(CampaignStatus::TRANSFORMED->value())->toBe(2) - ->and(CampaignStatus::REJECTED->value())->toBe(3); + expect(CampaignStatus::CREATED->value)->toBe("created") + ->and(CampaignStatus::TRANSFORMED->value)->toBe("transformed") + ->and(CampaignStatus::REJECTED->value)->toBe("rejected"); }); it('exposes its case name', function (): void { // THEN - expect(CampaignStatus::CREATED->name())->toBe('CREATED'); - }); - - it('returns the defined label', function (): void { - // THEN - expect(CampaignStatus::CREATED->label())->toBe('Crée') - ->and(CampaignStatus::TRANSFORMED->label())->toBe('Transformé') - ->and(CampaignStatus::REJECTED->label())->toBe('Rejeté'); - }); - - it('lists all labels keyed by case name', function (): void { - // THEN - expect(CampaignStatus::toLabels())->toBe([ - 'CREATED' => 'Crée', - 'TRANSFORMED' => 'Transformé', - 'REJECTED' => 'Rejeté', - ]); - }); - - it('lists all values keyed by case name', function (): void { - // THEN - expect(CampaignStatus::toValues())->toBe([ - 'CREATED' => 1, - 'TRANSFORMED' => 2, - 'REJECTED' => 3, - ]); - }); - - it('makes an instance from its backing value', function (): void { - // THEN - expect(CampaignStatus::make(1))->toBe(CampaignStatus::CREATED); - }); - - it('makes an instance from a label', function (): void { - // THEN - expect(CampaignStatus::make('Transformé', 'label'))->toBe(CampaignStatus::TRANSFORMED); - }); - - it('returns the same instance when already an enum case', function (): void { - // THEN - expect(CampaignStatus::make(CampaignStatus::REJECTED))->toBe(CampaignStatus::REJECTED); - }); - - it('returns null for an unknown value', function (): void { - // THEN - expect(CampaignStatus::make(99))->toBeNull(); - }); - - it('implements equals correctly', function (): void { - // THEN - expect(CampaignStatus::CREATED->equals(CampaignStatus::CREATED))->toBeTrue() - ->and(CampaignStatus::CREATED->equals(CampaignStatus::REJECTED))->toBeFalse() - ->and(CampaignStatus::CREATED->equals(null))->toBeFalse(); - }); - - it('serializes to json as its backing value', function (): void { - // THEN - expect(CampaignStatus::CREATED->jsonSerialize())->toBe(1) - ->and(json_encode(CampaignStatus::CREATED))->toBe('1'); + expect(CampaignStatus::CREATED->name)->toBe('CREATED'); }); }); diff --git a/tests/Unit/Domain/Enums/Campaign/CampaignTypeTest.php b/tests/Unit/Domain/Enums/Campaign/CampaignTypeTest.php index 2504339..f0d43b7 100644 --- a/tests/Unit/Domain/Enums/Campaign/CampaignTypeTest.php +++ b/tests/Unit/Domain/Enums/Campaign/CampaignTypeTest.php @@ -11,67 +11,12 @@ covers(CampaignType::class); describe('campaign type', function (): void { it('has the expected cases with their backing values', function (): void { // THEN - expect(CampaignType::CLUB->value())->toBe(1) - ->and(CampaignType::GAME->value())->toBe(2); + expect(CampaignType::CLUB->value)->toBe("club") + ->and(CampaignType::GAME->value)->toBe("game"); }); it('exposes its case name', function (): void { // THEN - expect(CampaignType::CLUB->name())->toBe('CLUB'); - }); - - it('returns the defined label', function (): void { - // THEN - expect(CampaignType::CLUB->label())->toBe('CLUB') - ->and(CampaignType::GAME->label())->toBe('GAME'); - }); - - it('lists all labels keyed by case name', function (): void { - // THEN - expect(CampaignType::toLabels())->toBe([ - 'CLUB' => 'CLUB', - 'GAME' => 'GAME', - ]); - }); - - it('lists all values keyed by case name', function (): void { - // THEN - expect(CampaignType::toValues())->toBe([ - 'CLUB' => 1, - 'GAME' => 2, - ]); - }); - - it('makes an instance from its backing value', function (): void { - // THEN - expect(CampaignType::make(1))->toBe(CampaignType::CLUB); - }); - - it('makes an instance from a label', function (): void { - // THEN - expect(CampaignType::make('GAME', 'label'))->toBe(CampaignType::GAME); - }); - - it('returns the same instance when already an enum case', function (): void { - // THEN - expect(CampaignType::make(CampaignType::CLUB))->toBe(CampaignType::CLUB); - }); - - it('returns null for an unknown value', function (): void { - // THEN - expect(CampaignType::make(99))->toBeNull(); - }); - - it('implements equals correctly', function (): void { - // THEN - expect(CampaignType::CLUB->equals(CampaignType::CLUB))->toBeTrue() - ->and(CampaignType::CLUB->equals(CampaignType::GAME))->toBeFalse() - ->and(CampaignType::CLUB->equals(null))->toBeFalse(); - }); - - it('serializes to json as its backing value', function (): void { - // THEN - expect(CampaignType::CLUB->jsonSerialize())->toBe(1) - ->and(json_encode(CampaignType::CLUB))->toBe('1'); + expect(CampaignType::CLUB->name)->toBe('CLUB'); }); }); diff --git a/tests/Unit/Domain/Enums/CountryTest.php b/tests/Unit/Domain/Enums/CountryTest.php index e9c49c6..3252171 100644 --- a/tests/Unit/Domain/Enums/CountryTest.php +++ b/tests/Unit/Domain/Enums/CountryTest.php @@ -11,71 +11,13 @@ covers(Country::class); describe('country', function (): void { it('has the expected cases with their backing values', function (): void { // THEN - expect(Country::FR->value())->toBe(1) - ->and(Country::BE->value())->toBe(2) - ->and(Country::LU->value())->toBe(3); + expect(Country::FR->value)->toBe("fr") + ->and(Country::BE->value)->toBe("be") + ->and(Country::LU->value)->toBe("lu"); }); it('exposes its case name', function (): void { // THEN - expect(Country::FR->name())->toBe('FR'); - }); - - it('returns the defined label', function (): void { - // THEN - expect(Country::FR->label())->toBe('FR') - ->and(Country::BE->label())->toBe('BE') - ->and(Country::LU->label())->toBe('LU'); - }); - - it('lists all labels keyed by case name', function (): void { - // THEN - expect(Country::toLabels())->toBe([ - 'FR' => 'FR', - 'BE' => 'BE', - 'LU' => 'LU', - ]); - }); - - it('lists all values keyed by case name', function (): void { - // THEN - expect(Country::toValues())->toBe([ - 'FR' => 1, - 'BE' => 2, - 'LU' => 3, - ]); - }); - - it('makes an instance from its backing value', function (): void { - // THEN - expect(Country::make(1))->toBe(Country::FR); - }); - - it('makes an instance from a label', function (): void { - // THEN - expect(Country::make('BE', 'label'))->toBe(Country::BE); - }); - - it('returns the same instance when already an enum case', function (): void { - // THEN - expect(Country::make(Country::LU))->toBe(Country::LU); - }); - - it('returns null for an unknown value', function (): void { - // THEN - expect(Country::make(99))->toBeNull(); - }); - - it('implements equals correctly', function (): void { - // THEN - expect(Country::FR->equals(Country::FR))->toBeTrue() - ->and(Country::FR->equals(Country::BE))->toBeFalse() - ->and(Country::FR->equals(null))->toBeFalse(); - }); - - it('serializes to json as its backing value', function (): void { - // THEN - expect(Country::FR->jsonSerialize())->toBe(1) - ->and(json_encode(Country::FR))->toBe('1'); + expect(Country::FR->name)->toBe('FR'); }); }); diff --git a/tests/Unit/Shared/Enums/CurrencyTest.php b/tests/Unit/Shared/Enums/CurrencyTest.php index 0f01dd3..1d0a2a3 100644 --- a/tests/Unit/Shared/Enums/CurrencyTest.php +++ b/tests/Unit/Shared/Enums/CurrencyTest.php @@ -11,56 +11,6 @@ covers(Currency::class); describe('currency', function (): void { it('has the expected cases with their backing values', function (): void { // THEN - expect(Currency::EUR->value())->toBe(1); - }); - - it('exposes its case name', function (): void { - // THEN - expect(Currency::EUR->name())->toBe('EUR'); - }); - - it('returns the defined label', function (): void { - // THEN - expect(Currency::EUR->label())->toBe('Euro'); - }); - - it('lists all labels keyed by case name', function (): void { - // THEN - expect(Currency::toLabels())->toBe([ - 'EUR' => 'Euro', - ]); - }); - - it('lists all values keyed by case name', function (): void { - // THEN - expect(Currency::toValues())->toBe([ - 'EUR' => 1, - ]); - }); - - it('makes an instance from its backing value', function (): void { - // THEN - expect(Currency::make(1))->toBe(Currency::EUR); - }); - - it('makes an instance from a label', function (): void { - // THEN - expect(Currency::make('Euro', 'label'))->toBe(Currency::EUR); - }); - - it('returns the same instance when already an enum case', function (): void { - // THEN - expect(Currency::make(Currency::EUR))->toBe(Currency::EUR); - }); - - it('returns null for an unknown value', function (): void { - // THEN - expect(Currency::make(99))->toBeNull(); - }); - - it('implements equals correctly', function (): void { - // THEN - expect(Currency::EUR->equals(Currency::EUR))->toBeTrue() - ->and(Currency::EUR->equals(null))->toBeFalse(); + expect(Currency::EUR->value)->toBe("euro"); }); });