diff --git a/src/Options.php b/src/Options.php index 46dee50..0abcb3c 100644 --- a/src/Options.php +++ b/src/Options.php @@ -41,7 +41,20 @@ trait Options } // Default callback - $callback ??= fn ($name, $value) => "'; + $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 "'; + }; $options = array_map($callback, array_keys($options), array_values($options)); diff --git a/tests/Pest.php b/tests/Pest.php index e29ceac..4ca7350 100644 --- a/tests/Pest.php +++ b/tests/Pest.php @@ -61,3 +61,35 @@ enum Role #[Instructions('Guest users can only view the existing records')] case GUEST; } + +enum MultiWordSnakeCaseEnum +{ + use Options; + + case FOO_BAR; + case BAR_BAZ; +} + +enum BackedMultiWordSnakeCaseEnum: int +{ + use Options; + + case FOO_BAR = 0; + case BAR_BAZ = 1; +} + +enum PascalCaseEnum +{ + use Options; + + case FooBar; + case BarBaz; +} + +enum BackedPascalCaseEnum: int +{ + use Options; + + case FooBar = 0; + case BarBaz = 1; +} diff --git a/tests/Pest/OptionsTest.php b/tests/Pest/OptionsTest.php index 477158a..6974ca4 100644 --- a/tests/Pest/OptionsTest.php +++ b/tests/Pest/OptionsTest.php @@ -27,3 +27,16 @@ it('returns default HTML options from backed enums') it('returns default HTML options from pure enums') ->expect(Role::stringOptions()) ->toBe('\n'); + +it('returns default HTML options from pure enums with snake case') + ->expect(MultiWordSnakeCaseEnum::stringOptions()) + ->toBe('\n'); + +it('returns default HTML options from backed enums with snake case') + ->expect(BackedMultiWordSnakeCaseEnum::stringOptions()) + ->toBe('\n'); + +it('returns default HTML options from pure enums with pascal case') + ->expect(BackedPascalCaseEnum::stringOptions()) + ->toBe('\n'); +