From 1bf34d46f929854a642deb3e53c35ef01048df8b Mon Sep 17 00:00:00 2001 From: Pol Dellaiera Date: Tue, 20 Aug 2019 10:50:14 +0200 Subject: [PATCH] Add Run operation. --- spec/drupol/collection/CollectionSpec.php | 46 ++++++++++++++++++++--- src/Collection.php | 19 +--------- src/Operation/Run.php | 39 +++++++++++++++++++ 3 files changed, 82 insertions(+), 22 deletions(-) create mode 100644 src/Operation/Run.php diff --git a/spec/drupol/collection/CollectionSpec.php b/spec/drupol/collection/CollectionSpec.php index 183078eee..8e4a89f2f 100644 --- a/spec/drupol/collection/CollectionSpec.php +++ b/spec/drupol/collection/CollectionSpec.php @@ -685,12 +685,48 @@ static function ($carry, $item) { public function it_can_run_an_operation(Operation $operation): void { - $operation - ->run($this) - ->shouldBeCalledOnce(); + $square = new class() extends \drupol\collection\Operation\Operation { + public function run(CollectionInterface $collection) + { + return Collection::with( + static function () use ($collection) { + foreach ($collection as $item) { + yield $item ** 2; + } + } + ); + } + }; - $this - ->run($operation); + $sqrt = new class() extends \drupol\collection\Operation\Operation { + public function run(CollectionInterface $collection) + { + return Collection::with( + static function () use ($collection) { + foreach ($collection as $item) { + yield $item ** .5; + } + } + ); + } + }; + + $map = new class() extends \drupol\collection\Operation\Operation { + public function run(CollectionInterface $collection) + { + return Collection::with( + static function () use ($collection) { + foreach ($collection as $item) { + yield (int) $item; + } + } + ); + } + }; + + $this::with(\range(1, 5)) + ->run($square, $sqrt, $map) + ->shouldIterateAs(\range(1, 5)); } public function it_can_skip(): void diff --git a/src/Collection.php b/src/Collection.php index d600457b4..387bced2d 100644 --- a/src/Collection.php +++ b/src/Collection.php @@ -34,6 +34,7 @@ use drupol\collection\Operation\Range; use drupol\collection\Operation\Rebase; use drupol\collection\Operation\Reduce; +use drupol\collection\Operation\Run; use drupol\collection\Operation\Skip; use drupol\collection\Operation\Slice; use drupol\collection\Operation\Walk; @@ -301,7 +302,7 @@ public function reduce(callable $callback, $initial = null) */ public function run(Operation ...$operations) { - return \array_reduce($operations, [$this, 'doRun'], $this); + return Run::with($operations)->run($this); } /** @@ -377,22 +378,6 @@ public function zip(...$items): CollectionInterface return $this->run(Zip::with($items)); } - /** - * Run an operation on the collection. - * - * @param \drupol\collection\Contract\Collection $collection - * The collection. - * @param \drupol\collection\Contract\Operation $operation - * The operation. - * - * @return mixed - * The operation result. - */ - private function doRun(CollectionInterface $collection, Operation $operation) - { - return $operation->run($collection); - } - /** * Get items as an array. * diff --git a/src/Operation/Run.php b/src/Operation/Run.php new file mode 100644 index 000000000..99e3ffd3f --- /dev/null +++ b/src/Operation/Run.php @@ -0,0 +1,39 @@ +parameters; + + return \array_reduce($operations, [$this, 'doRun'], $collection); + } + + /** + * Run an operation on the collection. + * + * @param \drupol\collection\Contract\Collection $collection + * The collection. + * @param \drupol\collection\Operation\Operation $operation + * The operation. + * + * @return mixed + * The operation result. + */ + private function doRun(CollectionInterface $collection, Operation $operation) + { + return $operation->run($collection); + } +}