Skip to content

[ExpressionLanguage] Fix redirects and revert merging reference docs with the main guide #17726

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
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
3 changes: 2 additions & 1 deletion _build/redirection_map
Original file line number Diff line number Diff line change
Expand Up @@ -538,5 +538,6 @@
/components/security /security
/components/var_dumper/advanced /components/var_dumper#advanced-usage
/components/yaml/yaml_format /components/yaml#yaml-format
/components/expression_language/syntax /components/expression_language#expression-language-syntax
/components/expression_language/ast /components/expression_language#expression-language-ast
/components/expression_language/caching /components/expression_language#expression-language-caching
/components/expression_language/extending /components/expression_language#expression-language-extending
314 changes: 6 additions & 308 deletions components/expression_language.rst
Original file line number Diff line number Diff line change
Expand Up @@ -73,317 +73,11 @@ The main class of the component is

var_dump($expressionLanguage->compile('1 + 2')); // displays (1 + 2)

.. _expression-language-syntax:

.. index::
single: Syntax; ExpressionLanguage

Expression Syntax
-----------------

The ExpressionLanguage component uses a specific syntax which is based on the
expression syntax of Twig. In this document, you can find all supported
syntaxes.

Supported Literals
~~~~~~~~~~~~~~~~~~

The component supports:

* **strings** - single and double quotes (e.g. ``'hello'``)
* **numbers** - e.g. ``103``
* **arrays** - using JSON-like notation (e.g. ``[1, 2]``)
* **hashes** - using JSON-like notation (e.g. ``{ foo: 'bar' }``)
* **booleans** - ``true`` and ``false``
* **null** - ``null``
* **exponential** - also known as scientific (e.g. ``1.99E+3`` or ``1e-2``)

.. caution::

A backslash (``\``) must be escaped by 4 backslashes (``\\\\``) in a string
and 8 backslashes (``\\\\\\\\``) in a regex::

echo $expressionLanguage->evaluate('"\\\\"'); // prints \
$expressionLanguage->evaluate('"a\\\\b" matches "/^a\\\\\\\\b$/"'); // returns true

Control characters (e.g. ``\n``) in expressions are replaced with
whitespace. To avoid this, escape the sequence with a single backslash
(e.g. ``\\n``).

.. _component-expression-objects:

Working with Objects
~~~~~~~~~~~~~~~~~~~~

When passing objects into an expression, you can use different syntaxes to
access properties and call methods on the object.

Accessing Public Properties
...........................

Public properties on objects can be accessed by using the ``.`` syntax, similar
to JavaScript::

class Apple
{
public $variety;
}

$apple = new Apple();
$apple->variety = 'Honeycrisp';

var_dump($expressionLanguage->evaluate(
'fruit.variety',
[
'fruit' => $apple,
]
));

This will print out ``Honeycrisp``.

Calling Methods
...............

The ``.`` syntax can also be used to call methods on an object, similar to
JavaScript::

class Robot
{
public function sayHi($times)
{
$greetings = [];
for ($i = 0; $i < $times; $i++) {
$greetings[] = 'Hi';
}

return implode(' ', $greetings).'!';
}
}

$robot = new Robot();

var_dump($expressionLanguage->evaluate(
'robot.sayHi(3)',
[
'robot' => $robot,
]
));

This will print out ``Hi Hi Hi!``.

.. _component-expression-functions:

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::

define('DB_USER', 'root');

var_dump($expressionLanguage->evaluate(
'constant("DB_USER")'
));

This will print out ``root``.

.. tip::

To read how to register your own functions to use in an expression, see
":ref:`expression-language-extending`".

.. _component-expression-arrays:

Working with Arrays
~~~~~~~~~~~~~~~~~~~

If you pass an array into an expression, use the ``[]`` syntax to access
array keys, similar to JavaScript::

$data = ['life' => 10, 'universe' => 10, 'everything' => 22];

