Skip to content

Commit dd03330

Browse files
committed
[Yaml] Allow Yaml component to get all the enum cases
1 parent 0055b23 commit dd03330

File tree

3 files changed

+37
-13
lines changed

3 files changed

+37
-13
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
7.1
5+
---
6+
7+
* Add support for getting all the enum cases with `!php/enum Foo`
8+
49
7.0
510
---
611

Inline.php

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -643,24 +643,31 @@ private static function evaluateScalar(string $scalar, int $flags, array &$refer
643643
}
644644

645645
$i = 0;
646-
$enum = self::parseScalar(substr($scalar, 10), 0, null, $i, false);
647-
if ($useValue = str_ends_with($enum, '->value')) {
648-
$enum = substr($enum, 0, -7);
649-
}
650-
if (!\defined($enum)) {
646+
$enumName = self::parseScalar(substr($scalar, 10), 0, null, $i, false);
647+
$useName = str_contains($enumName, '::');
648+
$enum = $useName ? strstr($enumName, '::', true) : $enumName;
649+
650+
if (!enum_exists($enum)) {
651651
throw new ParseException(sprintf('The enum "%s" is not defined.', $enum), self::$parsedLineNumber + 1, $scalar, self::$parsedFilename);
652652
}
653+
if (!$useName) {
654+
return $enum::cases();
655+
}
656+
if ($useValue = str_ends_with($enumName, '->value')) {
657+
$enumName = substr($enumName, 0, -7);
658+
}
653659

654-
$value = \constant($enum);
655-
656-
if (!$value instanceof \UnitEnum) {
657-
throw new ParseException(sprintf('The string "%s" is not the name of a valid enum.', $enum), self::$parsedLineNumber + 1, $scalar, self::$parsedFilename);
660+
if (!\defined($enumName)) {
661+
throw new ParseException(sprintf('The string "%s" is not the name of a valid enum.', $enumName), self::$parsedLineNumber + 1, $scalar, self::$parsedFilename);
658662
}
663+
664+
$value = \constant($enumName);
665+
659666
if (!$useValue) {
660667
return $value;
661668
}
662669
if (!$value instanceof \BackedEnum) {
663-
throw new ParseException(sprintf('The enum "%s" defines no value next to its name.', $enum), self::$parsedLineNumber + 1, $scalar, self::$parsedFilename);
670+
throw new ParseException(sprintf('The enum "%s" defines no value next to its name.', $enumName), self::$parsedLineNumber + 1, $scalar, self::$parsedFilename);
664671
}
665672

666673
return $value->value;

Tests/InlineTest.php

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,14 +76,21 @@ public function testParsePhpConstantThrowsExceptionWhenUndefined()
7676
public function testParsePhpEnumThrowsExceptionWhenUndefined()
7777
{
7878
$this->expectException(ParseException::class);
79-
$this->expectExceptionMessage('The enum "SomeEnum::Foo" is not defined');
80-
Inline::parse('!php/enum SomeEnum::Foo', Yaml::PARSE_CONSTANT);
79+
$this->expectExceptionMessage('The enum "SomeEnum" is not defined');
80+
Inline::parse('!php/enum SomeEnum', Yaml::PARSE_CONSTANT);
81+
}
82+
83+
public function testParsePhpEnumThrowsExceptionWhenNameUndefined()
84+
{
85+
$this->expectException(ParseException::class);
86+
$this->expectExceptionMessage('The string "Symfony\Component\Yaml\Tests\Fixtures\FooUnitEnum::Foo" is not the name of a valid enum');
87+
Inline::parse('!php/enum Symfony\Component\Yaml\Tests\Fixtures\FooUnitEnum::Foo', Yaml::PARSE_CONSTANT);
8188
}
8289

8390
public function testParsePhpEnumThrowsExceptionWhenNotAnEnum()
8491
{
8592
$this->expectException(ParseException::class);
86-
$this->expectExceptionMessage('The string "PHP_INT_MAX" is not the name of a valid enum');
93+
$this->expectExceptionMessage('The enum "PHP_INT_MAX" is not defined');
8794
Inline::parse('!php/enum PHP_INT_MAX', Yaml::PARSE_CONSTANT);
8895
}
8996

@@ -718,6 +725,11 @@ public function testDumpUnitEnum()
718725
$this->assertSame("!php/const Symfony\Component\Yaml\Tests\Fixtures\FooUnitEnum::BAR", Inline::dump(FooUnitEnum::BAR));
719726
}
720727

728+
public function testParseUnitEnumCases()
729+
{
730+
$this->assertSame(FooUnitEnum::cases(), Inline::parse("!php/enum Symfony\Component\Yaml\Tests\Fixtures\FooUnitEnum", Yaml::PARSE_CONSTANT));
731+
}
732+
721733
public function testParseUnitEnum()
722734
{
723735
$this->assertSame(FooUnitEnum::BAR, Inline::parse("!php/enum Symfony\Component\Yaml\Tests\Fixtures\FooUnitEnum::BAR", Yaml::PARSE_CONSTANT));

0 commit comments

Comments
 (0)