[FEATURE] Automatic enum conversion for arguments
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 is
possible 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:
namespace Vendor\Package;
enum VariantEnum: int
{
case BASIC = 123456;
}
<f:argument name="variant" type="Vendor\Package\VariantEnum" />
Selected variant: {variant.value}
Before this change, the <f:constant> ViewHelper needed to be used
if the value isn't already an enum:
<my:exampleComponent
variant="{f:constant(name: 'Vendor\Package\VariantEnum::BASIC')}"
/>
With this change, the name of the enum case is sufficient:
<my:exampleComponent
variant="BASIC"
/>
And because the enum is int-backed, this also works:
<my:exampleComponent
variant="123456"
/>
Related: #1044