[FEATURE] Automatic enum conversion for arguments #1271
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
It is already possible to use PHP enums in Fluid templates: Both
ViewHelpers and components can define an enum as argument type by using
the full name of the enum. With the
<f:constant>ViewHelper, it ispossible to access individual enum cases. And enum cases are just
special PHP objects, which is why their properties (such as "name" or
"value") can be accessed with Fluid's variable syntax.
This patch aims to make the usage of enums for ViewHelper and
components easier: If an argument type is a known enum, Fluid
automatically tries to convert the supplied value to the matching enum
case. This works both for basic and backed enums: For backed enums,
Fluid first tries to find the enum case by its backed value and falls
back to the enum name. For basic enums, only the name is considered.
If the supplied value cannot be converted, the value remains unchanged,
which leads to the normal validation exception for a non-matching
argument type.
Given the following enum and component:
Before this change, the
<f:constant>ViewHelper needed to be usedif the value isn't already an enum:
With this change, the name of the enum case is sufficient:
And because the enum is int-backed, this also works: