-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Serializer] Support for PHP 8.1 enums #40241
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
Comments
IMO, only backed enums should be supported by default. If you want to use a pure enum while still serializing it, requiring a custom normalizer seems better to me. The name property is specifically meant as a debugging tool in the Enum RFC. Exposing it in your API looks wrong to me. |
regarding the implementation, this should be done by building a |
But enums can also implement Interfaces and expose public functions maybe in the ObjectNormalizer ? |
@Neirda24 : Hmm. 🤔 We might leverage #39399 to choose between serializing the enum as its simple scalar equivalent, or whether to serialize it as an object with serialized methods. |
@Neirda24 the |
@stof : sure. I was talking about the public methods that can automagically be serialized when serializing an object. I agree that enums should be kept simple objects. But this could be a use case as the gap between enums and objects is quite thin. Just making sure to explictly says that BackedEnum is not treated as other objects and will by default only dump the current value. |
Only one normalizer will run (the first supporting the type) |
I might try to write the normalizer if You choose me ;) I think that public methods shouldn't be serialized automatically. It's custom normalizer's job. |
I guess we need to wait for php/php-src#6489 to be merged first. :-) |
Yes. I'm just can't wait for enums and offer my help asap |
Done! |
…lexandre-daubois) This PR was merged into the 5.4 branch. Discussion ---------- [Serializer] Add support of PHP backed enumerations | Q | A | ------------- | --- | Branch? | 5.4 | Bug fix? | no | New feature? | yes | Deprecations? | no | Tickets | Fix #40241 <!-- prefix each issue number with "Fix #", no need to create an issue if none exist, explain below instead --> | License | MIT | Doc PR | _none_  Commits ------- 3458e0e [Serializer] Add support of PHP backed enumerations
Description
https://wiki.php.net/rfc/enumerations#backed_enums
Backed enums have scalar equivalents. The Serializer should be able to normalize and denormalize such enums.
Example
Should pure enums (no scalar equivalent) be supported as well, by relying on the name property? (
Suit::Spades->name
)The text was updated successfully, but these errors were encountered: