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 #46771 [Yaml] Add support for !php/enum *->value syntax (nicolas-grekas)
This PR was merged into the 6.2 branch.
Discussion
----------
[Yaml] Add support for `!php/enum *->value` syntax
| Q | A
| ------------- | ---
| Branch? | 6.2
| Bug fix? | no
| New feature? | yes
| Deprecations? | no
| Tickets | Fix #44203 and the likes
| License | MIT
| Doc PR | -
This PR allows one to use the following Yaml together with enums:
```yaml
- !php/enum SomeEnum::Bar
- !php/enum SomeEnum::Bar->value
```
The first line gets the `SomeEnum::Bar` instance. It's the same as writing `!php/const SomeEnum::Bar` but with an additional check that this is really an enum.
The second line is the one that is really needed as it allows referencing the value backed by an enum, which is something that is not possible currently.
Commits
-------
f71e4ab614 [Yaml] Add support for `!php/enum *->value` syntax
@@ -623,6 +623,40 @@ private static function evaluateScalar(string $scalar, int $flags, array &$refer
623
623
thrownewParseException(sprintf('The string "%s" could not be parsed as a constant. Did you forget to pass the "Yaml::PARSE_CONSTANT" flag to the parser?', $scalar), self::$parsedLineNumber + 1, $scalar, self::$parsedFilename);
624
624
}
625
625
626
+
returnnull;
627
+
casestr_starts_with($scalar, '!php/enum'):
628
+
if (self::$constantSupport) {
629
+
if (!isset($scalar[11])) {
630
+
thrownewParseException('Missing value for tag "!php/enum".', self::$parsedLineNumber + 1, $scalar, self::$parsedFilename);
if ($useValue = str_ends_with($enum, '->value')) {
636
+
$enum = substr($enum, 0, -7);
637
+
}
638
+
if (!\defined($enum)) {
639
+
thrownewParseException(sprintf('The enum "%s" is not defined.', $enum), self::$parsedLineNumber + 1, $scalar, self::$parsedFilename);
640
+
}
641
+
642
+
$value = \constant($enum);
643
+
644
+
if (!$valueinstanceof \UnitEnum) {
645
+
thrownewParseException(sprintf('The string "%s" is not the name of a valid enum.', $enum), self::$parsedLineNumber + 1, $scalar, self::$parsedFilename);
646
+
}
647
+
if (!$useValue) {
648
+
return$value;
649
+
}
650
+
if (!$valueinstanceof \BackedEnum) {
651
+
thrownewParseException(sprintf('The enum "%s" defines no value next to its name.', $enum), self::$parsedLineNumber + 1, $scalar, self::$parsedFilename);
652
+
}
653
+
654
+
return$value->value;
655
+
}
656
+
if (self::$exceptionOnInvalidType) {
657
+
thrownewParseException(sprintf('The string "%s" could not be parsed as an enum. Did you forget to pass the "Yaml::PARSE_CONSTANT" flag to the parser?', $scalar), self::$parsedLineNumber + 1, $scalar, self::$parsedFilename);
658
+
}
659
+
626
660
returnnull;
627
661
casestr_starts_with($scalar, '!!float '):
628
662
return (float) substr($scalar, 8);
@@ -703,7 +737,7 @@ private static function parseTag(string $value, int &$i, int $flags): ?string
0 commit comments