bug #1724 fix batch filter with zero items (Tobion)

This PR was merged into the 1.x branch.

Discussion
----------

fix batch filter with zero items

| Q             | A
| ------------- | ---
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| License       | MIT

also added a test for preserving keys behavior

Commits
-------

8d6be28 fix batch filter with zero items
This commit is contained in:
Fabien Potencier
2015-07-03 08:37:27 +02:00
3 changed files with 21 additions and 1 deletions
+1 -1
View File
@@ -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(
@@ -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
@@ -0,0 +1,10 @@
--TEST--
"batch" filter with zero elements
--TEMPLATE--
{{ []|batch(3)|length }}
{{ []|batch(3, 'fill')|length }}
--DATA--
return array()
--EXPECT--
0
0