Skip to content

Commit

Permalink
Update Collection::compact() and let user provide parameters.
Browse files Browse the repository at this point in the history
  • Loading branch information
drupol committed Jul 16, 2020
1 parent 9970d38 commit 7733252
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 15 deletions.
7 changes: 5 additions & 2 deletions docs/pages/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -304,12 +304,15 @@ Remove all the null values from the collection.

Interface: `Compactable`_

Signature: ``Collection::compact();``
Signature: ``Collection::compact(...$values);``

.. code-block:: php
$collection = Collection::with(['a', 1 => 'b', null, false, 0, 'c'];)
->combine();
->compact(); // ['a', 1 => 'b', 3 => false, 4 => 0, 5 => 'c']
$collection = Collection::with(['a', 1 => 'b', null, false, 0, 'c'];)
->compact(null, 0); // ['a', 1 => 'b', 3 => false, 5 => 'c']
cycle
~~~~~
Expand Down
4 changes: 4 additions & 0 deletions spec/loophp/collection/CollectionSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,10 @@ public function it_can_compact(): void
$this::fromIterable($input)
->compact()
->shouldIterateAs(['a', 1 => 'b', 3 => false, 4 => 0, 5 => 'c']);

$this::fromIterable($input)
->compact(null, 0)
->shouldIterateAs(['a', 1 => 'b', 3 => false, 5 => 'c']);
}

public function it_can_contains(): void
Expand Down
4 changes: 2 additions & 2 deletions src/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,9 @@ public function combine(...$keys): BaseInterface
return $this->run(new Combine(...$keys));
}

public function compact(): BaseInterface
public function compact(...$values): BaseInterface
{
return $this->run(new Compact());
return $this->run(new Compact(...$values));
}

public function contains($key): bool
Expand Down
4 changes: 3 additions & 1 deletion src/Contract/Operation/Compactable.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ interface Compactable
/**
* Combine a collection of items with some other keys.
*
* @param mixed ...$values
*
* @return \loophp\collection\Base|\loophp\collection\Contract\Collection
*/
public function compact(): Base;
public function compact(...$values): Base;
}
37 changes: 27 additions & 10 deletions src/Operation/Compact.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,37 @@
use loophp\collection\Contract\Operation;
use loophp\collection\Transformation\Run;

use function in_array;

final class Compact extends AbstractOperation implements Operation
{
/**
* @param mixed ...$values
*/
public function __construct(...$values)
{
$this->storage['values'] = [] === $values ? [null] : $values;
}

public function __invoke(): Closure
{
return static function (iterable $collection): Generator {
return yield from
(new Run(
new Filter(
static function ($item): bool {
return null !== $item;
}
return
/**
* @param array<int, mixed> $values
*/
static function (iterable $collection, $values): Generator {
return yield from
(new Run(
new Filter(
/**
* @param mixed $item
*/
static function ($item) use ($values): bool {
return !in_array($item, $values, true);
}
)
)
)
)($collection);
};
)($collection);
};
}
}

0 comments on commit 7733252

Please sign in to comment.