-
Notifications
You must be signed in to change notification settings - Fork 23
/
LambdaExtension.php
300 lines (257 loc) · 10 KB
/
LambdaExtension.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
<?php
/**
* This file is part of TwigLambda
*
* (c) Damian Polac <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace DPolac\TwigLambda;
use DPolac\Dictionary;
class LambdaExtension extends \Twig_Extension
{
public function getOperators()
{
return [
[
'=>' => [
'precedence' => 0,
'class' => '\DPolac\TwigLambda\NodeExpression\SimpleLambda'
],
],
[
'=>' => [
'precedence' => 0,
'class' => '\DPolac\TwigLambda\NodeExpression\LambdaWithArguments',
'associativity' => \Twig_ExpressionParser::OPERATOR_LEFT
],
';' => [
'precedence' => 5,
'class' => '\DPolac\TwigLambda\NodeExpression\Arguments',
'associativity' => \Twig_ExpressionParser::OPERATOR_RIGHT
],
]
];
}
public function getFunctions()
{
return [
new \Twig_SimpleFunction('call', '\DPolac\TwigLambda\LambdaExtension::call'),
];
}
public function getTests()
{
return [
new \Twig_SimpleTest('every', '\DPolac\TwigLambda\LambdaExtension::every'),
new \Twig_SimpleTest('any', '\DPolac\TwigLambda\LambdaExtension::any'),
];
}
public function getFilters()
{
return [
new \Twig_SimpleFilter('map', '\DPolac\TwigLambda\LambdaExtension::map'),
new \Twig_SimpleFilter('select', '\DPolac\TwigLambda\LambdaExtension::map'),
new \Twig_SimpleFilter('filter', '\DPolac\TwigLambda\LambdaExtension::filter'),
new \Twig_SimpleFilter('where', '\DPolac\TwigLambda\LambdaExtension::filter'),
new \Twig_SimpleFilter('unique_by', '\DPolac\TwigLambda\LambdaExtension::uniqueBy'),
new \Twig_SimpleFilter('group_by', '\DPolac\TwigLambda\LambdaExtension::groupBy'),
new \Twig_SimpleFilter('sort_by', '\DPolac\TwigLambda\LambdaExtension::sortBy'),
new \Twig_SimpleFilter('count_by', '\DPolac\TwigLambda\LambdaExtension::countBy'),
];
}
public static function map($array, $callback)
{
if (!is_callable($callback)) {
throw new \Twig_Error_Runtime(sprintf(
'Second argument of "map" must be callable, but is "%s".', gettype($callback)));
}
if (is_array($array)) {
$array = array_map($callback, $array, array_keys($array));
} elseif ($array instanceof \Traversable) {
$result = new Dictionary();
foreach ($array as $i => $item) {
$result[$i] = $callback($item, $i);
}
$array = $result;
} else {
throw new \Twig_Error_Runtime(sprintf(
'First argument of "map" must be array or Traversable, but is "%s".', gettype($array)));
}
return $array;
}
public static function filter($array, $callback)
{
if (!is_callable($callback)) {
throw new \Twig_Error_Runtime(sprintf(
'Second argument of "filter" must be callable, but is "%s".', gettype($callback)));
}
if (is_array($array)) {
$array = array_filter($array, $callback, ARRAY_FILTER_USE_BOTH);
} elseif ($array instanceof \Traversable) {
$result = new Dictionary();
foreach ($array as $i => $item) {
if ($callback($item, $i)) {
$result[$i] = $item;
}
}
$array = $result;
} else {
throw new \Twig_Error_Runtime(sprintf(
'First argument of "filter" must be array or Traversable, but is "%s".', gettype($array)));
}
return $array;
}
public static function uniqueBy($array, $callback)
{
if (!is_array($array) && !($array instanceof \Traversable)) {
throw new \Twig_Error_Runtime(sprintf(
'First argument of "unique_by" must be array or Traversable, but is "%s".', gettype($array)));
}
if ('==' === $callback) {
$callback = function($item1, $item2) { return $item1 == $item2; };
} else if ('===' === $callback) {
$callback = function($item1, $item2) { return $item1 === $item2; };
} else if (!is_callable($callback)) {
throw new \Twig_Error_Runtime(sprintf(
'Second argument of "unique_by" must be callable, "==" or "===", but is "%s".', gettype($callback)));
}
if ($array instanceof \Traversable) {
if ($array instanceof \Iterator) {
// convert Iterator to IteratorAggregate for nested foreach
$array = Dictionary::fromArray($array);
}
$result = new Dictionary();
} else {
$result = [];
}
foreach ($array as $i => $item) {
foreach ($array as $j => $previous) {
if ($i === $j) {
// add to results if already checked every previous element
$result[$i] = $item;
} elseif (isset($result[$j]) && $callback($item, $previous, $i, $j)) {
// skip if is identical with value which is already in results array
continue 2;
}
}
}
return $result;
}
public static function groupBy($array, $callback)
{
if (!is_callable($callback)) {
throw new \Twig_Error_Runtime(sprintf(
'Second argument of "group_by" must be callable, but is "%s".', gettype($callback)));
}
if (!is_array($array) && !($array instanceof \Traversable)) {
throw new \Twig_Error_Runtime(sprintf(
'First argument of "group_by" must be array or Traversable, but is "%s".', gettype($array)));
}
$results = new Dictionary();
foreach ($array as $i => $item) {
$key = $callback($item, $i);
if (!isset($results[$key])) {
$results[$key] = [$i => $item];
} else {
$results[$key][$i] = $item;
}
}
return $results;
}
public static function sortBy($array, $callback, $direction = 'ASC')
{
if (!is_callable($callback)) {
throw new \Twig_Error_Runtime(sprintf(
'Second argument of "sort_by" must be callable, but is "%s".', gettype($callback)));
}
if (!is_array($array) && !($array instanceof \Traversable)) {
throw new \Twig_Error_Runtime(sprintf(
'First argument of "sort_by" must be array or Traversable, but is "%s".', gettype($array)));
}
if ($array instanceof \Traversable) {
if ($array instanceof Dictionary) {
$array = $array->getCopy();
} else {
$array = Dictionary::fromArray($array);
}
return $array->sortBy($callback, $direction);
} else {
$direction = (strtoupper($direction) === 'DESC') ? SORT_DESC : SORT_ASC;
$order = self::map($array, $callback);
array_multisort($order, $direction, SORT_REGULAR, $array);
return $array;
}
}
public static function countBy($array, $callback)
{
if (!is_callable($callback)) {
throw new \Twig_Error_Runtime(sprintf(
'Second argument of "count_by" must be callable, but is "%s".', gettype($callback)));
}
if (!is_array($array) && !($array instanceof \Traversable)) {
throw new \Twig_Error_Runtime(sprintf(
'First argument of "count_by" must be array or Traversable, but is "%s".', gettype($array)));
}
$result = new Dictionary();
foreach ($array as $i => $element) {
$key = $callback($element, $i);
if (is_bool($key)) {
$key = $key ? 'true' : 'false';
} elseif (is_null($key)) {
$key = 'null';
}
if (!isset($result[$key])) {
$result[$key] = 1;
} else {
++$result[$key];
}
}
return $result;
}
public static function every($array, $callback)
{
if (!is_callable($callback)) {
throw new \Twig_Error_Runtime(sprintf(
'Second argument of "every" must be callable, but is "%s".', gettype($callback)));
}
if (!is_array($array) && !($array instanceof \Traversable)) {
throw new \Twig_Error_Runtime(sprintf(
'First argument of "every" must be array or Traversable, but is "%s".', gettype($array)));
}
foreach ($array as $i => $item) {
if (!$callback($item, $i)) {
return false;
}
}
return true;
}
public static function any($array, $callback)
{
if (!is_callable($callback)) {
throw new \Twig_Error_Runtime(sprintf(
'Second argument of "any" must be callable, but is "%s".', gettype($callback)));
}
if (!is_array($array) && !($array instanceof \Traversable)) {
throw new \Twig_Error_Runtime(sprintf(
'First argument of "any" must be array or Traversable, but is "%s".', gettype($array)));
}
foreach ($array as $i => $item) {
if ($callback($item, $i)) {
return true;
}
}
return false;
}
public static function call($callback, array $args = [])
{
if (!is_callable($callback)) {
throw new \InvalidArgumentException('First argument must be callable.');
}
return call_user_func_array($callback, $args);
}
public function getName()
{
return 'dpolac_lambda_extension';
}
}