Skip to content

Use value when printing BackedEnum#4124

Closed
ruudk wants to merge 1 commit intotwigphp:4.xfrom
ruudk:print-backed-enums
Closed

Use value when printing BackedEnum#4124
ruudk wants to merge 1 commit intotwigphp:4.xfrom
ruudk:print-backed-enums

Conversation

@ruudk
Copy link
Copy Markdown
Contributor

@ruudk ruudk commented Jul 7, 2024

Let's say you have the following Enum:

enum OrderStatus: string
{
    case Paid = 'paid';
}

Currently, we have to write {{ status.value }} to display an enum value.

Given that BackedEnums cannot implement Stringable / __toString, why not handle them in the EscapeRuntime?

Now we can write {{ status }} and it will print the value.

Currently, we have to write `{{ status.value }}` to display an enum value.

Given that BackedEnums cannot implement `Stringable` / `__toString`, why not handle them in
the EscapeRuntime?

Now we can write `{{ status }}` and it will print the value.
@smnandre
Copy link
Copy Markdown
Contributor

smnandre commented Jul 7, 2024

Only handled in the Escaper would lead to many frustrations/incomprehensions and a weird DX, as it won't work for filters for instance... and that would require a lot of code in many places..

Let's say you have a string my_string, a object my_stringable which class implements Stringable, and a string backed enum my_enum.

With your proposal, this would happen:

✅ {{ my_string }}
✅ {{ my_string|upper }}

✅ {{ my_stringable }}
✅ {{ my_stringable|upper }}  *

✅ {{ my_enum }}
❌ {{ my_enum|upper }}

Depending on the filters, call like the last one could either display the unchanged scalar value (with filters returning the value if not stringable) or throw an error (when type is checked).. with no predictability.

But... for filter that require pre-escaped arguments, then the escaped scalar equivalent would be used.

* my_stringable|upper works because strict_types are not set so Stringable are cast to string .. which won't be possible with BackedEnum https://3v4l.org/8gl3Z#v8.3.9 VS https://3v4l.org/HsQRW#v8.3.9)

So, as long PHP scalar backed enums are not (and cannot be) Stringable in PHP ... i don't think this should be done in Twig.

@fabpot
Copy link
Copy Markdown
Contributor

fabpot commented Jul 7, 2024

Maybe a more natural way would be to implement this feature in CoreExtension::getAttribute().

@stof
Copy link
Copy Markdown
Member

stof commented Jul 8, 2024

@fabpot converting backed enums to their values when reading them in getAttribute would be a huge BC break and would prevent using any method of the enum (accessing their value is not the only thing you can do with an enum)

@ruudk
Copy link
Copy Markdown
Contributor Author

ruudk commented Aug 30, 2024

Let's close this, unless somebody has a good idea on how to solve it 😊

@ruudk ruudk closed this Aug 30, 2024
@ruudk ruudk deleted the print-backed-enums branch August 30, 2024 12:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

4 participants