mirror of
https://github.com/archtechx/enums.git
synced 2025-12-12 23:14:04 +00:00
Support UPPER_SNAKE_CASE and PascalCase in stringOptions() (resolve #22)
This commit is contained in:
parent
fc428d533d
commit
fea911eba7
3 changed files with 59 additions and 1 deletions
|
|
@ -41,7 +41,20 @@ trait Options
|
|||
}
|
||||
|
||||
// Default callback
|
||||
$callback ??= fn ($name, $value) => "<option value=\"{$value}\">" . ucfirst(strtolower($name)) . '</option>';
|
||||
$callback ??= function ($name, $value) {
|
||||
if (str_contains($name, '_')) {
|
||||
// Snake case
|
||||
$words = explode('_', $name);
|
||||
} else if (strtoupper($name) === $name) {
|
||||
// If the entire name is uppercase without underscores, it's a single word
|
||||
$words = [$name];
|
||||
} else {
|
||||
// Pascal case or camel case
|
||||
$words = array_filter(preg_split('/(?=[A-Z])/', $name));
|
||||
}
|
||||
|
||||
return "<option value=\"{$value}\">" . ucfirst(strtolower(implode(' ', $words))) . '</option>';
|
||||
};
|
||||
|
||||
$options = array_map($callback, array_keys($options), array_values($options));
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue