Skip to content

Commit

Permalink
feature #17632 [ExpressionLanguage] Add enum expression function (a…
Browse files Browse the repository at this point in the history
…lexandre-daubois)

This PR was merged into the 6.3 branch.

Discussion
----------

[ExpressionLanguage] Add `enum` expression function

Related to symfony/symfony#48669.

:information_source: I added a namespace to the enum in the example to kind of remind that triple backslashes must be used in the expression to use the namespaced enum.

Commits
-------

42dc551 [ExpressionLanguage] Add `enum` expression function
  • Loading branch information
OskarStark committed Jan 10, 2023
2 parents bba5281 + 42dc551 commit 3f9c576
Showing 1 changed file with 47 additions and 3 deletions.
50 changes: 47 additions & 3 deletions components/expression_language/syntax.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand All @@ -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
Expand Down

0 comments on commit 3f9c576

Please sign in to comment.