]> git.andy128k.dev Git - missing-tools.git/commitdiff
fix chunks padding
authorAndrey Kutejko <andy128k@gmail.com>
Mon, 7 Oct 2013 16:46:14 +0000 (19:46 +0300)
committerAndrey Kutejko <andy128k@gmail.com>
Mon, 7 Oct 2013 16:46:14 +0000 (19:46 +0300)
src/collectiontools.php
t/CollectionTest.php

index eb2a42dcaf56f38fa349e3164725a5b42b7085f4..fd62b2a26befc60269256866bbaa7286cb242816 100644 (file)
@@ -46,9 +46,12 @@ final class Collections
         }
         $c = count($chunk);
         if ($c != 0) {
-            if ($pad)
-                while ($c < $size)
+            if ($pad) {
+                while ($c < $size) {
                     $chunk[] = null;
+                    ++$c;
+                }
+            }
             $result[] = $chunk;
         }
         return $result;
index 64a40d984a7f370e8d5bf9c87761ab8aa0b25403..dcf952f3e9abc2a60a2f4646b6fbac368057a23e 100644 (file)
@@ -30,5 +30,19 @@ class CollectionsTest extends PHPUnit_Framework_TestCase
         $this->assertEquals(3, count($chunks[0]));
         $this->assertEquals(1, count($chunks[1]));
     }
+
+    public function testChunksPad()
+    {
+        $arr = array(
+            'apple',
+            'grapefruit',
+            'carrot',
+            'tomato',
+        );
+        $chunks = Collections::chunks($arr, 3, true);
+        $this->assertEquals(2, count($chunks));
+        $this->assertEquals(3, count($chunks[0]));
+        $this->assertEquals(3, count($chunks[1]));
+    }
 }