Skip to content

Commit

Permalink
Let the Filter operation use a variadic parameter.
Browse files Browse the repository at this point in the history
  • Loading branch information
drupol committed Aug 20, 2019
1 parent b4050b5 commit fd081c2
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 11 deletions.
4 changes: 2 additions & 2 deletions src/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,9 @@ public static function empty(): CollectionInterface
/**
* {@inheritdoc}
*/
public function filter(callable $callback = null): CollectionInterface
public function filter(callable ...$callbacks): CollectionInterface
{
return $this->run(Filter::with($callback));
return $this->run(Filter::with($callbacks));
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/Contract/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,11 @@ public function count(): int;
/**
* Run a filter over each of the items.
*
* @param null|callable $callback
* @param callable ...$callbacks
*
* @return \drupol\collection\Contract\Collection
*/
public function filter(callable $callback = null): self;
public function filter(callable ...$callbacks): self;

/**
* Get the first item from the collection passing the given truth test.
Expand Down
16 changes: 9 additions & 7 deletions src/Operation/Filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,21 @@ final class Filter extends Operation
*/
public function run(CollectionInterface $collection): CollectionInterface
{
$callback = $this->parameters[0];
[$callbacks] = $this->parameters;

if (null === $callback) {
$callback = static function ($value) {
if ([] === $callbacks) {
$callbacks[] = static function ($value) {
return $value;
};
}

return Collection::with(
static function () use ($callback, $collection) {
foreach ($collection->getIterator() as $key => $value) {
if (true === (bool) $callback($value, $key)) {
yield $key => $value;
static function () use ($callbacks, $collection) {
foreach ($callbacks as $callback) {
foreach ($collection->getIterator() as $key => $value) {
if (true === (bool) $callback($value, $key)) {
yield $key => $value;
}
}
}
}
Expand Down

0 comments on commit fd081c2

Please sign in to comment.