Skip to content

Commit e096ef4

Browse files
alexandre-dauboisnicolas-grekas
authored andcommitted
[DependencyInjection] Add support of PHP enumerations
1 parent eee9793 commit e096ef4

File tree

3 files changed

+19
-0
lines changed

3 files changed

+19
-0
lines changed

Inline.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,8 @@ public static function dump($value, int $flags = 0): string
127127
return self::dumpNull($flags);
128128
case $value instanceof \DateTimeInterface:
129129
return $value->format('c');
130+
case $value instanceof \UnitEnum:
131+
return sprintf('!php/const %s::%s', \get_class($value), $value->name);
130132
case \is_object($value):
131133
if ($value instanceof TaggedValue) {
132134
return '!'.$value->getTag().' '.self::dump($value->getValue(), $flags);

Tests/Fixtures/FooUnitEnum.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace Symfony\Component\Yaml\Tests\Fixtures;
4+
5+
enum FooUnitEnum
6+
{
7+
case BAR;
8+
}

Tests/InlineTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Symfony\Component\Yaml\Exception\ParseException;
1616
use Symfony\Component\Yaml\Inline;
1717
use Symfony\Component\Yaml\Tag\TaggedValue;
18+
use Symfony\Component\Yaml\Tests\Fixtures\FooUnitEnum;
1819
use Symfony\Component\Yaml\Yaml;
1920

2021
class InlineTest extends TestCase
@@ -577,6 +578,14 @@ public function testDumpDateTime($dateTime, $expected)
577578
$this->assertSame($expected, Inline::dump($dateTime));
578579
}
579580

581+
/**
582+
* @requires PHP 8.1
583+
*/
584+
public function testDumpUnitEnum()
585+
{
586+
$this->assertSame("!php/const Symfony\Component\Yaml\Tests\Fixtures\FooUnitEnum::BAR", Inline::dump(FooUnitEnum::BAR));
587+
}
588+
580589
public function getDateTimeDumpTests()
581590
{
582591
$tests = [];

0 commit comments

Comments
 (0)