Skip to content

Commit

Permalink
bug #3065 Fix the filter filter (fabpot)
Browse files Browse the repository at this point in the history
This PR was merged into the 2.x branch.

Discussion
----------

Fix the filter filter

closes #3063

Commits
-------

29ad145 fixed the filter filter
  • Loading branch information
fabpot committed Jun 14, 2019
2 parents b957ff6 + 29ad145 commit 1107684
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
* 2.11.3 (2019-XX-XX)

* fixed the filter filter (allow the result to be used several times)
* fixed macro auto-import when a template contains only macros

* 2.11.2 (2019-06-05)
Expand Down
9 changes: 5 additions & 4 deletions src/Extension/CoreExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -1522,11 +1522,12 @@ function twig_array_column($array, $name): array

function twig_array_filter($array, $arrow)
{
foreach ($array as $k => $v) {
if ($arrow($v, $k)) {
yield $k => $v;
}
if (\is_array($array)) {
return array_filter($array, $arrow, \ARRAY_FILTER_USE_BOTH);
}

// the IteratorIterator wrapping is needed as some internal PHP classes are \Traversable but do not implement \Iterator
return new \CallbackFilterIterator(new \IteratorIterator($array), $arrow);
}

function twig_array_map($array, $arrow)
Expand Down
7 changes: 7 additions & 0 deletions test/Twig/Tests/Fixtures/filters/filter.test
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@
{% for k, v in xml|filter(x => true) %}
{{ k }}/{{ v }}
{% endfor %}

{% set coll = ['a', 'b']|filter(v => v is same as('a')) %}
{% if coll|length > 0 %}
{{- coll|join(', ') }}
{% endif %}
--DATA--
return [
'it' => new \ArrayIterator(['a' => 1, 'b' => 2, 'c' => 5, 'd' => 8]),
Expand Down Expand Up @@ -68,3 +73,5 @@ elem/baz
elem/foo
elem/bar
elem/baz

a

0 comments on commit 1107684

Please sign in to comment.