Skip to content

Commit 8f1976e

Browse files
[ExpressionLanguage] Add enum expression function
1 parent 7a79bc7 commit 8f1976e

File tree

1 file changed

+40
-3
lines changed

1 file changed

+40
-3
lines changed

components/expression_language/syntax.rst

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,18 +127,55 @@ Working with Functions
127127
----------------------
128128

129129
You can also use registered functions in the expression by using the same
130-
syntax as PHP and JavaScript. The ExpressionLanguage component comes with one
131-
function by default: ``constant()``, which will return the value of the PHP
132-
constant::
130+
syntax as PHP and JavaScript. The ExpressionLanguage component comes with the
131+
following functions by default:
132+
133+
* ``constant()``
134+
* ``enum()``
135+
136+
Constant function
137+
~~~~~~~~~~~~~~~~~
138+
139+
This function will return the value of a PHP constant::
133140

134141
define('DB_USER', 'root');
135142

136143
var_dump($expressionLanguage->evaluate(
137144
'constant("DB_USER")'
138145
));
139146

147+
// This also works with class constants
148+
class Foo
149+
{
150+
public const DB_USER = 'root';
151+
}
152+
153+
var_dump($expressionLanguage->evaluate(
154+
'constant("App\\\SomeNamespace\\\Foo::DB_USER")'
155+
));
156+
140157
This will print out ``root``.
141158

159+
Enum function
160+
~~~~~~~~~~~~~
161+
162+
This function will return the case of an enumeration::
163+
164+
enum Foo
165+
{
166+
case Bar;
167+
}
168+
169+
var_dump(App\Enum\Foo::Bar === $expressionLanguage->evaluate(
170+
'enum("App\\\Enum\\\Foo::Bar")'
171+
));
172+
173+
This will print out ``true``.
174+
175+
.. versionadded:: 6.3
176+
177+
The ``enum()`` function was introduced in Symfony 6.3.
178+
142179
.. tip::
143180

144181
To read how to register your own functions to use in an expression, see

0 commit comments

Comments
 (0)