Skip to content

[Yaml] Allow to get all the enum cases #19527

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 7, 2024
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
20 changes: 20 additions & 0 deletions components/yaml.rst
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,26 @@ and the special ``!php/enum`` syntax to parse them as proper PHP enums::
// the value of the 'foo' key is a string because it missed the `!php/enum` syntax
// $parameters = ['foo' => 'FooEnum::Foo', 'bar' => 'foo'];

You can also use ``!php/enum`` to get all the enumeration cases by only
giving the enumeration FQCN::

enum FooEnum: string
{
case Foo = 'foo';
case Bar = 'bar';
}

// ...

$yaml = '{ bar: !php/enum FooEnum }';
$parameters = Yaml::parse($yaml, Yaml::PARSE_CONSTANT);
// $parameters = ['bar' => ['foo', 'bar']];

.. versionadded:: 7.1

The support for using the enum FQCN without specifying a case
was introduced in Symfony 7.1.

Parsing and Dumping of Binary Data
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Expand Down
13 changes: 13 additions & 0 deletions reference/formats/yaml.rst
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,19 @@ official YAML specification but are useful in Symfony applications:
# ... or you can also use "->value" to directly use the value of a BackedEnum case
operator_type: !php/enum App\Operator\Enum\Type::Or->value

This tag allows to omit the enum case and only provide the enum FQCN
to return an array of all available enum cases:

.. code-block:: yaml

data:
operator_types: !php/enum App\Operator\Enum\Type

.. versionadded:: 7.1

The support for using the enum FQCN without specifying a case
was introduced in Symfony 7.1.

Unsupported YAML Features
~~~~~~~~~~~~~~~~~~~~~~~~~

Expand Down