Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a1ab166

Browse files
committed
[Yaml] Allow Yaml component to get all the enum cases
1 parent fafbbfa commit a1ab166

File tree

3 files changed

+39
-13
lines changed

3 files changed

+39
-13
lines changed

src/Symfony/Component/Yaml/CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
6.4
5+
---
6+
7+
* Add support for getting all the enum cases with `!php/enum Foo` and `!php/enum Foo::cases()`
8+
49
6.3
510
---
611

src/Symfony/Component/Yaml/Inline.php

+18-10
Original file line numberDiff line numberDiff line change
@@ -643,24 +643,32 @@ 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+
$containsScopeResolutionOperator = str_contains($enumName, '::');
648+
$useName = $containsScopeResolutionOperator && !str_ends_with($enumName, '::cases()');
649+
$enum = $containsScopeResolutionOperator ? strstr($enumName, '::', true) : $enumName;
650+
651+
if (!enum_exists($enum)) {
651652
throw new ParseException(sprintf('The enum "%s" is not defined.', $enum), self::$parsedLineNumber + 1, $scalar, self::$parsedFilename);
652653
}
654+
if (!$useName) {
655+
return $enum::cases();
656+
}
657+
if ($useValue = str_ends_with($enumName, '->value')) {
658+
$enumName = substr($enumName, 0, -7);
659+
}
653660

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);
661+
if (!\defined($enumName)) {
662+
throw new ParseException(sprintf('The string "%s" is not the name of a valid enum.', $enumName), self::$parsedLineNumber + 1, $scalar, self::$parsedFilename);
658663
}
664+
665+
$value = \constant($enumName);
666+
659667
if (!$useValue) {
660668
return $value;
661669
}
662670
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);
671+
throw new ParseException(sprintf('The enum "%s" defines no value next to its name.', $enumName), self::$parsedLineNumber + 1, $scalar, self::$parsedFilename);
664672
}
665673

666674
return $value->value;

src/Symfony/Component/Yaml/Tests/InlineTest.php

+16-3
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

@@ -715,6 +722,12 @@ public function testDumpUnitEnum()
715722
$this->assertSame("!php/const Symfony\Component\Yaml\Tests\Fixtures\FooUnitEnum::BAR", Inline::dump(FooUnitEnum::BAR));
716723
}
717724

725+
public function testParseUnitEnumCases()
726+
{
727+
$this->assertSame(FooUnitEnum::cases(), Inline::parse("!php/enum Symfony\Component\Yaml\Tests\Fixtures\FooUnitEnum", Yaml::PARSE_CONSTANT));
728+
$this->assertSame(FooUnitEnum::cases(), Inline::parse("!php/enum Symfony\Component\Yaml\Tests\Fixtures\FooUnitEnum::cases()", Yaml::PARSE_CONSTANT));
729+
}
730+
718731
public function testParseUnitEnum()
719732
{
720733
$this->assertSame(FooUnitEnum::BAR, Inline::parse("!php/enum Symfony\Component\Yaml\Tests\Fixtures\FooUnitEnum::BAR", Yaml::PARSE_CONSTANT));

0 commit comments

Comments
 (0)