Skip to content

Commit 5c15f89

Browse files
committed
added the key to the map and filter filters
1 parent 13274bb commit 5c15f89

6 files changed

Lines changed: 60 additions & 5 deletions

File tree

doc/filters/filter.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,15 @@ function. The arrow function receives the value of the sequence or mapping:
2929
{% endfor %}
3030
{# output l = 40 xl = 42 #}
3131
32+
The arrow function also receives the key as a second argument:
33+
34+
.. code-block:: twig
35+
36+
{% for k, v in sizes|filter((v, k) => v > 38 and k != "xl") -%}
37+
{{ k }} = {{ v }}
38+
{% endfor %}
39+
{# output l = 40 #}
40+
3241
Note that the arrow function has access to the current context.
3342

3443
Arguments

doc/filters/map.rst

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,18 @@ mapping. The arrow function receives the value of the sequence or mapping:
1717
{{ people|map(p => "#{p.first} #{p.last}")|join(', ') }}
1818
{# outputs Bob Smith, Alice Dupond #}
1919
20+
The arrow function also receives the key as a second argument:
21+
22+
.. code-block:: twig
23+
24+
{% set people = {
25+
"Bob": "Smith",
26+
"Alice": "Dupond",
27+
} %}
28+
29+
{{ people|map((first, last) => "#{first} #{last}")|join(', ') }}
30+
{# outputs Bob Smith, Alice Dupond #}
31+
2032
Note that the arrow function has access to the current context.
2133

2234
Arguments

src/Extension/CoreExtension.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1689,6 +1689,10 @@ function twig_array_batch($items, $size, $fill = null, $preserveKeys = true)
16891689
function twig_array_filter($array, $arrow)
16901690
{
16911691
if (\is_array($array)) {
1692+
if (\PHP_VERSION_ID >= 50600) {
1693+
return array_filter($array, $arrow, \ARRAY_FILTER_USE_BOTH);
1694+
}
1695+
16921696
return array_filter($array, $arrow);
16931697
}
16941698

@@ -1697,11 +1701,12 @@ function twig_array_filter($array, $arrow)
16971701

16981702
function twig_array_map($array, $arrow)
16991703
{
1700-
if (!\is_array($array)) {
1701-
$array = iterator_to_array($array);
1704+
$r = [];
1705+
foreach ($array as $k => $v) {
1706+
$r[$k] = $arrow($v, $k);
17021707
}
17031708

1704-
return array_map($arrow, $array);
1709+
return $r;
17051710
}
17061711

17071712
function twig_array_reduce($array, $arrow, $initial = null)

test/Twig/Tests/Fixtures/filters/filter.test

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
{{ k }} = {{ v }}
88
{% endfor %}
99

10-
{% for k, v in {a: 1, b: 2, c: 5, d: 2}|filter((v) => v > offset) -%}
10+
{% for k, v in {a: 1, b: 2, c: 5, d: 8}|filter(v => v > offset) -%}
1111
{{ k }} = {{ v }}
1212
{% endfor %}
1313

@@ -19,16 +19,18 @@
1919
{{ k }} = {{ v }}
2020
{% endfor %}
2121
--DATA--
22-
return ['it' => new \ArrayIterator(['a' => 1, 'b' => 2, 'c' => 5, 'd' => 2])]
22+
return ['it' => new \ArrayIterator(['a' => 1, 'b' => 2, 'c' => 5, 'd' => 8])]
2323
--EXPECT--
2424
1 = 5
2525
3 = 4
2626
4 = 5
2727

2828
c = 5
29+
d = 8
2930

3031
1 = 5
3132
3 = 4
3233
4 = 5
3334

3435
c = 5
36+
d = 8
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
--TEST--
2+
"filter" filter (PHP 5.6 required)
3+
--CONDITION--
4+
version_compare(phpversion(), '5.6.0', '>=')
5+
--TEMPLATE--
6+
{% set offset = 3 %}
7+
8+
{% for k, v in {a: 1, b: 2, c: 5, d: 8}|filter((v, k) => (v > offset) and (k != "d")) -%}
9+
{{ k }} = {{ v }}
10+
{% endfor %}
11+
12+
{% for k, v in it|filter((v, k) => (v > offset) and (k != "d")) -%}
13+
{{ k }} = {{ v }}
14+
{% endfor %}
15+
--DATA--
16+
return ['it' => new \ArrayIterator(['a' => 1, 'b' => 2, 'c' => 5, 'd' => 8])]
17+
--EXPECT--
18+
c = 5
19+
20+
c = 5

test/Twig/Tests/Fixtures/filters/map.test

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111
{{ k }} = {{ v }}
1212
{% endfor %}
1313

14+
{% for k, v in {a: 1, b: 2}|map((item, k) => item ~ "*" ~ k ) -%}
15+
{{ k }} = {{ v }}
16+
{% endfor %}
17+
1418
{% for k, v in [1, 2]|map(item => item + 2 ) -%}
1519
{{ k }} = {{ v }}
1620
{% endfor %}
@@ -27,6 +31,9 @@ return ['it' => new \ArrayIterator([1, 2])]
2731
a = 1*
2832
b = 2*
2933

34+
a = 1*a
35+
b = 2*b
36+
3037
0 = 3
3138
1 = 4
3239

0 commit comments

Comments
 (0)