diff --git a/module/Core/functions/functions.php b/module/Core/functions/functions.php index a4126a13..72406e6a 100644 --- a/module/Core/functions/functions.php +++ b/module/Core/functions/functions.php @@ -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 $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(), + ) ); }