You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
feature #52230 [Yaml] Allow to get all the enum cases (phansys)
This PR was merged into the 7.1 branch.
Discussion
----------
[Yaml] Allow to get all the enum cases
| Q | A
| ------------- | ---
| Branch? | 7.1
| Bug fix? | no
| New feature? | yes
| Deprecations? | no
| Issues | n/a
| License | MIT
<!--
Replace this notice by a description of your feature/bugfix.
This will help reviewers and should be a good start for the documentation.
Additionally (see https://symfony.com/releases):
- Always add tests and ensure they pass.
- Bug fixes must be submitted against the lowest maintained branch where they apply
(lowest branches are regularly merged to upper ones so they get the fixes too).
- Features and deprecations must be submitted against the latest branch.
- For new features, provide some code snippets to help understand usage.
- Changelog entry should follow https://symfony.com/doc/current/contributing/code/conventions.html#writing-a-changelog-entry
- Never break backward compatibility (see https://symfony.com/bc).
-->
With this addition, the `!php/enum` syntax is allowed to expose an array with all the enum cases (the result from [`UnitEnum::cases()`](https://www.php.net/manual/en/unitenum.cases.php)). This is useful for cases like `choices` option from the `Choice` validation constraint:
**BEFORE**:
```yaml
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**:
```yaml
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:
```yaml
App\Entity\User:
properties:
status:
- Choice:
choices: !php/const 'App\Entity\User::AVAILABLE_STATUSES'
```
Commits
-------
3286539bef [Yaml] Allow Yaml component to get all the enum cases
thrownewParseException(sprintf('The enum "%s" is not defined.', $enum), self::$parsedLineNumber + 1, $scalar, self::$parsedFilename);
652
652
}
653
+
if (!$useName) {
654
+
return$enum::cases();
655
+
}
656
+
if ($useValue = str_ends_with($enumName, '->value')) {
657
+
$enumName = substr($enumName, 0, -7);
658
+
}
653
659
654
-
$value = \constant($enum);
655
-
656
-
if (!$valueinstanceof \UnitEnum) {
657
-
thrownewParseException(sprintf('The string "%s" is not the name of a valid enum.', $enum), self::$parsedLineNumber + 1, $scalar, self::$parsedFilename);
660
+
if (!\defined($enumName)) {
661
+
thrownewParseException(sprintf('The string "%s" is not the name of a valid enum.', $enumName), self::$parsedLineNumber + 1, $scalar, self::$parsedFilename);
658
662
}
663
+
664
+
$value = \constant($enumName);
665
+
659
666
if (!$useValue) {
660
667
return$value;
661
668
}
662
669
if (!$valueinstanceof \BackedEnum) {
663
-
thrownewParseException(sprintf('The enum "%s" defines no value next to its name.', $enum), self::$parsedLineNumber + 1, $scalar, self::$parsedFilename);
670
+
thrownewParseException(sprintf('The enum "%s" defines no value next to its name.', $enumName), self::$parsedLineNumber + 1, $scalar, self::$parsedFilename);
0 commit comments