Skip to content

Commit

Permalink
Differentiate Operation and Transformation.
Browse files Browse the repository at this point in the history
  • Loading branch information
drupol committed Sep 6, 2019
1 parent 3e59719 commit 3546cfa
Show file tree
Hide file tree
Showing 21 changed files with 165 additions and 90 deletions.
36 changes: 15 additions & 21 deletions spec/drupol/collection/CollectionSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -832,41 +832,35 @@ public function it_can_rsample(): void
public function it_can_run_an_operation(Operation $operation): void
{
$square = new class() implements Operation {
public function on(iterable $collection)
public function on(iterable $collection): \Closure
{
return Collection::with(
static function () use ($collection) {
foreach ($collection as $item) {
yield $item ** 2;
}
return static function () use ($collection) {
foreach ($collection as $item) {
yield $item ** 2;
}
);
};
}
};

$sqrt = new class() implements Operation {
public function on(iterable $collection)
public function on(iterable $collection): \Closure
{
return Collection::with(
static function () use ($collection) {
foreach ($collection as $item) {
yield $item ** .5;
}
return static function () use ($collection) {
foreach ($collection as $item) {
yield $item ** .5;
}
);
};
}
};

$map = new class() implements Operation {
public function on(iterable $collection)
public function on(iterable $collection): \Closure
{
return Collection::with(
static function () use ($collection) {
foreach ($collection as $item) {
yield (int) $item;
}
return static function () use ($collection) {
foreach ($collection as $item) {
yield (int) $item;
}
);
};
}
};

Expand Down
12 changes: 6 additions & 6 deletions src/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@

use drupol\collection\Contract\Base as BaseInterface;
use drupol\collection\Contract\Operation;
use drupol\collection\Contract\Transformer;
use drupol\collection\Iterator\ClosureIterator;
use drupol\collection\Operation\Run;
use drupol\collection\Transformation\Run;
use drupol\collection\Transformation\Transform;

/**
* Class Base.
Expand Down Expand Up @@ -66,12 +68,10 @@ public function run(Operation ...$operations)
}

/**
* @param array $data
*
* @return \drupol\collection\Contract\Base
* {@inheritdoc}
*/
public static function with($data = []): BaseInterface
public function transform(Transformer ...$transformers)
{
return new static($data);
return (new Transform(...$transformers))->on($this);
}
}
42 changes: 21 additions & 21 deletions src/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,18 @@

use drupol\collection\Contract\Base as BaseInterface;
use drupol\collection\Contract\Collection as CollectionInterface;
use drupol\collection\Operation\All;
use drupol\collection\Operation\Append;
use drupol\collection\Operation\Apply;
use drupol\collection\Operation\Chunk;
use drupol\collection\Operation\Collapse;
use drupol\collection\Operation\Combine;
use drupol\collection\Operation\Contains;
use drupol\collection\Operation\Count;
use drupol\collection\Operation\Distinct;
use drupol\collection\Operation\Filter;
use drupol\collection\Operation\First;
use drupol\collection\Operation\Flatten;
use drupol\collection\Operation\Flip;
use drupol\collection\Operation\Forget;
use drupol\collection\Operation\Get;
use drupol\collection\Operation\Implode;
use drupol\collection\Operation\Intersperse;
use drupol\collection\Operation\Keys;
use drupol\collection\Operation\Last;
use drupol\collection\Operation\Limit;
use drupol\collection\Operation\Merge;
use drupol\collection\Operation\Normalize;
Expand All @@ -34,14 +27,21 @@
use drupol\collection\Operation\Pluck;
use drupol\collection\Operation\Prepend;
use drupol\collection\Operation\Range;
use drupol\collection\Operation\Reduce;
use drupol\collection\Operation\Reduction;
use drupol\collection\Operation\Run;
use drupol\collection\Operation\Skip;
use drupol\collection\Operation\Slice;
use drupol\collection\Operation\Sort;
use drupol\collection\Operation\Walk;
use drupol\collection\Operation\Zip;
use drupol\collection\Transformation\All;
use drupol\collection\Transformation\Contains;
use drupol\collection\Transformation\Count;
use drupol\collection\Transformation\First;
use drupol\collection\Transformation\Get;
use drupol\collection\Transformation\Implode;
use drupol\collection\Transformation\Last;
use drupol\collection\Transformation\Reduce;
use drupol\collection\Transformation\Run;

/**
* Class Collection.
Expand All @@ -53,7 +53,7 @@ final class Collection extends Base implements CollectionInterface
*/
public function all(): array
{
return $this->run(new All());
return $this->transform(new All());
}

/**
Expand Down Expand Up @@ -111,15 +111,15 @@ public function combine($keys): BaseInterface
*/
public function contains($key): bool
{
return $this->run(new Contains($key));
return $this->transform(new Contains($key));
}

/**
* {@inheritdoc}
*/
public function count(): int
{
return $this->run(new Count());
return $this->transform(new Count());
}

/**
Expand Down Expand Up @@ -157,7 +157,7 @@ public function filter(callable ...$callbacks): BaseInterface
*/
public function first(callable $callback = null, $default = null)
{
return $this->run(new First($callback, $default));
return $this->transform(new First($callback, $default));
}

/**
Expand Down Expand Up @@ -195,15 +195,15 @@ public function forget(...$keys): BaseInterface
*/
public function get($key, $default = null)
{
return $this->run(new Get($key, $default));
return $this->transform(new Get($key, $default));
}

/**
* {@inheritdoc}
*/
public function implode(string $implode = ''): string
public function implode(string $glue = ''): string
{
return (new Implode($implode))->on($this);
return $this->transform(new Implode($glue));
}

/**
Expand Down Expand Up @@ -253,7 +253,7 @@ public function keys(): BaseInterface
*/
public function last()
{
return $this->run(new Last());
return $this->transform(new Last());
}

/**
Expand Down Expand Up @@ -363,15 +363,15 @@ public static function range(int $start = 0, $end = \INF, $step = 1): Collection
*/
public function rebase(): BaseInterface
{
return new Collection($this->run(new All()));
return new Collection($this->transform(new All()));
}

/**
* {@inheritdoc}
*/
public function reduce(callable $callback, $initial = null)
{
return $this->run(new Reduce($callback, $initial));
return $this->transform(new Reduce($callback, $initial));
}

/**
Expand Down Expand Up @@ -461,9 +461,9 @@ public function walk(callable ...$callbacks): BaseInterface
}

/**
* {@inheritdoc}
* @param array $data
*
* @return \drupol\collection\Contract\Collection
* @return \drupol\collection\Contract\Base
*/
public static function with($data = []): BaseInterface
{
Expand Down
2 changes: 1 addition & 1 deletion src/Contract/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
/**
* Interface Base.
*/
interface Base extends \IteratorAggregate, Runable
interface Base extends \IteratorAggregate, Runable, Transformable
{
}
6 changes: 3 additions & 3 deletions src/Contract/Operation.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
/**
* Interface Operation.
*/
interface Operation
interface Operation extends Transformer
{
/**
* @param iterable $collection
*
* @return mixed
* @return \Closure
*/
public function on(iterable $collection);
public function on(iterable $collection): \Closure;
}
18 changes: 18 additions & 0 deletions src/Contract/Transformable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace drupol\collection\Contract;

/**
* Interface Transformable.
*/
interface Transformable
{
/**
* @param \drupol\collection\Contract\Transformer ...$transformers
*
* @return bool|int|mixed|string
*/
public function transform(Transformer ...$transformers);
}
18 changes: 18 additions & 0 deletions src/Contract/Transformer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace drupol\collection\Contract;

/**
* Interface Transformer.
*/
interface Transformer
{
/**
* @param iterable $collection
*
* @return bool|mixed
*/
public function on(iterable $collection);
}
2 changes: 1 addition & 1 deletion src/Operation/Distinct.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ final class Distinct implements Operation
/**
* {@inheritdoc}
*/
public function on(iterable $collection)
public function on(iterable $collection): \Closure
{
return static function () use ($collection) {
$seen = new Collection([]);
Expand Down
2 changes: 1 addition & 1 deletion src/Operation/Only.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function __construct(...$keys)
/**
* {@inheritdoc}
*/
public function on(iterable $collection)
public function on(iterable $collection): \Closure
{
[$keys] = $this->keys;

Expand Down
1 change: 1 addition & 0 deletions src/Operation/Pluck.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use ArrayAccess;
use drupol\collection\Contract\Collection;
use drupol\collection\Contract\Operation;
use drupol\collection\Transformation\Get;

/**
* Class Pluck.
Expand Down
2 changes: 1 addition & 1 deletion src/Operation/Reduction.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function __construct(callable $callback, $initial)
/**
* {@inheritdoc}
*/
public function on(iterable $collection)
public function on(iterable $collection): \Closure
{
$callback = $this->callback;
$initial = $this->initial;
Expand Down
6 changes: 3 additions & 3 deletions src/Operation/All.php → src/Transformation/All.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

declare(strict_types=1);

namespace drupol\collection\Operation;
namespace drupol\collection\Transformation;

use drupol\collection\Contract\Operation;
use drupol\collection\Contract\Transformer;

/**
* Class All.
*/
final class All implements Operation
final class All implements Transformer
{
/**
* {@inheritdoc}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

declare(strict_types=1);

namespace drupol\collection\Operation;
namespace drupol\collection\Transformation;

use drupol\collection\Contract\Operation;
use drupol\collection\Contract\Transformer;

/**
* Class Contains.
*/
final class Contains implements Operation
final class Contains implements Transformer
{
/**
* @var mixed
Expand Down
6 changes: 3 additions & 3 deletions src/Operation/Count.php → src/Transformation/Count.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@

declare(strict_types=1);

namespace drupol\collection\Operation;
namespace drupol\collection\Transformation;

use drupol\collection\Contract\Operation;
use drupol\collection\Contract\Transformer;
use drupol\collection\Iterator\ClosureIterator;

/**
* Class Count.
*
* Be careful, this will only work with finite collection sets.
*/
final class Count implements Operation
final class Count implements Transformer
{
/**
* {@inheritdoc}
Expand Down

0 comments on commit 3546cfa

Please sign in to comment.