Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Config] Support passing the value of backed enums in enumNode #57659

Open
Seldaek opened this issue Jul 5, 2024 · 2 comments · May be fixed by #57686
Open

[Config] Support passing the value of backed enums in enumNode #57659

Seldaek opened this issue Jul 5, 2024 · 2 comments · May be fixed by #57686

Comments

@Seldaek
Copy link
Member

Seldaek commented Jul 5, 2024

Description

This idea came up while looking at this mess. I figured it would be nice if you could just pass ->enumNode(BackedEnum::cases()) or even ->enumNode(BackedEnum::class). Turns out it is possible (the former) thanks to #49098 but then if you do that the users have to also manage to create enum cases like foo: !php/enum \App\Enum\FooEnum::Bar, which requires a lot of internal knowledge and is not super user friendly.

So my request is.. Could we make it so that if all enumNode cases are actual enum instances and there is no plain string in there, and the enum is backed, then if a string is passed in as value in the config it would do enum::tryFrom(string) to convert it automatically?

Example

foo: "bar" would auto-convert to \App\Enum\FooEnum::Bar

@janmartenjongerius
Copy link

janmartenjongerius commented Jul 8, 2024

The only downside I could see, which is a faux pas anyway, is when someone changes the value of an enum case, to correct it, while keeping the case in-tact.

However, that's a risk one has to accept when configuring without explicitly using the enum case in the first place. Also, this same risk is present on non-enum configuration. At the very least, an enum already denotes the maintainer is more careful about selecting allowed values for configuration.

The risk could even be mitigated by the maintainer through overriding the tryFrom initializer and supporting the previously accepted value for the corrected case.

👍 I'm really stoked for this enhancement.

@alexandre-daubois
Copy link
Contributor

I really like this feature! I gave a try on the implementation, tell me what you think 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants