From ccf3cd1c81711849c311d0d66b402ef2592c60a3 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Luiz=20=E2=80=9CFelds=E2=80=9D=20Liscia?= Date: Thu, 21 Feb 2013 12:11:54 -0300 Subject: [PATCH] optimize batch function and add tests --- lib/Twig/Extension/Core.php | 7 ++- test/Twig/Tests/Fixtures/filters/batch.test | 50 +++++++++----------- .../Fixtures/filters/batch_with_empty_fill.test | 37 ++++++++++++++ .../Tests/Fixtures/filters/batch_with_fill.test | 37 ++++++++++++++ 4 files changed, 100 insertions(+), 31 deletions(-) create mode 100644 test/Twig/Tests/Fixtures/filters/batch_with_empty_fill.test create mode 100644 test/Twig/Tests/Fixtures/filters/batch_with_fill.test diff --git a/lib/Twig/Extension/Core.php b/lib/Twig/Extension/Core.php index a856b59..7302e0e 100644 --- a/lib/Twig/Extension/Core.php +++ b/lib/Twig/Extension/Core.php @@ -1328,9 +1328,10 @@ function twig_array_batch($items, $size, $fill = null) if (null !== $fill) { $last = count($result) - 1; - while (count($result[$last]) < $size) { - $result[$last][] = $fill; - } + $result[$last] = array_merge( + $result[$last], + array_fill(0, $size - count($result[$last]), $fill) + ); } return $result; diff --git a/test/Twig/Tests/Fixtures/filters/batch.test b/test/Twig/Tests/Fixtures/filters/batch.test index af996f2..cb6de7f 100644 --- a/test/Twig/Tests/Fixtures/filters/batch.test +++ b/test/Twig/Tests/Fixtures/filters/batch.test @@ -1,37 +1,31 @@ --TEST-- "batch" filter --TEMPLATE-- - -{% for row in items|batch(3, '') %} - +{% for row in items|batch(3) %} +
{% for column in row %} -
+
{{ column }}
{% endfor %} - + {% endfor %} -
{{ column }}
--DATA-- return array('items' => array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j')) --EXPECT-- - - - - - - - - - - - - - - - - - - - - - -
abc
def
ghi
j
+
+
a
+
b
+
c
+
+
+
d
+
e
+
f
+
+
+
g
+
h
+
i
+
+
+
j
+
diff --git a/test/Twig/Tests/Fixtures/filters/batch_with_empty_fill.test b/test/Twig/Tests/Fixtures/filters/batch_with_empty_fill.test new file mode 100644 index 0000000..af996f2 --- /dev/null +++ b/test/Twig/Tests/Fixtures/filters/batch_with_empty_fill.test @@ -0,0 +1,37 @@ +--TEST-- +"batch" filter +--TEMPLATE-- + +{% for row in items|batch(3, '') %} + + {% for column in row %} + + {% endfor %} + +{% endfor %} +
{{ column }}
+--DATA-- +return array('items' => array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j')) +--EXPECT-- + + + + + + + + + + + + + + + + + + + + + +
abc
def
ghi
j
diff --git a/test/Twig/Tests/Fixtures/filters/batch_with_fill.test b/test/Twig/Tests/Fixtures/filters/batch_with_fill.test new file mode 100644 index 0000000..746295f --- /dev/null +++ b/test/Twig/Tests/Fixtures/filters/batch_with_fill.test @@ -0,0 +1,37 @@ +--TEST-- +"batch" filter +--TEMPLATE-- + +{% for row in items|batch(3, 'fill') %} + + {% for column in row %} + + {% endfor %} + +{% endfor %} +
{{ column }}
+--DATA-- +return array('items' => array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j')) +--EXPECT-- + + + + + + + + + + + + + + + + + + + + + +
abc
def
ghi
jfillfill
-- 1.7.2.5