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

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/Symfony/Component/Yaml/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

7.1
---

* Add support for getting all the enum cases with `!php/enum Foo`

7.0
---

Expand Down
27 changes: 17 additions & 10 deletions src/Symfony/Component/Yaml/Inline.php
Original file line number Diff line number Diff line change
Expand Up @@ -643,24 +643,31 @@ private static function evaluateScalar(string $scalar, int $flags, array &$refer
}

$i = 0;
$enum = self::parseScalar(substr($scalar, 10), 0, null, $i, false);
if ($useValue = str_ends_with($enum, '->value')) {
$enum = substr($enum, 0, -7);
}
if (!\defined($enum)) {
$enumName = self::parseScalar(substr($scalar, 10), 0, null, $i, false);
$useName = str_contains($enumName, '::');
$enum = $useName ? strstr($enumName, '::', true) : $enumName;

if (!enum_exists($enum)) {
throw new ParseException(sprintf('The enum "%s" is not defined.', $enum), self::$parsedLineNumber + 1, $scalar, self::$parsedFilename);
}
if (!$useName) {
return $enum::cases();
}
if ($useValue = str_ends_with($enumName, '->value')) {
$enumName = substr($enumName, 0, -7);
}

$value = \constant($enum);

if (!$value instanceof \UnitEnum) {
throw new ParseException(sprintf('The string "%s" is not the name of a valid enum.', $enum), self::$parsedLineNumber + 1, $scalar, self::$parsedFilename);
if (!\defined($enumName)) {
throw new ParseException(sprintf('The string "%s" is not the name of a valid enum.', $enumName), self::$parsedLineNumber + 1, $scalar, self::$parsedFilename);
}

$value = \constant($enumName);

if (!$useValue) {
return $value;
}
if (!$value instanceof \BackedEnum) {
throw new ParseException(sprintf('The enum "%s" defines no value next to its name.', $enum), self::$parsedLineNumber + 1, $scalar, self::$parsedFilename);
throw new ParseException(sprintf('The enum "%s" defines no value next to its name.', $enumName), self::$parsedLineNumber + 1, $scalar, self::$parsedFilename);
}

return $value->value;
Expand Down
18 changes: 15 additions & 3 deletions src/Symfony/Component/Yaml/Tests/InlineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,21 @@ public function testParsePhpConstantThrowsExceptionWhenUndefined()
public function testParsePhpEnumThrowsExceptionWhenUndefined()
{
$this->expectException(ParseException::class);
$this->expectExceptionMessage('The enum "SomeEnum::Foo" is not defined');
Inline::parse('!php/enum SomeEnum::Foo', Yaml::PARSE_CONSTANT);
$this->expectExceptionMessage('The enum "SomeEnum" is not defined');
Inline::parse('!php/enum SomeEnum', Yaml::PARSE_CONSTANT);
}

public function testParsePhpEnumThrowsExceptionWhenNameUndefined()
{
$this->expectException(ParseException::class);
$this->expectExceptionMessage('The string "Symfony\Component\Yaml\Tests\Fixtures\FooUnitEnum::Foo" is not the name of a valid enum');
Inline::parse('!php/enum Symfony\Component\Yaml\Tests\Fixtures\FooUnitEnum::Foo', Yaml::PARSE_CONSTANT);
}

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

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

public function testParseUnitEnumCases()
{
$this->assertSame(FooUnitEnum::cases(), Inline::parse("!php/enum Symfony\Component\Yaml\Tests\Fixtures\FooUnitEnum", Yaml::PARSE_CONSTANT));
}

public function testParseUnitEnum()
{
$this->assertSame(FooUnitEnum::BAR, Inline::parse("!php/enum Symfony\Component\Yaml\Tests\Fixtures\FooUnitEnum::BAR", Yaml::PARSE_CONSTANT));
Expand Down