From: Andrey Kutejko Date: Mon, 7 Oct 2013 16:46:14 +0000 (+0300) Subject: fix chunks padding X-Git-Tag: 0.1~9 X-Git-Url: https://git.andy128k.dev/?a=commitdiff_plain;h=d373e8699a08084110540d38f156129303ee7f18;p=missing-tools.git fix chunks padding --- diff --git a/src/collectiontools.php b/src/collectiontools.php index eb2a42d..fd62b2a 100644 --- a/src/collectiontools.php +++ b/src/collectiontools.php @@ -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; diff --git a/t/CollectionTest.php b/t/CollectionTest.php index 64a40d9..dcf952f 100644 --- a/t/CollectionTest.php +++ b/t/CollectionTest.php @@ -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])); + } }