Skip to content

Commit 8f8b9d2

Browse files
committed
feature #19527 [Yaml] Allow to get all the enum cases (alexandre-daubois)
This PR was merged into the 7.1 branch. Discussion ---------- [Yaml] Allow to get all the enum cases Fix #19491 Commits ------- 5d2b2a6 [Yaml] Allow to get all the enum cases
2 parents bc313e8 + 5d2b2a6 commit 8f8b9d2

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

components/yaml.rst

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,26 @@ and the special ``!php/enum`` syntax to parse them as proper PHP enums::
355355
// the value of the 'foo' key is a string because it missed the `!php/enum` syntax
356356
// $parameters = ['foo' => 'FooEnum::Foo', 'bar' => 'foo'];
357357

358+
You can also use ``!php/enum`` to get all the enumeration cases by only
359+
giving the enumeration FQCN::
360+
361+
enum FooEnum: string
362+
{
363+
case Foo = 'foo';
364+
case Bar = 'bar';
365+
}
366+
367+
// ...
368+
369+
$yaml = '{ bar: !php/enum FooEnum }';
370+
$parameters = Yaml::parse($yaml, Yaml::PARSE_CONSTANT);
371+
// $parameters = ['bar' => ['foo', 'bar']];
372+
373+
.. versionadded:: 7.1
374+
375+
The support for using the enum FQCN without specifying a case
376+
was introduced in Symfony 7.1.
377+
358378
Parsing and Dumping of Binary Data
359379
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
360380

reference/formats/yaml.rst

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,19 @@ official YAML specification but are useful in Symfony applications:
346346
# ... or you can also use "->value" to directly use the value of a BackedEnum case
347347
operator_type: !php/enum App\Operator\Enum\Type::Or->value
348348
349+
This tag allows to omit the enum case and only provide the enum FQCN
350+
to return an array of all available enum cases:
351+
352+
.. code-block:: yaml
353+
354+
data:
355+
operator_types: !php/enum App\Operator\Enum\Type
356+
357+
.. versionadded:: 7.1
358+
359+
The support for using the enum FQCN without specifying a case
360+
was introduced in Symfony 7.1.
361+
349362
Unsupported YAML Features
350363
~~~~~~~~~~~~~~~~~~~~~~~~~
351364

0 commit comments

Comments
 (0)