Skip to content

Commit

Permalink
bug #2884 Fix "batch filter clobbers array keys when fill parameter i…
Browse files Browse the repository at this point in the history
…s used " (fabpot)

This PR was squashed before being merged into the 1.x branch (closes #2884).

Discussion
----------

Fix "batch filter clobbers array keys when fill parameter is used "

closes #2568

Commits
-------

750cb23 fixed batch filter clobbers array keys when fill parameter is used
ede9a60 added preserveKeys support for the batch filter
  • Loading branch information
fabpot committed Mar 12, 2019
2 parents 4eeaf76 + 750cb23 commit 7e30569
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 10 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG
@@ -1,5 +1,7 @@
* 1.38.0 (2019-XX-XX)

* fixed batch filter clobbers array keys when fill parameter is used
* added preserveKeys support for the batch filter
* fixed "embed" support when used from "template_from_string"
* added the possibility to pass a TemplateWrapper to Twig\Environment::load()
* improved the performance of the sandbox
Expand Down
15 changes: 7 additions & 8 deletions src/Extension/CoreExtension.php
Expand Up @@ -1638,23 +1638,22 @@ function twig_constant_is_defined($constant, $object = null)
*
* @return array
*/
function twig_array_batch($items, $size, $fill = null)
function twig_array_batch($items, $size, $fill = null, $preserveKeys = true)
{
if ($items instanceof \Traversable) {
$items = iterator_to_array($items, false);
$items = iterator_to_array($items, $preserveKeys);
}

$size = ceil($size);

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

if (null !== $fill && !empty($result)) {
if (null !== $fill && $result) {
$last = \count($result) - 1;
if ($fillCount = $size - \count($result[$last])) {
$result[$last] = array_merge(
$result[$last],
array_fill(0, $fillCount, $fill)
);
for ($i = 0; $i < $fillCount; $i++) {
$result[$last][] = $fill;
}
}
}

Expand Down
4 changes: 2 additions & 2 deletions test/Twig/Tests/Fixtures/filters/batch_with_keys.test
@@ -1,8 +1,8 @@
--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(',') }}
{{ {'foo': 'bar', 'key': 'value'}|batch(4)|first|keys|join(',') }}
{{ {'foo': 'bar', 'key': 'value'}|batch(4, 'fill')|first|keys|join(',') }}
--DATA--
return []
--EXPECT--
Expand Down
23 changes: 23 additions & 0 deletions test/Twig/Tests/Fixtures/filters/batch_with_more_elements.test
@@ -0,0 +1,23 @@
--TEST--
"batch" filter
--TEMPLATE--
{% for row in items|batch(3, 'fill') %}
<div class=row>
{% for key, column in row %}
<div class={{ key }}>{{ column }}</div>
{% endfor %}
</div>
{% endfor %}
--DATA--
return ['items' => ['a' => 'a', 'b' => 'b', 'c' => 'c', 'd' => 'd', '123' => 'e']]
--EXPECT--
<div class=row>
<div class=a>a</div>
<div class=b>b</div>
<div class=c>c</div>
</div>
<div class=row>
<div class=d>d</div>
<div class=123>e</div>
<div class=124>fill</div>
</div>

0 comments on commit 7e30569

Please sign in to comment.