Skip to content
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

[Yaml] Allow to get all the enum cases #52230

Merged
merged 1 commit into from
Feb 3, 2024
Merged

Conversation

phansys
Copy link
Contributor

@phansys phansys commented Oct 22, 2023

Q A
Branch? 7.1
Bug fix? no
New feature? yes
Deprecations? no
Issues n/a
License MIT

With this addition, the !php/enum syntax is allowed to expose an array with all the enum cases (the result from UnitEnum::cases()). This is useful for cases like choices option from the Choice validation constraint:

BEFORE:

App\Entity\User:
    properties:
        status:
            - Choice:
                choices:
                    - !php/enum 'App\Entity\Enum\UserStatus::Enabled'
                    - !php/enum 'App\Entity\Enum\UserStatus::Disabled'
                    - !php/enum 'App\Entity\Enum\UserStatus::Blocked'

AFTER:

App\Entity\User:
    properties:
        status:
            - Choice:
                choices: !php/enum 'App\Entity\Enum\UserStatus'

Prior to the support for enumerations, this was allowed by array constants:

App\Entity\User:
    properties:
        status:
            - Choice:
                choices: !php/const 'App\Entity\User::AVAILABLE_STATUSES'

@derrabus
Copy link
Member

Please provide a meaningful PR description that explains the feature you're adding, how to use it and what's your motivation behind it. This helps us deciding whether we want to add your feature to Symfony. In addition to that, it helps the documentation team who will probably want to document your feature and announce it in our blog.

@phansys
Copy link
Contributor Author

phansys commented Oct 22, 2023

Please provide a meaningful PR description that explains the feature you're adding, how to use it and what's your motivation behind it. This helps us deciding whether we want to add your feature to Symfony. In addition to that, it helps the documentation team who will probably want to document your feature and announce it in our blog.

I've added an example with a use case. Please, let me know if more context is required.
Thank you.

@GromNaN
Copy link
Member

GromNaN commented Oct 23, 2023

Prior to the support for enumerations, this was allowed by array constants

It is still possible to add a constant to an enum.
The proposed syntax seems good and concise.

@OskarStark OskarStark changed the title [Yaml] Allow Yaml component to get all the enum cases [Yaml] Allow to get all the enum cases Oct 23, 2023
@fabpot
Copy link
Member

fabpot commented Feb 3, 2024

Thank you @phansys.

@fabpot fabpot merged commit d00444c into symfony:7.1 Feb 3, 2024
4 of 9 checks passed
@phansys phansys deleted the yaml_enum branch February 3, 2024 17:28
@fabpot fabpot mentioned this pull request May 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants