Skip to content

[ExpressionLanguage] Add enum expression function #17632

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
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
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a namespace please

{
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