optimize batch function and add tests
authorLuiz “Felds” Liscia <felds@seepix.com.br>
Thu, 21 Feb 2013 15:11:54 +0000 (12:11 -0300)
committerLuiz “Felds” Liscia <felds@seepix.com.br>
Thu, 21 Feb 2013 15:11:54 +0000 (12:11 -0300)
lib/Twig/Extension/Core.php
test/Twig/Tests/Fixtures/filters/batch.test
test/Twig/Tests/Fixtures/filters/batch_with_empty_fill.test [new file with mode: 0644]
test/Twig/Tests/Fixtures/filters/batch_with_fill.test [new file with mode: 0644]

index a856b59..7302e0e 100644 (file)
@@ -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;
index af996f2..cb6de7f 100644 (file)
@@ -1,37 +1,31 @@
 --TEST--
 "batch" filter
 --TEMPLATE--
-<table>
-{% for row in items|batch(3, '') %}
-  <tr>
+{% for row in items|batch(3) %}
+  <div class=row>
   {% for column in row %}
-    <td>{{ column }}</td>
+    <div class=item>{{ column }}</div>
   {% endfor %}
-  </tr>
+  </div>
 {% endfor %}
-</table>
 --DATA--
 return array('items' => array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'))
 --EXPECT--
-<table>
-  <tr>
-      <td>a</td>
-      <td>b</td>
-      <td>c</td>
-    </tr>
-  <tr>
-      <td>d</td>
-      <td>e</td>
-      <td>f</td>
-    </tr>
-  <tr>
-      <td>g</td>
-      <td>h</td>
-      <td>i</td>
-    </tr>
-  <tr>
-      <td>j</td>
-      <td></td>
-      <td></td>
-    </tr>
-</table>
+<div class=row>
+      <div class=item>a</div>
+      <div class=item>b</div>
+      <div class=item>c</div>
+    </div>
+  <div class=row>
+      <div class=item>d</div>
+      <div class=item>e</div>
+      <div class=item>f</div>
+    </div>
+  <div class=row>
+      <div class=item>g</div>
+      <div class=item>h</div>
+      <div class=item>i</div>
+    </div>
+  <div class=row>
+      <div class=item>j</div>
+    </div>
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 (file)
index 0000000..af996f2
--- /dev/null
@@ -0,0 +1,37 @@
+--TEST--
+"batch" filter
+--TEMPLATE--
+<table>
+{% for row in items|batch(3, '') %}
+  <tr>
+  {% for column in row %}
+    <td>{{ column }}</td>
+  {% endfor %}
+  </tr>
+{% endfor %}
+</table>
+--DATA--
+return array('items' => array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'))
+--EXPECT--
+<table>
+  <tr>
+      <td>a</td>
+      <td>b</td>
+      <td>c</td>
+    </tr>
+  <tr>
+      <td>d</td>
+      <td>e</td>
+      <td>f</td>
+    </tr>
+  <tr>
+      <td>g</td>
+      <td>h</td>
+      <td>i</td>
+    </tr>
+  <tr>
+      <td>j</td>
+      <td></td>
+      <td></td>
+    </tr>
+</table>
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 (file)
index 0000000..746295f
--- /dev/null
@@ -0,0 +1,37 @@
+--TEST--
+"batch" filter
+--TEMPLATE--
+<table>
+{% for row in items|batch(3, 'fill') %}
+  <tr>
+  {% for column in row %}
+    <td>{{ column }}</td>
+  {% endfor %}
+  </tr>
+{% endfor %}
+</table>
+--DATA--
+return array('items' => array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'))
+--EXPECT--
+<table>
+  <tr>
+      <td>a</td>
+      <td>b</td>
+      <td>c</td>
+    </tr>
+  <tr>
+      <td>d</td>
+      <td>e</td>
+      <td>f</td>
+    </tr>
+  <tr>
+      <td>g</td>
+      <td>h</td>
+      <td>i</td>
+    </tr>
+  <tr>
+      <td>j</td>
+      <td>fill</td>
+      <td>fill</td>
+    </tr>
+</table>