Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commita1ab166

Browse files
committed
[Yaml] Allow Yaml component to get all the enum cases
1 parentfafbbfa commita1ab166

File tree

3 files changed

+39
-13
lines changed

3 files changed

+39
-13
lines changed

‎src/Symfony/Component/Yaml/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+
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‎

Lines changed: 18 additions & 10 deletions
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
thrownewParseException(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 (!$valueinstanceof \UnitEnum) {
657-
thrownewParseException(sprintf('The string "%s" is not the name of a valid enum.',$enum),self::$parsedLineNumber +1,$scalar,self::$parsedFilename);
661+
if (!\defined($enumName)) {
662+
thrownewParseException(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 (!$valueinstanceof \BackedEnum) {
663-
thrownewParseException(sprintf('The enum "%s" defines no value next to its name.',$enum),self::$parsedLineNumber +1,$scalar,self::$parsedFilename);
671+
thrownewParseException(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‎

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,14 +76,21 @@ public function testParsePhpConstantThrowsExceptionWhenUndefined()
7676
publicfunctiontestParsePhpEnumThrowsExceptionWhenUndefined()
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+
publicfunctiontestParsePhpEnumThrowsExceptionWhenNameUndefined()
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
publicfunctiontestParsePhpEnumThrowsExceptionWhenNotAnEnum()
8491
{
8592
$this->expectException(ParseException::class);
86-
$this->expectExceptionMessage('Thestring "PHP_INT_MAX" is notthe name of a valid enum');
93+
$this->expectExceptionMessage('Theenum "PHP_INT_MAX" is notdefined');
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+
publicfunctiontestParseUnitEnumCases()
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
publicfunctiontestParseUnitEnum()
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)

[8]ページ先頭

©2009-2025 Movatter.jp