var_dump($expressionLanguage->evaluate(
'data["life"] + data["universe"] + data["everything"]',
[
'data' => $data,
]
));

This will print out ``42``.

Supported Operators
~~~~~~~~~~~~~~~~~~~

The component comes with a lot of operators:

Arithmetic Operators
....................

* ``+`` (addition)
* ``-`` (subtraction)
* ``*`` (multiplication)
* ``/`` (division)
* ``%`` (modulus)
* ``**`` (pow)

For example::

var_dump($expressionLanguage->evaluate(
'life + universe + everything',
[
'life' => 10,
'universe' => 10,
'everything' => 22,
]
));

This will print out ``42``.

Bitwise Operators
.................

* ``&`` (and)
* ``|`` (or)
* ``^`` (xor)

Comparison Operators
....................

* ``==`` (equal)
* ``===`` (identical)
* ``!=`` (not equal)
* ``!==`` (not identical)
* ``<`` (less than)
* ``>`` (greater than)
* ``<=`` (less than or equal to)
* ``>=`` (greater than or equal to)
* ``matches`` (regex match)

.. tip::

To test if a string does *not* match a regex, use the logical ``not``
operator in combination with the ``matches`` operator::

$expressionLanguage->evaluate('not ("foo" matches "/bar/")'); // returns true

You must use parentheses because the unary operator ``not`` has precedence
over the binary operator ``matches``.

Examples::

$ret1 = $expressionLanguage->evaluate(
'life == everything',
[
'life' => 10,
'everything' => 22,
]
);

$ret2 = $expressionLanguage->evaluate(
'life > everything',
[
'life' => 10,
'everything' => 22,
]
);

Both variables would be set to ``false``.

Logical Operators
.................

* ``not`` or ``!``
* ``and`` or ``&&``
* ``or`` or ``||``

For example::

$ret = $expressionLanguage->evaluate(
'life < universe or life < everything',
[
'life' => 10,
'universe' => 10,
'everything' => 22,
]
);

This ``$ret`` variable will be set to ``true``.

String Operators
................

* ``~`` (concatenation)

For example::

var_dump($expressionLanguage->evaluate(
'firstName~" "~lastName',
[
'firstName' => 'Arthur',
'lastName' => 'Dent',
]
));

This would print out ``Arthur Dent``.

Array Operators
...............

* ``in`` (contain)
* ``not in`` (does not contain)

For example::

class User
{
public $group;
}

$user = new User();
$user->group = 'human_resources';

$inGroup = $expressionLanguage->evaluate(
'user.group in ["human_resources", "marketing"]',
[
'user' => $user,
]
);

The ``$inGroup`` would evaluate to ``true``.

Numeric Operators
.................

* ``..`` (range)

For example::

class User
{
public $age;
}

$user = new User();
$user->age = 34;

$expressionLanguage->evaluate(
'user.age in 18..45',
[
'user' => $user,
]
);

This will evaluate to ``true``, because ``user.age`` is in the range from
``18`` to ``45``.

Ternary Operators
.................

* ``foo ? 'yes' : 'no'``
* ``foo ?: 'no'`` (equal to ``foo ? foo : 'no'``)
* ``foo ? 'yes'`` (equal to ``foo ? 'yes' : ''``)
See :doc:`/components/expression_language/syntax` to learn the syntax of the
ExpressionLanguage component

Passing in Variables
--------------------
Expand Down Expand Up @@ -428,6 +122,8 @@ expressions (e.g. the request, the current user, etc.):
.. index::
single: Caching; ExpressionLanguage

.. _expression-language-caching:

Caching
-------

Expand Down Expand Up @@ -498,6 +194,8 @@ Both ``evaluate()`` and ``compile()`` can handle ``ParsedExpression`` and
single: AST; ExpressionLanguage
single: AST; Abstract Syntax Tree

.. _expression-language-ast:

AST Dumping and Editing
-----------------------

Expand Down
Loading