From 5d2b2a6fd738c48de9275535cf1522e972c5d5f4 Mon Sep 17 00:00:00 2001 From: Alexandre Daubois Date: Wed, 7 Feb 2024 13:32:43 +0100 Subject: [PATCH] [Yaml] Allow to get all the enum cases --- components/yaml.rst | 20 ++++++++++++++++++++ reference/formats/yaml.rst | 13 +++++++++++++ 2 files changed, 33 insertions(+) diff --git a/components/yaml.rst b/components/yaml.rst index 627fc4479e0..5f724e0572c 100644 --- a/components/yaml.rst +++ b/components/yaml.rst @@ -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 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/reference/formats/yaml.rst b/reference/formats/yaml.rst index 64adac599fb..6a61cafa74a 100644 --- a/reference/formats/yaml.rst +++ b/reference/formats/yaml.rst @@ -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 ~~~~~~~~~~~~~~~~~~~~~~~~~