From 8662b58d676b1a74eaa6bdc34c98f6d488eaf783 Mon Sep 17 00:00:00 2001 From: Andrey Kutejko Date: Fri, 2 Jan 2015 18:03:15 +0200 Subject: [PATCH] collection inits utility --- src/collection.php | 14 ++++++++++++++ t/CollectionTest.php | 15 +++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/src/collection.php b/src/collection.php index d9b39ae..11b31e1 100644 --- a/src/collection.php +++ b/src/collection.php @@ -91,5 +91,19 @@ final class Collection } return $groups; } + + public static function inits($collection) + { + $result = array(); + $init = array(); + + $result[] = $init; + foreach ($collection as $item) { + $init = array_merge($init, array($item)); + $result[] = $init; + } + + return $result; + } } diff --git a/t/CollectionTest.php b/t/CollectionTest.php index a1c505e..24db627 100644 --- a/t/CollectionTest.php +++ b/t/CollectionTest.php @@ -58,5 +58,20 @@ class CollectionTest extends PHPUnit_Framework_TestCase $this->assertEquals(2, count($groups['red'])); $this->assertEquals(2, count($groups['green'])); } + + public function testInits() + { + $this->assertEquals(array(array()), + \PFF\Collection::inits(array())); + + $this->assertEquals(array(array(), array('1')), + \PFF\Collection::inits(array('1'))); + + $this->assertEquals(array(array(), array('1'), array('1', 2)), + \PFF\Collection::inits(array('1', 2))); + + $this->assertEquals(array(array(), array(1), array(1, '2'), array(1, '2', 3)), + \PFF\Collection::inits(array(1, '2', 3))); + } } -- 2.49.0