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.
This commit is contained in:
parent
198da8a11c
commit
0111ee2ac4
2 changed files with 0 additions and 179 deletions
|
|
@ -4,16 +4,8 @@ declare(strict_types=1);
|
|||
|
||||
namespace Domain\Models\ValueObjects\Employees;
|
||||
|
||||
use Shared\Enums\BaseEnum;
|
||||
|
||||
/**
|
||||
* @use BaseEnum<string>
|
||||
*/
|
||||
enum EmployeeSortColumn: string
|
||||
{
|
||||
/** @use BaseEnum<string> */
|
||||
use BaseEnum;
|
||||
|
||||
case arcadiaId = 'arcadiaId';
|
||||
case startDate = 'startDate';
|
||||
case endDate = 'endDate';
|
||||
|
|
|
|||
|
|
@ -1,171 +0,0 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Shared\Enums;
|
||||
|
||||
/**
|
||||
* @template V of string|int
|
||||
*
|
||||
* @mixin \BackedEnum
|
||||
*/
|
||||
trait BaseEnum
|
||||
{
|
||||
private const LABELS_CONSTANT_NAME = 'LABELS';
|
||||
|
||||
/**
|
||||
* @return V
|
||||
*/
|
||||
public function value(): string | int
|
||||
{
|
||||
return $this->value;
|
||||
}
|
||||
|
||||
public function label(): string
|
||||
{
|
||||
return self::toLabels()[$this->name] ?? ucfirst(strtolower($this->name));
|
||||
}
|
||||
|
||||
public function name(): string
|
||||
{
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string,string>
|
||||
*/
|
||||
public static function toLabels(): array
|
||||
{
|
||||
/** @var array<string, string>|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<string, string>|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<string, V>
|
||||
*/
|
||||
public static function toValues(): array
|
||||
{
|
||||
/** @var array<string, V>|null $cache */
|
||||
static $cache = null;
|
||||
|
||||
if ($cache !== null) {
|
||||
return $cache;
|
||||
}
|
||||
|
||||
/** @var array<string, V> $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<string, array<string, mixed>>
|
||||
*/
|
||||
private static function getCustomDefinitions(): array
|
||||
{
|
||||
/** @var array<string, array<string, mixed>>|null $cache */
|
||||
static $cache = null;
|
||||
|
||||
if ($cache !== null) {
|
||||
return $cache;
|
||||
}
|
||||
|
||||
$names = array_column(self::cases(), 'name', 'name');
|
||||
/** @var array<string, array<string, mixed>> $filteredConstants */
|
||||
$filteredConstants = [];
|
||||
|
||||
foreach ((new \ReflectionClass(self::class))->getConstants() as $key => $constant) {
|
||||
if ($key === self::LABELS_CONSTANT_NAME || ! \is_array($constant)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
/** @var array<string, mixed> $constant */
|
||||
if (! empty(array_intersect_key($constant, $names))) {
|
||||
$filteredConstants[$key] = $constant;
|
||||
}
|
||||
}
|
||||
|
||||
return $cache = $filteredConstants;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue