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
-------
750cb237 fixed batch filter clobbers array keys when fill parameter is used
ede9a604 added preserveKeys support for the batch filter
... | ... |
@@ -1,5 +1,7 @@ |
1 | 1 |
* 1.38.0 (2019-XX-XX) |
2 | 2 |
|
3 |
+ * fixed batch filter clobbers array keys when fill parameter is used |
|
4 |
+ * added preserveKeys support for the batch filter |
|
3 | 5 |
* fixed "embed" support when used from "template_from_string" |
4 | 6 |
* added the possibility to pass a TemplateWrapper to Twig\Environment::load() |
5 | 7 |
* improved the performance of the sandbox |
... | ... |
@@ -1638,23 +1638,22 @@ function twig_constant_is_defined($constant, $object = null) |
1638 | 1638 |
* |
1639 | 1639 |
* @return array |
1640 | 1640 |
*/ |
1641 |
-function twig_array_batch($items, $size, $fill = null) |
|
1641 |
+function twig_array_batch($items, $size, $fill = null, $preserveKeys = true) |
|
1642 | 1642 |
{ |
1643 | 1643 |
if ($items instanceof \Traversable) { |
1644 |
- $items = iterator_to_array($items, false); |
|
1644 |
+ $items = iterator_to_array($items, $preserveKeys); |
|
1645 | 1645 |
} |
1646 | 1646 |
|
1647 | 1647 |
$size = ceil($size); |
1648 | 1648 |
|
1649 |
- $result = array_chunk($items, $size, true); |
|
1649 |
+ $result = array_chunk($items, $size, $preserveKeys); |
|
1650 | 1650 |
|
1651 |
- if (null !== $fill && !empty($result)) { |
|
1651 |
+ if (null !== $fill && $result) { |
|
1652 | 1652 |
$last = \count($result) - 1; |
1653 | 1653 |
if ($fillCount = $size - \count($result[$last])) { |
1654 |
- $result[$last] = array_merge( |
|
1655 |
- $result[$last], |
|
1656 |
- array_fill(0, $fillCount, $fill) |
|
1657 |
- ); |
|
1654 |
+ for ($i = 0; $i < $fillCount; $i++) { |
|
1655 |
+ $result[$last][] = $fill; |
|
1656 |
+ } |
|
1658 | 1657 |
} |
1659 | 1658 |
} |
1660 | 1659 |
|
... | ... |
@@ -1,8 +1,8 @@ |
1 | 1 |
--TEST-- |
2 | 2 |
"batch" filter preserves array keys |
3 | 3 |
--TEMPLATE-- |
4 |
-{{ {'foo': 'bar', 'key': 'value'}|batch(4)|first|keys|join(',') }} |
|
5 |
-{{ {'foo': 'bar', 'key': 'value'}|batch(4, 'fill')|first|keys|join(',') }} |
|
4 |
+{{ {'foo': 'bar', 'key': 'value'}|batch(4)|first|keys|join(',') }} |
|
5 |
+{{ {'foo': 'bar', 'key': 'value'}|batch(4, 'fill')|first|keys|join(',') }} |
|
6 | 6 |
--DATA-- |
7 | 7 |
return [] |
8 | 8 |
--EXPECT-- |
9 | 9 |
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> |