[Routing] Add EnumRequirement to help generate route requirements from a \BackedEnum#45803
Conversation
|
Hey! I think @usu has recently worked with this code. Maybe they can help review this? Cheers! Carsonbot |
|
Note: the alternative mentioned on Slack was to use a PHP 8 attribute to configure the |
|
If we really want to support subsets, we should give the subset of "what". Honestly, I find this quite ugly... but 🤷 |
|
What about renaming BackedEnumRequirement to EnumRequirement? I feel like adding "Backed" doesn't provide anything. |
|
I'll change the signature to |
It cannot since it will throw once ppl give it a try - and it's a logical error to expect that a pure enum could work as a part of matching a URL, so ppl that could have this expectation are wrong anyway. "BackedEnum" just adds to the ugliness of using enums to me... |
cb68e74 to
275d111
Compare
275d111 to
ce87606
Compare
|
Thank you @fancyweb. |
|
Similar solution for |
Both ways could be supported natively if php/php-src#8825 were available, reducing the boilerplate needed for common simple cases. |
Ref #44831
I'd like to limit a route parameter allowed values to the backed values of an enum to use it in conjunction with the new
\BackedEnumargument resolver (ie fail from the start).Also, sometimes, I'd like to limit it only to a subset of the backed values.
I couldn't find a way to do that because enums can't implement
__toString()and accessing->valueis not considered a constant operation.We can leverage the fact that route requirements can be a
\Stringable.Before (no enum):
Allow all enum cases:
Allow a subset:
Probably not the best solution but I hope we can find something for that use case for 6.1 😄
cc @ogizanagi