Browse code

fixed batch filter clobbers array keys when fill parameter is used

Fabien Potencier authored on 12/03/2019 09:48:58
Showing 3 changed files
... ...
@@ -1,5 +1,6 @@
1 1
 * 1.38.0 (2019-XX-XX)
2 2
 
3
+ * fixed batch filter clobbers array keys when fill parameter is used
3 4
  * added preserveKeys support for the batch filter
4 5
  * fixed "embed" support when used from "template_from_string"
5 6
  * added the possibility to pass a TemplateWrapper to Twig\Environment::load()
... ...
@@ -1651,7 +1651,9 @@ function twig_array_batch($items, $size, $fill = null, $preserveKeys = true)
1651 1651
     if (null !== $fill && $result) {
1652 1652
         $last = \count($result) - 1;
1653 1653
         if ($fillCount = $size - \count($result[$last])) {
1654
-            $result[$last] = array_merge($result[$last], array_fill(0, $fillCount, $fill));
1654
+            for ($i = 0; $i < $fillCount; $i++) {
1655
+                $result[$last][] = $fill;
1656
+            }
1655 1657
         }
1656 1658
     }
1657 1659
 
1658 1660
new file mode 100644
... ...
@@ -0,0 +1,23 @@
1
+--TEST--
2
+"batch" filter
3
+--TEMPLATE--
4
+{% for row in items|batch(3, 'fill') %}
5
+  <div class=row>
6
+  {% for key, column in row %}
7
+    <div class={{ key }}>{{ column }}</div>
8
+  {% endfor %}
9
+  </div>
10
+{% endfor %}
11
+--DATA--
12
+return ['items' => ['a' => 'a', 'b' => 'b', 'c' => 'c', 'd' => 'd', '123' => 'e']]
13
+--EXPECT--
14
+<div class=row>
15
+      <div class=a>a</div>
16
+      <div class=b>b</div>
17
+      <div class=c>c</div>
18
+    </div>
19
+  <div class=row>
20
+      <div class=d>d</div>
21
+      <div class=123>e</div>
22
+      <div class=124>fill</div>
23
+    </div>