Skip to content

Commit b4735b9

Browse files
[ExpressionLanguage] Add flags support in parse() and lint()
1 parent 78f039d commit b4735b9

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

components/expression_language.rst

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,50 @@ can chain multiple coalescing operators.
9292
* ``foo[3] ?? 'no'``
9393
* ``foo.baz ?? foo['baz'] ?? 'no'``
9494

95+
Parsing and Linting Expressions
96+
...............................
97+
98+
The ExpressionLanguage component provides a way to parse and lint expressions.
99+
The :method:`Symfony\\Component\\ExpressionLanguage\\ExpressionLanguage::parse`
100+
method returns a :class:`Symfony\\Component\\ExpressionLanguage\\ParsedExpression`
101+
instance that can be used to inspect and manipulate the expression. The
102+
:method:`Symfony\\Component\\ExpressionLanguage\\ExpressionLanguage::lint`, on the
103+
other hand, returns a boolean indicating if the expression is valid or not::
104+
105+
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
106+
107+
$expressionLanguage = new ExpressionLanguage();
108+
109+
var_dump($expressionLanguage->parse('1 + 2'));
110+
// displays the AST nodes of the expression which can be
111+
// inspected and manipulated
112+
113+
var_dump($expressionLanguage->lint('1 + 2')); // displays true
114+
115+
The call to these methods can be configured through flags. The available flags
116+
are available in the :class:`Symfony\\Component\\ExpressionLanguage\\Parser` class
117+
and are the following:
118+
119+
* ``IGNORE_UNKNOWN_VARIABLES``: don't throw an exception if a variable is not
120+
defined in the expression;
121+
* ``IGNORE_UNKNOWN_FUNCTIONS``: don't throw an exception if a function is not
122+
defined in the expression.
123+
124+
This is how you can use these flags::
125+
126+
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
127+
use Symfony\Component\ExpressionLanguage\Parser;
128+
129+
$expressionLanguage = new ExpressionLanguage();
130+
131+
// this return true because the unknown variables and functions are ignored
132+
var_dump($expressionLanguage->lint('unknown_var + unknown_function()', Parser::IGNORE_UNKNOWN_VARIABLES | Parser::IGNORE_UNKNOWN_FUNCTIONS));
133+
134+
.. versionadded:: 7.1
135+
136+
The support for flags in the ``parse()`` and ``lint()`` methods
137+
was introduced in Symfony 7.1.
138+
95139
Passing in Variables
96140
--------------------
97141

0 commit comments

Comments
 (0)