Skip to content

Commit

Permalink
fix batch filter with zero items
Browse files Browse the repository at this point in the history
also added a test for preserving keys behavior
  • Loading branch information
Tobion committed Jul 2, 2015
1 parent ae448f3 commit 8d6be28
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/Twig/Extension/Core.php
Original file line number Diff line number Diff line change
Expand Up @@ -1488,7 +1488,7 @@ function twig_array_batch($items, $size, $fill = null)

$result = array_chunk($items, $size, true);

if (null !== $fill) {
if (null !== $fill && !empty($result)) {
$last = count($result) - 1;
if ($fillCount = $size - count($result[$last])) {
$result[$last] = array_merge(
Expand Down
10 changes: 10 additions & 0 deletions test/Twig/Tests/Fixtures/filters/batch_with_keys.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
--TEST--
"batch" filter preserves array keys
--TEMPLATE--
{{ {'foo': 'bar', 'key': 'value'}|batch(4)|first|keys|join(',') }}
{{ {'foo': 'bar', 'key': 'value'}|batch(4, 'fill')|first|keys|join(',') }}
--DATA--
return array()
--EXPECT--
foo,key
foo,key,0,1
10 changes: 10 additions & 0 deletions test/Twig/Tests/Fixtures/filters/batch_with_zero_elements.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
--TEST--
"batch" filter with zero elements
--TEMPLATE--
{{ []|batch(3)|length }}
{{ []|batch(3, 'fill')|length }}
--DATA--
return array()
--EXPECT--
0
0

0 comments on commit 8d6be28

Please sign in to comment.