mirror of
https://github.com/twigphp/Twig.git
synced 2026-08-30 12:06:56 +00:00
fixed the "filter" filter when the argument is \Traversable but does not implement \Iterator
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
* 1.42.0 (2019-XX-XX)
|
||||
|
||||
* fixed the "filter" filter when the argument is \Traversable but does not implement \Iterator (\SimpleXmlElement for instance)
|
||||
* fixed a PHP fatal error when calling a macro imported in a block in a nested block
|
||||
* fixed a PHP fatal error when calling a macro imported in the template in another macro
|
||||
* fixed wrong error message on "import" and "from"
|
||||
|
||||
@@ -1686,21 +1686,30 @@ function twig_array_batch($items, $size, $fill = null, $preserveKeys = true)
|
||||
return $result;
|
||||
}
|
||||
|
||||
function twig_array_filter($array, $arrow)
|
||||
{
|
||||
if (\is_array($array)) {
|
||||
if (\PHP_VERSION_ID >= 50600) {
|
||||
return array_filter($array, $arrow, \ARRAY_FILTER_USE_BOTH);
|
||||
if (\PHP_VERSION_ID < 50500) {
|
||||
function twig_array_filter($array, $arrow)
|
||||
{
|
||||
if (\is_array($array)) {
|
||||
if (\PHP_VERSION_ID >= 50600) {
|
||||
return array_filter($array, $arrow, \ARRAY_FILTER_USE_BOTH);
|
||||
}
|
||||
|
||||
return array_filter($array, $arrow);
|
||||
}
|
||||
|
||||
return array_filter($array, $arrow);
|
||||
}
|
||||
while ($array instanceof \IteratorAggregate) {
|
||||
$array = $array->getIterator();
|
||||
}
|
||||
|
||||
while ($array instanceof \IteratorAggregate) {
|
||||
$array = $array->getIterator();
|
||||
}
|
||||
// some internal PHP classes are \Traversable but do not implement \Iterator
|
||||
if ($array instanceof \Iterator) {
|
||||
return new \CallbackFilterIterator($array, $arrow);
|
||||
}
|
||||
|
||||
return new \CallbackFilterIterator($array, $arrow);
|
||||
throw new RuntimeException('The "filter" filter argument only supports objects implementing \Iterator; upgrade to PHP 5.5+.');
|
||||
}
|
||||
} else {
|
||||
require_once __DIR__.'/twig_array_filter_55.php';
|
||||
}
|
||||
|
||||
function twig_array_map($array, $arrow)
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
function twig_array_filter($array, $arrow)
|
||||
{
|
||||
foreach ($array as $k => $v) {
|
||||
if ($arrow($v, $k)) {
|
||||
yield $k => $v;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
--TEST--
|
||||
"filter" filter (PHP 5.5 required)
|
||||
--CONDITION--
|
||||
version_compare(phpversion(), '5.5.0', '>=')
|
||||
--TEMPLATE--
|
||||
{% for k, v in xml|filter(x => true) %}
|
||||
{{ k }}/{{ v }}
|
||||
{% endfor %}
|
||||
|
||||
{# we can iterate more than once #}
|
||||
{% for k, v in xml|filter(x => true) %}
|
||||
{{ k }}/{{ v }}
|
||||
{% endfor %}
|
||||
--DATA--
|
||||
return ['xml' => new \SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?><doc><elem>foo</elem><elem>bar</elem><elem>baz</elem></doc>')]
|
||||
--EXPECT--
|
||||
elem/foo
|
||||
elem/bar
|
||||
elem/baz
|
||||
|
||||
elem/foo
|
||||
elem/bar
|
||||
elem/baz
|
||||
@@ -9,6 +9,11 @@ version_compare(phpversion(), '5.6.0', '>=')
|
||||
{{ k }} = {{ v }}
|
||||
{% endfor %}
|
||||
|
||||
{% for k, v in it|filter((v, k) => (v > offset) and (k != "d")) -%}
|
||||
{{ k }} = {{ v }}
|
||||
{% endfor %}
|
||||
|
||||
{# we can iterate more than once #}
|
||||
{% for k, v in it|filter((v, k) => (v > offset) and (k != "d")) -%}
|
||||
{{ k }} = {{ v }}
|
||||
{% endfor %}
|
||||
@@ -18,3 +23,5 @@ return ['it' => new \ArrayIterator(['a' => 1, 'b' => 2, 'c' => 5, 'd' => 8])]
|
||||
c = 5
|
||||
|
||||
c = 5
|
||||
|
||||
c = 5
|
||||
|
||||
Reference in New Issue
Block a user