Skip to content

Commit a3eda50

Browse files
[Yaml] Add support for !php/enum *->value syntax
1 parent 4f43ee8 commit a3eda50

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

components/yaml.rst

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,30 @@ syntax to parse them as proper PHP constants::
338338
$parameters = Yaml::parse($yaml, Yaml::PARSE_CONSTANT);
339339
// $parameters = ['foo' => 'PHP_INT_SIZE', 'bar' => 8];
340340

341+
Parsing Unit and Backed Enumerations
342+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
343+
344+
Unit and backed enumerations can be parsed by the YAML parser thanks to the
345+
special ``!php/enum`` syntax and the ``PARSE_CONSTANT`` flag. Depending if
346+
you need the concrete enumeration case or the actual value of the case when
347+
dealing with backed enumerations, both syntax are available::
348+
349+
enum FooEnum: string
350+
{
351+
case Foo = 'foo';
352+
case Bar = 'bar';
353+
}
354+
355+
// ...
356+
357+
$yaml = '{ foo: FooEnum::Foo, bar: !php/enum FooEnum::Foo }';
358+
$parameters = Yaml::parse($yaml, Yaml::PARSE_CONSTANT);
359+
// $parameters = ['foo' => 'FooEnum::Foo', 'bar' => FooEnum::Foo];
360+
361+
$yaml = '{ foo: FooEnum::Foo, bar: !php/enum FooEnum::Foo->value }';
362+
$parameters = Yaml::parse($yaml, Yaml::PARSE_CONSTANT);
363+
// $parameters = ['foo' => 'FooEnum::Foo', 'bar' => 'foo'];
364+
341365
Parsing and Dumping of Binary Data
342366
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
343367

0 commit comments

Comments
 (0)