mirror of
https://github.com/shlinkio/shlink.git
synced 2026-02-28 04:03:12 +08:00
Reduce duplicated code between enumValues and enumNames
This commit is contained in:
@@ -256,14 +256,7 @@ function toProblemDetailsType(string $errorCode): string
|
||||
*/
|
||||
function enumValues(string $enum): array
|
||||
{
|
||||
static $cache;
|
||||
if ($cache === null) {
|
||||
$cache = [];
|
||||
}
|
||||
|
||||
return $cache[$enum] ?? (
|
||||
$cache[$enum] = array_map(static fn (BackedEnum $type) => (string) $type->value, $enum::cases())
|
||||
);
|
||||
return enumSide($enum, 'value');
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -271,14 +264,27 @@ function enumValues(string $enum): array
|
||||
* @return string[]
|
||||
*/
|
||||
function enumNames(string $enum): array
|
||||
{
|
||||
return enumSide($enum, 'name');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param class-string<BackedEnum> $enum
|
||||
* @param 'name'|'value' $type
|
||||
* @return string[]
|
||||
*/
|
||||
function enumSide(string $enum, string $type): array
|
||||
{
|
||||
static $cache;
|
||||
if ($cache === null) {
|
||||
$cache = [];
|
||||
}
|
||||
|
||||
return $cache[$enum] ?? (
|
||||
$cache[$enum] = array_map(static fn (BackedEnum $type) => (string) $type->name, $enum::cases())
|
||||
return $cache[$type][$enum] ?? (
|
||||
$cache[$type][$enum] = array_map(
|
||||
static fn (BackedEnum $entry) => (string) ($type === 'name' ? $entry->name : $entry->value),
|
||||
$enum::cases(),
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user