... | ... |
@@ -1,5 +1,6 @@ |
1 | 1 |
* 1.38.0 (2019-XX-XX) |
2 | 2 |
|
3 |
+ * added preserveKeys support for the batch filter |
|
3 | 4 |
* fixed "embed" support when used from "template_from_string" |
4 | 5 |
* added the possibility to pass a TemplateWrapper to Twig\Environment::load() |
5 | 6 |
* improved the performance of the sandbox |
... | ... |
@@ -1638,23 +1638,20 @@ 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 |
+ $result[$last] = array_merge($result[$last], array_fill(0, $fillCount, $fill)); |
|
1658 | 1655 |
} |
1659 | 1656 |
} |
1660 | 1657 |
|
... | ... |
@@ -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-- |