Skip to content

Commit

Permalink
Move static methods out of the Collection interface.
Browse files Browse the repository at this point in the history
  • Loading branch information
drupol committed Aug 13, 2019
1 parent 6659473 commit 7677b5d
Show file tree
Hide file tree
Showing 23 changed files with 108 additions and 104 deletions.
26 changes: 18 additions & 8 deletions src/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,11 @@ public function walk(callable ...$callbacks): CollectionInterface
}

/**
* {@inheritdoc}
* Create a new collection instance.
*
* @param null|array|callable|Closure|Collection $data
*
* @return \drupol\collection\Contract\Collection
*/
public static function with($data = []): CollectionInterface
{
Expand All @@ -366,27 +370,33 @@ public static function with($data = []): CollectionInterface
}

/**
* {@inheritdoc}
* @param array $data
*
* @return \drupol\collection\Contract\Collection
*/
public static function withArray(array $array): CollectionInterface
public static function withArray(array $data): CollectionInterface
{
$instance = new static();

$instance->source = static function () use ($array) {
yield from $array;
$instance->source = static function () use ($data) {
yield from $data;
};

return $instance;
}

/**
* {@inheritdoc}
* Create a new collection instance.
*
* @param callable $callable
*
* @return \drupol\collection\Contract\Collection
*/
public static function withClosure(callable $callback): CollectionInterface
public static function withClosure(callable $callable): CollectionInterface
{
$instance = new static();

$instance->source = $callback;
$instance->source = $callable;

return $instance;
}
Expand Down
27 changes: 0 additions & 27 deletions src/Contract/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

namespace drupol\collection\Contract;

use Closure;

/**
* Interface Collection.
*/
Expand Down Expand Up @@ -249,31 +247,6 @@ public function slice(int $offset, int $length = null): self;
*/
public function walk(callable ...$callbacks): self;

/**
* Create a new collection instance.
*
* @param null|array|callable|Closure|Collection $data
*
* @return \drupol\collection\Contract\Collection
*/
public static function with($data = []): self;

/**
* @param array $data
*
* @return \drupol\collection\Contract\Collection
*/
public static function withArray(array $data): self;

/**
* Create a new collection instance.
*
* @param callable $callable
*
* @return \drupol\collection\Contract\Collection
*/
public static function withClosure(callable $callable): self;

/**
* Zip the collection together with one or more arrays.
*
Expand Down
7 changes: 4 additions & 3 deletions src/Operation/Append.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

namespace drupol\collection\Operation;

use drupol\collection\Contract\Collection;
use drupol\collection\Collection;
use drupol\collection\Contract\Collection as CollectionInterface;

/**
* Class Append.
Expand All @@ -14,11 +15,11 @@ final class Append extends Operation
/**
* {@inheritdoc}
*/
public function run(Collection $collection): Collection
public function run(CollectionInterface $collection): CollectionInterface
{
$items = $this->parameters;

return $collection::withClosure(
return Collection::withClosure(
static function () use ($items, $collection) {
foreach ($collection as $item) {
yield $item;
Expand Down
11 changes: 6 additions & 5 deletions src/Operation/Chunk.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

namespace drupol\collection\Operation;

use drupol\collection\Contract\Collection;
use drupol\collection\Collection;
use drupol\collection\Contract\Collection as CollectionInterface;

/**
* Class Chunk.
Expand All @@ -14,15 +15,15 @@ final class Chunk extends Operation
/**
* {@inheritdoc}
*/
public function run(Collection $collection): Collection
public function run(CollectionInterface $collection): CollectionInterface
{
$size = $this->parameters[0];

if (0 >= $size) {
return $collection::with();
return Collection::with();
}

return $collection::withClosure(
return Collection::withClosure(
static function () use ($size, $collection) {
$iterator = $collection->getIterator();

Expand All @@ -33,7 +34,7 @@ static function () use ($size, $collection) {
$values[$iterator->key()] = $iterator->current();
}

yield $collection::withArray($values);
yield Collection::withArray($values);
}
}
);
Expand Down
9 changes: 5 additions & 4 deletions src/Operation/Collapse.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

namespace drupol\collection\Operation;

use drupol\collection\Contract\Collection;
use drupol\collection\Collection;
use drupol\collection\Contract\Collection as CollectionInterface;

/**
* Class Collapse.
Expand All @@ -14,12 +15,12 @@ final class Collapse extends Operation
/**
* {@inheritdoc}
*/
public function run(Collection $collection): Collection
public function run(CollectionInterface $collection): CollectionInterface
{
return $collection::withClosure(
return Collection::withClosure(
static function () use ($collection) {
foreach ($collection as $values) {
if (\is_array($values) || $values instanceof Collection) {
if (\is_array($values) || $values instanceof CollectionInterface) {
yield from $values;
}
}
Expand Down
9 changes: 5 additions & 4 deletions src/Operation/Combine.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

namespace drupol\collection\Operation;

use drupol\collection\Contract\Collection;
use drupol\collection\Collection;
use drupol\collection\Contract\Collection as CollectionInterface;

/**
* Class Combine.
Expand All @@ -14,14 +15,14 @@ final class Combine extends Operation
/**
* {@inheritdoc}
*/
public function run(Collection $collection): Collection
public function run(CollectionInterface $collection): CollectionInterface
{
$keys = $this->parameters;

return $collection::withClosure(
return Collection::withClosure(
static function () use ($keys, $collection) {
$original = $collection->getIterator();
$keysIterator = $collection::with($keys)->getIterator();
$keysIterator = Collection::with($keys)->getIterator();

for (; true === ($original->valid() && $keysIterator->valid()); $original->next(), $keysIterator->next()
) {
Expand Down
7 changes: 4 additions & 3 deletions src/Operation/Filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

namespace drupol\collection\Operation;

use drupol\collection\Contract\Collection;
use drupol\collection\Collection;
use drupol\collection\Contract\Collection as CollectionInterface;

/**
* Class Filter.
Expand All @@ -14,7 +15,7 @@ final class Filter extends Operation
/**
* {@inheritdoc}
*/
public function run(Collection $collection): Collection
public function run(CollectionInterface $collection): CollectionInterface
{
$callback = $this->parameters[0];

Expand All @@ -24,7 +25,7 @@ public function run(Collection $collection): Collection
};
}

return $collection::withClosure(
return Collection::withClosure(
static function () use ($callback, $collection) {
foreach ($collection->getIterator() as $key => $value) {
if (true === (bool) $callback($value, $key)) {
Expand Down
9 changes: 5 additions & 4 deletions src/Operation/Flatten.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

namespace drupol\collection\Operation;

use drupol\collection\Contract\Collection;
use drupol\collection\Collection;
use drupol\collection\Contract\Collection as CollectionInterface;

/**
* Class Flatten.
Expand All @@ -14,11 +15,11 @@ final class Flatten extends Operation
/**
* {@inheritdoc}
*/
public function run(Collection $collection): Collection
public function run(CollectionInterface $collection): CollectionInterface
{
$depth = $this->parameters[0];

return $collection::withClosure(
return Collection::withClosure(
static function () use ($depth, $collection) {
$iterator = $collection->getIterator();

Expand All @@ -30,7 +31,7 @@ static function () use ($depth, $collection) {
yield $i;
}
} else {
foreach ($collection::with($item)->flatten($depth - 1) as $flattenItem) {
foreach (Collection::with($item)->flatten($depth - 1) as $flattenItem) {
yield $flattenItem;
}
}
Expand Down
7 changes: 4 additions & 3 deletions src/Operation/Flip.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

namespace drupol\collection\Operation;

use drupol\collection\Contract\Collection;
use drupol\collection\Collection;
use drupol\collection\Contract\Collection as CollectionInterface;

/**
* Class Flip.
Expand All @@ -14,9 +15,9 @@ final class Flip extends Operation
/**
* {@inheritdoc}
*/
public function run(Collection $collection): Collection
public function run(CollectionInterface $collection): CollectionInterface
{
return $collection::withClosure(
return Collection::withClosure(
static function () use ($collection) {
foreach ($collection as $key => $value) {
yield $value => $key;
Expand Down
7 changes: 4 additions & 3 deletions src/Operation/Forget.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

namespace drupol\collection\Operation;

use drupol\collection\Contract\Collection;
use drupol\collection\Collection;
use drupol\collection\Contract\Collection as CollectionInterface;

/**
* Class Forget.
Expand All @@ -14,11 +15,11 @@ final class Forget extends Operation
/**
* {@inheritdoc}
*/
public function run(Collection $collection): Collection
public function run(CollectionInterface $collection): CollectionInterface
{
$keys = $this->parameters;

return $collection::withClosure(
return Collection::withClosure(
static function () use ($keys, $collection) {
$keys = array_flip($keys);

Expand Down
7 changes: 4 additions & 3 deletions src/Operation/Keys.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

namespace drupol\collection\Operation;

use drupol\collection\Contract\Collection;
use drupol\collection\Collection;
use drupol\collection\Contract\Collection as CollectionInterface;

/**
* Class Keys.
Expand All @@ -14,9 +15,9 @@ final class Keys extends Operation
/**
* {@inheritdoc}
*/
public function run(Collection $collection): Collection
public function run(CollectionInterface $collection): CollectionInterface
{
return $collection::withClosure(
return Collection::withClosure(
static function () use ($collection) {
foreach ($collection as $key => $value) {
yield $key;
Expand Down
7 changes: 4 additions & 3 deletions src/Operation/Limit.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

namespace drupol\collection\Operation;

use drupol\collection\Contract\Collection;
use drupol\collection\Collection;
use drupol\collection\Contract\Collection as CollectionInterface;

/**
* Class Limit.
Expand All @@ -14,11 +15,11 @@ final class Limit extends Operation
/**
* {@inheritdoc}
*/
public function run(Collection $collection): Collection
public function run(CollectionInterface $collection): CollectionInterface
{
$limit = $this->parameters[0];

return $collection::withClosure(
return Collection::withClosure(
static function () use ($limit, $collection) {
$iterator = $collection->getIterator();

Expand Down
7 changes: 4 additions & 3 deletions src/Operation/Merge.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

namespace drupol\collection\Operation;

use drupol\collection\Contract\Collection;
use drupol\collection\Collection;
use drupol\collection\Contract\Collection as CollectionInterface;

/**
* Class Merge.
Expand All @@ -14,11 +15,11 @@ final class Merge extends Operation
/**
* {@inheritdoc}
*/
public function run(Collection $collection): Collection
public function run(CollectionInterface $collection): CollectionInterface
{
$sources = $this->parameters;

return $collection::withClosure(
return Collection::withClosure(
static function () use ($sources, $collection) {
foreach ($collection as $item) {
yield $item;
Expand Down

0 comments on commit 7677b5d

Please sign in to comment.