From 0111ee2ac4429efdecaef337774dc8d07de4620d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Munsch?= Date: Thu, 13 Aug 2026 11:52:13 +0200 Subject: [PATCH] refactor(Employees): remove BaseEnum trait and clean up EmployeeSortColumn This change simplifies the EmployeeSortColumn enum by removing the BaseEnum trait, which is no longer needed. --- .../Employees/EmployeeSortColumn.php | 8 - src/Shared/Enums/BaseEnum.php | 171 ------------------ 2 files changed, 179 deletions(-) delete mode 100644 src/Shared/Enums/BaseEnum.php diff --git a/src/Domain/Models/ValueObjects/Employees/EmployeeSortColumn.php b/src/Domain/Models/ValueObjects/Employees/EmployeeSortColumn.php index beb4dd0..aa64ce6 100644 --- a/src/Domain/Models/ValueObjects/Employees/EmployeeSortColumn.php +++ b/src/Domain/Models/ValueObjects/Employees/EmployeeSortColumn.php @@ -4,16 +4,8 @@ declare(strict_types=1); namespace Domain\Models\ValueObjects\Employees; -use Shared\Enums\BaseEnum; - -/** - * @use BaseEnum - */ enum EmployeeSortColumn: string { - /** @use BaseEnum */ - use BaseEnum; - case arcadiaId = 'arcadiaId'; case startDate = 'startDate'; case endDate = 'endDate'; diff --git a/src/Shared/Enums/BaseEnum.php b/src/Shared/Enums/BaseEnum.php deleted file mode 100644 index 8a1e7e1..0000000 --- a/src/Shared/Enums/BaseEnum.php +++ /dev/null @@ -1,171 +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 $cache */ - static $cache = null; - - if ($cache !== null) { - return $cache; - } - - $rawLabels = defined(self::class . '::' . self::LABELS_CONSTANT_NAME) - ? constant(self::class . '::' . self::LABELS_CONSTANT_NAME) - : null; - - /** @var array|null $labels */ - $labels = is_array($rawLabels) ? $rawLabels : 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] = isset($labels[$enum->name]) ? (string) $labels[$enum->name] : ucfirst(strtolower($enum->name)); - } - - return $cache = $result; - } - - /** - * @return array - */ - public static function toValues(): array - { - /** @var array|null $cache */ - static $cache = null; - - if ($cache !== null) { - return $cache; - } - - /** @var array $result */ - $result = array_column( - self::cases(), - 'value', - 'name' - ); - - return $cache = $result; - } - - /** - * @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(object ...$others): bool - { - return in_array($this, $others, true); - } - - public function jsonSerialize(): mixed - { - return $this->value; - } - - // * PRIVATE - - /** - * @return array> - */ - private static function getCustomDefinitions(): array - { - /** @var array>|null $cache */ - static $cache = null; - - if ($cache !== null) { - return $cache; - } - - $names = array_column(self::cases(), 'name', 'name'); - /** @var array> $filteredConstants */ - $filteredConstants = []; - - foreach ((new \ReflectionClass(self::class))->getConstants() as $key => $constant) { - if ($key === self::LABELS_CONSTANT_NAME || ! \is_array($constant)) { - continue; - } - - /** @var array $constant */ - if (! empty(array_intersect_key($constant, $names))) { - $filteredConstants[$key] = $constant; - } - } - - return $cache = $filteredConstants; - } -}