Navigation Menu

Skip to content

Commit

Permalink
Updates slice so that it still accepts a SimpleXMLElement
Browse files Browse the repository at this point in the history
  • Loading branch information
bob-p committed Feb 26, 2015
1 parent fb8b04e commit aea075b
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/Twig/Extension/Core.php
Expand Up @@ -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) {
Expand Down
6 changes: 4 additions & 2 deletions test/Twig/Tests/Fixtures/filters/slice.test
Expand Up @@ -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
Expand All @@ -49,4 +50,5 @@ bc
1

4
34
34
2

0 comments on commit aea075b

Please sign in to comment.