diff --git a/docs/pages/api.rst b/docs/pages/api.rst index b36fc0e1f..969d7cda7 100644 --- a/docs/pages/api.rst +++ b/docs/pages/api.rst @@ -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 ~~~~~ diff --git a/spec/loophp/collection/CollectionSpec.php b/spec/loophp/collection/CollectionSpec.php index 180cd6ec1..32514bf0e 100644 --- a/spec/loophp/collection/CollectionSpec.php +++ b/spec/loophp/collection/CollectionSpec.php @@ -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 diff --git a/src/Collection.php b/src/Collection.php index 28f31daf4..140480735 100644 --- a/src/Collection.php +++ b/src/Collection.php @@ -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 diff --git a/src/Contract/Operation/Compactable.php b/src/Contract/Operation/Compactable.php index 7c96e270e..b59a7208d 100644 --- a/src/Contract/Operation/Compactable.php +++ b/src/Contract/Operation/Compactable.php @@ -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; } diff --git a/src/Operation/Compact.php b/src/Operation/Compact.php index 293dd1eb7..a655e271e 100644 --- a/src/Operation/Compact.php +++ b/src/Operation/Compact.php @@ -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 $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); + }; } }