From 42dc55113d2f22d0b349039c59d49e9f8ce8a54d Mon Sep 17 00:00:00 2001 From: Alexandre Daubois Date: Fri, 30 Dec 2022 08:55:05 +0100 Subject: [PATCH] [ExpressionLanguage] Add `enum` expression function --- components/expression_language/syntax.rst | 50 +++++++++++++++++++++-- 1 file changed, 47 insertions(+), 3 deletions(-) diff --git a/components/expression_language/syntax.rst b/components/expression_language/syntax.rst index de0fddf80bf..ae91619b43b 100644 --- a/components/expression_language/syntax.rst +++ b/components/expression_language/syntax.rst @@ -127,9 +127,16 @@ Working with Functions ---------------------- You can also use registered functions in the expression by using the same -syntax as PHP and JavaScript. The ExpressionLanguage component comes with one -function by default: ``constant()``, which will return the value of the PHP -constant:: +syntax as PHP and JavaScript. The ExpressionLanguage component comes with the +following functions by default: + +* ``constant()`` +* ``enum()`` + +``constant()`` function +~~~~~~~~~~~~~~~~~~~~~~~ + +This function will return the value of a PHP constant:: define('DB_USER', 'root'); @@ -139,6 +146,43 @@ constant:: This will print out ``root``. +This also works with class constants:: + + namespace App\SomeNamespace; + + class Foo + { + public const API_ENDPOINT = '/api'; + } + + var_dump($expressionLanguage->evaluate( + 'constant("App\\\SomeNamespace\\\Foo::API_ENDPOINT")' + )); + +This will print out ``/api``. + +``enum()`` function +~~~~~~~~~~~~~~~~~~~ + +This function will return the case of an enumeration:: + + namespace App\SomeNamespace; + + enum Foo + { + case Bar; + } + + var_dump(App\Enum\Foo::Bar === $expressionLanguage->evaluate( + 'enum("App\\\SomeNamespace\\\Foo::Bar")' + )); + +This will print out ``true``. + +.. versionadded:: 6.3 + + The ``enum()`` function was introduced in Symfony 6.3. + .. tip:: To read how to register your own functions to use in an expression, see