@@ -9,24 +9,102 @@ expression syntax of Twig. In this document, you can find all supported
9
9
syntaxes.
10
10
11
11
Supported Literals
12
- ~~~~~~~~~~~~~~~~~~
12
+ ------------------
13
13
14
14
The component supports:
15
15
16
16
* **strings ** - single and double quotes (e.g. ``'hello' ``)
17
17
* **numbers ** - e.g. ``103 ``
18
- * **arrays ** - using twig notation (e.g. ``[1, 2] ``)
19
- * **hashes ** - using twig notation (e.g. ``{ foo: 'bar' } ``)
18
+ * **arrays ** - using JSON-like notation (e.g. ``[1, 2] ``)
19
+ * **hashes ** - using JSON-like notation (e.g. ``{ foo: 'bar' } ``)
20
20
* **booleans ** - ``true `` and ``false ``
21
21
* **null ** - ``null ``
22
22
23
+ .. _component-expression-objects :
24
+
25
+ Working with Objects
26
+ --------------------
27
+
28
+ When passing objects into an expression, you can use different syntaxes to
29
+ access properties and call methods on the object.
30
+
31
+ Accessing Public Methods
32
+ ~~~~~~~~~~~~~~~~~~~~~~~~
33
+
34
+ Public properties on objects can be accessed by using the ``. `` syntax, similar
35
+ to JavaScript::
36
+
37
+ class Apple
38
+ {
39
+ public $variety;
40
+ }
41
+
42
+ $apple = new Apple();
43
+ $apple->variety = 'Honeycrisp';
44
+
45
+ echo $language->evaluate(
46
+ 'fruit.variety',
47
+ array(
48
+ 'fruit' => $apple,
49
+ )
50
+ );
51
+
52
+ Calling Methods
53
+ ~~~~~~~~~~~~~~~
54
+
55
+ The ``. `` syntax can also be used to call methods on an object, similar to
56
+ JavaScript::
57
+
58
+ class Robot
59
+ {
60
+ public function sayHi($times)
61
+ {
62
+ $greetings = array();
63
+ for ($i = 0; $i < $times; $i++) {
64
+ $greetings[] = 'Hi';
65
+ }
66
+
67
+ return implode(' ', $greetings).'!';
68
+ }
69
+ }
70
+
71
+ $robot = new Robot();
72
+
73
+ echo $language->evaluate(
74
+ 'robot.sayHi(3)',
75
+ array(
76
+ 'robot' => $robot,
77
+ )
78
+ );
79
+
80
+ This will print "Hi Hi Hi!";
81
+
82
+ .. _component-expression-arrays :
83
+
84
+ Working with Arrays
85
+ -------------------
86
+
87
+ If you pass an array into an expression, use the ``[] `` syntax to access
88
+ array keys, similar to JavaScript::
89
+
90
+ $data = array('life' => 10, 'universe' => 10, 'everything' => 22);
91
+
92
+ echo $language->evaluate(
93
+ 'data["life"] + data["universe"] + data["everything"]',
94
+ array(
95
+ 'data' => $data,
96
+ )
97
+ );
98
+
99
+ This will print ``42 ``.
100
+
23
101
Supported Operators
24
- ~~~~~~~~~~~~~~~~~~~
102
+ -------------------
25
103
26
104
The component comes with a lot of operators:
27
105
28
106
Arithmetic Operators
29
- ....................
107
+ ~~~~~~~~~~~~~~~~~~~~
30
108
31
109
* ``+ `` (addition)
32
110
* ``- `` (subtraction)
@@ -35,20 +113,33 @@ Arithmetic Operators
35
113
* ``% `` (modulus)
36
114
* ``** `` (pow)
37
115
116
+ For example::
117
+
118
+ echo $language->evaluate(
119
+ 'life + universe + everything',
120
+ array(
121
+ 'life' => 10,
122
+ 'universe' => 10,
123
+ 'everything' => 22,
124
+ )
125
+ );
126
+
127
+ This will print out ``42 ``.
128
+
38
129
Assignment Operators
39
- ....................
130
+ ~~~~~~~~~~~~~~~~~~~~
40
131
41
132
* ``= ``
42
133
43
134
Bitwise Operators
44
- .................
135
+ ~~~~~~~~~~~~~~~~~
45
136
46
137
* ``& `` (and)
47
138
* ``| `` (or)
48
139
* ``^ `` (xor)
49
140
50
141
Comparison Operators
51
- ....................
142
+ ~~~~~~~~~~~~~~~~~~~~
52
143
53
144
* ``== `` (equal)
54
145
* ``=== `` (identical)
@@ -67,31 +158,95 @@ Comparison Operators
67
158
68
159
$language->evaluate('not "foo" matches "/bar/"'); // returns true
69
160
161
+ Examples::
162
+
163
+ $ret1 = $language->evaluate(
164
+ 'life == everything',
165
+ array(
166
+ 'life' => 10,
167
+ 'universe' => 10,
168
+ 'everything' => 22,
169
+ )
170
+ );
171
+
172
+ $ret2 = $language->evaluate(
173
+ 'life > everything',
174
+ array(
175
+ 'life' => 10,
176
+ 'universe' => 10,
177
+ 'everything' => 22,
178
+ )
179
+ );
180
+
181
+ These would both return ``false ``.
182
+
70
183
Logical Operators
71
- .................
184
+ ~~~~~~~~~~~~~~~~~
72
185
73
186
* ``not `` or ``! ``
74
187
* ``and `` or ``&& ``
75
188
* ``or `` or ``|| ``
76
189
190
+ For example::
191
+
192
+ $ret = $language->evaluate(
193
+ 'life < universe or life < everything',
194
+ array(
195
+ 'life' => 10,
196
+ 'universe' => 10,
197
+ 'everything' => 22,
198
+ )
199
+ );
200
+
201
+ This would return ``true ``.
202
+
77
203
String Operators
78
- ................
204
+ ~~~~~~~~~~~~~~~~
79
205
80
206
* ``~ `` (concatenation)
81
207
208
+ For example::
209
+
210
+ $ret4 = $language->evaluate(
211
+ 'firstName~" "~lastName',
212
+ array(
213
+ 'firstName' => 'Arthur',
214
+ 'lastName' => 'Dent',
215
+ )
216
+ );
217
+
218
+ This would print out ``Arthur Dent ``.
219
+
82
220
Array Operators
83
- ...............
221
+ ~~~~~~~~~~~~~~~
84
222
85
223
* ``in `` (contain)
86
224
* ``not in `` (does not contain)
87
225
226
+ For example::
227
+
228
+ class User
229
+ {
230
+ public $group;
231
+ }
232
+
233
+ $user = new User();
234
+ $user->group = 'human_resources';
235
+
236
+ $ret5 = $language->evaluate(
237
+ 'user.group in ["human_resources", "marketing"]',
238
+ array(
239
+ 'user' => $user
240
+ )
241
+ );
242
+
88
243
Numeric Operators
89
- .................
244
+ ~~~~~~~~~~~~~~~~~
90
245
91
246
* ``.. `` (range)
92
247
93
248
Ternary Operators
94
- .................
249
+ ~~~~~~~~~~~~~~~~~
95
250
96
251
* ``foo ? 'yes' : 'no' ``
97
252
* ``foo ?: 'no' `` (equal to ``foo ? foo : 'no' ``)
0 commit comments