From: Marcin Misiurski Date: Mon, 5 Aug 2013 15:48:38 +0000 (+0200) Subject: Don't use array_fill for full array X-Git-Url: http://git.silmor.de/gitweb/?a=commitdiff_plain;h=270c7943b19e24acfdfe883898bb74eee4b81eb8;p=web%2Fkonrad%2Ftwig.git Don't use array_fill for full array --- diff --git a/lib/Twig/Extension/Core.php b/lib/Twig/Extension/Core.php index e68687b..7eb7f9b 100644 --- a/lib/Twig/Extension/Core.php +++ b/lib/Twig/Extension/Core.php @@ -1329,7 +1329,7 @@ function twig_constant($constant, $object = null) * * @param array $items An array of items * @param integer $size The size of the batch - * @param string $fill A string to fill missing items + * @param mixed $fill A value used to fill missing items * * @return array */ @@ -1345,10 +1345,12 @@ function twig_array_batch($items, $size, $fill = null) if (null !== $fill) { $last = count($result) - 1; - $result[$last] = array_merge( - $result[$last], - array_fill(0, $size - count($result[$last]), $fill) - ); + if ($fillCount = $size - count($result[$last])) { + $result[$last] = array_merge( + $result[$last], + array_fill(0, $fillCount, $fill) + ); + } } return $result; diff --git a/test/Twig/Tests/Fixtures/filters/batch_with_exact_elements.test b/test/Twig/Tests/Fixtures/filters/batch_with_exact_elements.test new file mode 100644 index 0000000..72483f4 --- /dev/null +++ b/test/Twig/Tests/Fixtures/filters/batch_with_exact_elements.test @@ -0,0 +1,33 @@ +--TEST-- +"batch" filter +--TEMPLATE-- +{% for row in items|batch(3, 'fill') %} +
+ {% for column in row %} +
{{ column }}
+ {% endfor %} +
+{% endfor %} +--DATA-- +return array('items' => array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l')) +--EXPECT-- +
+
a
+
b
+
c
+
+
+
d
+
e
+
f
+
+
+
g
+
h
+
i
+
+
+
j
+
k
+
l
+