bug #1637 Updates slice so that it still accepts a SimpleXMLElement (bob-p)

This PR was merged into the 1.18-dev branch.

Discussion
----------

Updates slice so that it still accepts a SimpleXMLElement

Since #1503 you cannot pass a SimpleXMLElement to slice as you get:

   Catchable fatal error: Argument 1 passed to LimitIterator::__construct() must implement interface Iterator, instance of SimpleXMLElement given

As SimpleXMLElement implements Traversable but not Iterator

This checks that $item is an instance of Iterator before passing to LimitIterator

Commits
-------

aea075b Updates slice so that it still accepts a SimpleXMLElement
This commit is contained in:
Fabien Potencier
2015-02-27 15:25:52 +01:00
2 changed files with 5 additions and 3 deletions
+1 -1
View File
@@ -707,7 +707,7 @@ function twig_slice(Twig_Environment $env, $item, $start, $length = null, $prese
$item = $item->getIterator();
}
if ($start >= 0 && $length >= 0) {
if ($start >= 0 && $length >= 0 && $item instanceof Iterator) {
try {
return iterator_to_array(new LimitIterator($item, $start, $length === null ? -1 : $length), $preserveKeys);
} catch (OutOfBoundsException $exception) {
+4 -2
View File
@@ -24,8 +24,9 @@
{{ arr|slice(3)|join('') }}
{{ arr[2:]|join('') }}
{{ xml|slice(1)|join('')}}
--DATA--
return array('start' => 1, 'length' => 2, 'arr' => new ArrayObject(array(1, 2, 3, 4)))
return array('start' => 1, 'length' => 2, 'arr' => new ArrayObject(array(1, 2, 3, 4)), 'xml' => new SimpleXMLElement('<items><item>1</item><item>2</item></items>'))
--EXPECT--
23
23
@@ -49,4 +50,5 @@ bc
1
4
34
34
2