From 940beb409bd7347f637c25f9c9f9c624c828ebe5 Mon Sep 17 00:00:00 2001 From: Pol Dellaiera Date: Sat, 28 Dec 2019 18:00:42 +0100 Subject: [PATCH] Update Cycle operation. --- spec/drupol/collection/CollectionSpec.php | 7 +++--- src/Collection.php | 4 ++-- src/Contract/Cycleable.php | 4 ++-- src/Operation/Cycle.php | 29 +++++++++++++++-------- 4 files changed, 26 insertions(+), 18 deletions(-) diff --git a/spec/drupol/collection/CollectionSpec.php b/spec/drupol/collection/CollectionSpec.php index 857ea6607..b46c790a3 100644 --- a/spec/drupol/collection/CollectionSpec.php +++ b/spec/drupol/collection/CollectionSpec.php @@ -323,16 +323,15 @@ public function it_can_cycle(): void ->beConstructedThrough('with', [['1', '2', '3']]); $this - ->cycle(1) + ->cycle(3) ->shouldIterateAs(['1', '2', '3']); $this - ->cycle(2) + ->cycle(6) ->shouldIterateAs(['1', '2', '3', '1', '2', '3']); $this - ->cycle() - ->limit(7) + ->cycle(7) ->shouldIterateAs(['1', '2', '3', '1', '2', '3', '1']); } diff --git a/src/Collection.php b/src/Collection.php index 2ee8a379e..cfa719264 100644 --- a/src/Collection.php +++ b/src/Collection.php @@ -137,9 +137,9 @@ public function count(): int * * @return \drupol\collection\Contract\Collection */ - public function cycle(int $count = 0): BaseInterface + public function cycle(int $length = 0): BaseInterface { - return $this->run(new Cycle($count)); + return $this->run(new Cycle($length)); } /** diff --git a/src/Contract/Cycleable.php b/src/Contract/Cycleable.php index 394b55fd4..ce4e41163 100644 --- a/src/Contract/Cycleable.php +++ b/src/Contract/Cycleable.php @@ -10,9 +10,9 @@ interface Cycleable { /** - * @param int $count + * @param int $length * * @return \drupol\collection\Contract\Collection */ - public function cycle(int $count = 0): Base; + public function cycle(int $length = 0): Base; } diff --git a/src/Operation/Cycle.php b/src/Operation/Cycle.php index 8524253c6..ee5277a04 100644 --- a/src/Operation/Cycle.php +++ b/src/Operation/Cycle.php @@ -6,7 +6,10 @@ use Closure; use drupol\collection\Contract\Operation; +use drupol\collection\Iterator\IterableIterator; use Generator; +use InfiniteIterator; +use LimitIterator; /** * Class Cycle. @@ -16,16 +19,16 @@ final class Cycle implements Operation /** * @var int */ - private $count; + private $length; /** * Cycle constructor. * - * @param int $count + * @param int $length */ - public function __construct(int $count = 0) + public function __construct(int $length = 0) { - $this->count = $count; + $this->length = $length; } /** @@ -33,13 +36,19 @@ public function __construct(int $count = 0) */ public function on(iterable $collection): Closure { - $count = $this->count; + $length = $this->length; - return static function () use ($collection, $count): Generator { - for ($j = 0 === $count ? 1 : 0; $j !== $count; ++$j) { - foreach ($collection as $value) { - yield $value; - } + return static function () use ($collection, $length): Generator { + $cycleIterator = new LimitIterator( + new InfiniteIterator( + new IterableIterator($collection) + ), + 0, + $length + ); + + foreach ($cycleIterator as $value) { + yield $value; } }; }