@@ -73,317 +73,11 @@ The main class of the component is
73
73
74
74
var_dump($expressionLanguage->compile('1 + 2')); // displays (1 + 2)
75
75
76
- .. _expression-language-syntax :
77
-
78
- .. index ::
79
- single: Syntax; ExpressionLanguage
80
-
81
76
Expression Syntax
82
77
-----------------
83
78
84
- The ExpressionLanguage component uses a specific syntax which is based on the
85
- expression syntax of Twig. In this document, you can find all supported
86
- syntaxes.
87
-
88
- Supported Literals
89
- ~~~~~~~~~~~~~~~~~~
90
-
91
- The component supports:
92
-
93
- * **strings ** - single and double quotes (e.g. ``'hello' ``)
94
- * **numbers ** - e.g. ``103 ``
95
- * **arrays ** - using JSON-like notation (e.g. ``[1, 2] ``)
96
- * **hashes ** - using JSON-like notation (e.g. ``{ foo: 'bar' } ``)
97
- * **booleans ** - ``true `` and ``false ``
98
- * **null ** - ``null ``
99
- * **exponential ** - also known as scientific (e.g. ``1.99E+3 `` or ``1e-2 ``)
100
-
101
- .. caution ::
102
-
103
- A backslash (``\ ``) must be escaped by 4 backslashes (``\\\\ ``) in a string
104
- and 8 backslashes (``\\\\\\\\ ``) in a regex::
105
-
106
- echo $expressionLanguage->evaluate('"\\\\"'); // prints \
107
- $expressionLanguage->evaluate('"a\\\\b" matches "/^a\\\\\\\\b$/"'); // returns true
108
-
109
- Control characters (e.g. ``\n ``) in expressions are replaced with
110
- whitespace. To avoid this, escape the sequence with a single backslash
111
- (e.g. ``\\n ``).
112
-
113
- .. _component-expression-objects :
114
-
115
- Working with Objects
116
- ~~~~~~~~~~~~~~~~~~~~
117
-
118
- When passing objects into an expression, you can use different syntaxes to
119
- access properties and call methods on the object.
120
-
121
- Accessing Public Properties
122
- ...........................
123
-
124
- Public properties on objects can be accessed by using the ``. `` syntax, similar
125
- to JavaScript::
126
-
127
- class Apple
128
- {
129
- public $variety;
130
- }
131
-
132
- $apple = new Apple();
133
- $apple->variety = 'Honeycrisp';
134
-
135
- var_dump($expressionLanguage->evaluate(
136
- 'fruit.variety',
137
- [
138
- 'fruit' => $apple,
139
- ]
140
- ));
141
-
142
- This will print out ``Honeycrisp ``.
143
-
144
- Calling Methods
145
- ...............
146
-
147
- The ``. `` syntax can also be used to call methods on an object, similar to
148
- JavaScript::
149
-
150
- class Robot
151
- {
152
- public function sayHi($times)
153
- {
154
- $greetings = [];
155
- for ($i = 0; $i < $times; $i++) {
156
- $greetings[] = 'Hi';
157
- }
158
-
159
- return implode(' ', $greetings).'!';
160
- }
161
- }
162
-
163
- $robot = new Robot();
164
-
165
- var_dump($expressionLanguage->evaluate(
166
- 'robot.sayHi(3)',
167
- [
168
- 'robot' => $robot,
169
- ]
170
- ));
171
-
172
- This will print out ``Hi Hi Hi! ``.
173
-
174
- .. _component-expression-functions :
175
-
176
- Working with Functions
177
- ~~~~~~~~~~~~~~~~~~~~~~
178
-
179
- You can also use registered functions in the expression by using the same
180
- syntax as PHP and JavaScript. The ExpressionLanguage component comes with one
181
- function by default: ``constant() ``, which will return the value of the PHP
182
- constant::
183
-
184
- define('DB_USER', 'root');
185
-
186
- var_dump($expressionLanguage->evaluate(
187
- 'constant("DB_USER")'
188
- ));
189
-
190
- This will print out ``root ``.
191
-
192
- .. tip ::
193
-
194
- To read how to register your own functions to use in an expression, see
195
- ":ref: `expression-language-extending `".
196
-
197
- .. _component-expression-arrays :
198
-
199
- Working with Arrays
200
- ~~~~~~~~~~~~~~~~~~~
201
-
202
- If you pass an array into an expression, use the ``[] `` syntax to access
203
- array keys, similar to JavaScript::
204
-
205
- $data = ['life' => 10, 'universe' => 10, 'everything' => 22];
206
-
207
- var_dump($expressionLanguage->evaluate(
208
- 'data["life"] + data["universe"] + data["everything"]',
209
- [
210
- 'data' => $data,
211
- ]
212
- ));
213
-
214
- This will print out ``42 ``.
215
-
216
- Supported Operators
217
- ~~~~~~~~~~~~~~~~~~~
218
-
219
- The component comes with a lot of operators:
220
-
221
- Arithmetic Operators
222
- ....................
223
-
224
- * ``+ `` (addition)
225
- * ``- `` (subtraction)
226
- * ``* `` (multiplication)
227
- * ``/ `` (division)
228
- * ``% `` (modulus)
229
- * ``** `` (pow)
230
-
231
- For example::
232
-
233
- var_dump($expressionLanguage->evaluate(
234
- 'life + universe + everything',
235
- [
236
- 'life' => 10,
237
- 'universe' => 10,
238
- 'everything' => 22,
239
- ]
240
- ));
241
-
242
- This will print out ``42 ``.
243
-
244
- Bitwise Operators
245
- .................
246
-
247
- * ``& `` (and)
248
- * ``| `` (or)
249
- * ``^ `` (xor)
250
-
251
- Comparison Operators
252
- ....................
253
-
254
- * ``== `` (equal)
255
- * ``=== `` (identical)
256
- * ``!= `` (not equal)
257
- * ``!== `` (not identical)
258
- * ``< `` (less than)
259
- * ``> `` (greater than)
260
- * ``<= `` (less than or equal to)
261
- * ``>= `` (greater than or equal to)
262
- * ``matches `` (regex match)
263
-
264
- .. tip ::
265
-
266
- To test if a string does *not * match a regex, use the logical ``not ``
267
- operator in combination with the ``matches `` operator::
268
-
269
- $expressionLanguage->evaluate('not ("foo" matches "/bar/")'); // returns true
270
-
271
- You must use parentheses because the unary operator ``not `` has precedence
272
- over the binary operator ``matches ``.
273
-
274
- Examples::
275
-
276
- $ret1 = $expressionLanguage->evaluate(
277
- 'life == everything',
278
- [
279
- 'life' => 10,
280
- 'everything' => 22,
281
- ]
282
- );
283
-
284
- $ret2 = $expressionLanguage->evaluate(
285
- 'life > everything',
286
- [
287
- 'life' => 10,
288
- 'everything' => 22,
289
- ]
290
- );
291
-
292
- Both variables would be set to ``false ``.
293
-
294
- Logical Operators
295
- .................
296
-
297
- * ``not `` or ``! ``
298
- * ``and `` or ``&& ``
299
- * ``or `` or ``|| ``
300
-
301
- For example::
302
-
303
- $ret = $expressionLanguage->evaluate(
304
- 'life < universe or life < everything',
305
- [
306
- 'life' => 10,
307
- 'universe' => 10,
308
- 'everything' => 22,
309
- ]
310
- );
311
-
312
- This ``$ret `` variable will be set to ``true ``.
313
-
314
- String Operators
315
- ................
316
-
317
- * ``~ `` (concatenation)
318
-
319
- For example::
320
-
321
- var_dump($expressionLanguage->evaluate(
322
- 'firstName~" "~lastName',
323
- [
324
- 'firstName' => 'Arthur',
325
- 'lastName' => 'Dent',
326
- ]
327
- ));
328
-
329
- This would print out ``Arthur Dent ``.
330
-
331
- Array Operators
332
- ...............
333
-
334
- * ``in `` (contain)
335
- * ``not in `` (does not contain)
336
-
337
- For example::
338
-
339
- class User
340
- {
341
- public $group;
342
- }
343
-
344
- $user = new User();
345
- $user->group = 'human_resources';
346
-
347
- $inGroup = $expressionLanguage->evaluate(
348
- 'user.group in ["human_resources", "marketing"]',
349
- [
350
- 'user' => $user,
351
- ]
352
- );
353
-
354
- The ``$inGroup `` would evaluate to ``true ``.
355
-
356
- Numeric Operators
357
- .................
358
-
359
- * ``.. `` (range)
360
-
361
- For example::
362
-
363
- class User
364
- {
365
- public $age;
366
- }
367
-
368
- $user = new User();
369
- $user->age = 34;
370
-
371
- $expressionLanguage->evaluate(
372
- 'user.age in 18..45',
373
- [
374
- 'user' => $user,
375
- ]
376
- );
377
-
378
- This will evaluate to ``true ``, because ``user.age `` is in the range from
379
- ``18 `` to ``45 ``.
380
-
381
- Ternary Operators
382
- .................
383
-
384
- * ``foo ? 'yes' : 'no' ``
385
- * ``foo ?: 'no' `` (equal to ``foo ? foo : 'no' ``)
386
- * ``foo ? 'yes' `` (equal to ``foo ? 'yes' : '' ``)
79
+ See :doc: `/components/expression_language/syntax ` to learn the syntax of the
80
+ ExpressionLanguage component
387
81
388
82
Passing in Variables
389
83
--------------------
@@ -428,6 +122,8 @@ expressions (e.g. the request, the current user, etc.):
428
122
.. index ::
429
123
single: Caching; ExpressionLanguage
430
124
125
+ .. _expression-language-caching :
126
+
431
127
Caching
432
128
-------
433
129
@@ -498,6 +194,8 @@ Both ``evaluate()`` and ``compile()`` can handle ``ParsedExpression`` and
498
194
single: AST; ExpressionLanguage
499
195
single: AST; Abstract Syntax Tree
500
196
197
+ .. _expression-language-ast :
198
+
501
199
AST Dumping and Editing
502
200
-----------------------
503
201
0 commit comments