Skip to content

Commit

Permalink
Add Run operation.
Browse files Browse the repository at this point in the history
  • Loading branch information
drupol committed Aug 20, 2019
1 parent a41491c commit 1bf34d4
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 22 deletions.
46 changes: 41 additions & 5 deletions spec/drupol/collection/CollectionSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 2 additions & 17 deletions src/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -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.
*
Expand Down
39 changes: 39 additions & 0 deletions src/Operation/Run.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

declare(strict_types=1);

namespace drupol\collection\Operation;

use drupol\collection\Contract\Collection as CollectionInterface;

/**
* Class Run.
*/
final class Run extends Operation
{
/**
* {@inheritdoc}
*/
public function run(CollectionInterface $collection)
{
[$operations] = $this->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);
}
}

0 comments on commit 1bf34d4

Please sign in to comment.