From b78a704f6c8dcecaee20284ef4441d17099e3103 Mon Sep 17 00:00:00 2001 From: Pol Dellaiera Date: Sat, 8 Aug 2020 17:08:37 +0200 Subject: [PATCH] Update Contains transformation, make it variadic. --- spec/loophp/collection/CollectionSpec.php | 8 ++++ src/Collection.php | 4 +- src/Contract/Transformation/Containsable.php | 6 +-- src/Transformation/Contains.php | 41 +++++++++----------- 4 files changed, 32 insertions(+), 27 deletions(-) diff --git a/spec/loophp/collection/CollectionSpec.php b/spec/loophp/collection/CollectionSpec.php index 03c0e1630..140b2a4ee 100644 --- a/spec/loophp/collection/CollectionSpec.php +++ b/spec/loophp/collection/CollectionSpec.php @@ -461,6 +461,14 @@ public function it_can_contains(): void $this::fromIterable(range('A', 'C')) ->contains('unknown') ->shouldReturn(false); + + $this::fromIterable(range('A', 'C')) + ->contains('C', 'A') + ->shouldReturn(true); + + $this::fromIterable(range('A', 'C')) + ->contains('C', 'unknown', 'A') + ->shouldReturn(false); } public function it_can_convert_use_a_string_as_parameter(): void diff --git a/src/Collection.php b/src/Collection.php index 6ba8f5748..76c90f9d1 100644 --- a/src/Collection.php +++ b/src/Collection.php @@ -276,9 +276,9 @@ public function compact(...$values): CollectionInterface return $this->run(new Compact(...$values)); } - public function contains($value): bool + public function contains(...$value): bool { - return $this->transform(new Contains($value)); + return $this->transform(new Contains(...$value)); } public function count(): int diff --git a/src/Contract/Transformation/Containsable.php b/src/Contract/Transformation/Containsable.php index 9109409a0..4c1ccc3af 100644 --- a/src/Contract/Transformation/Containsable.php +++ b/src/Contract/Transformation/Containsable.php @@ -10,8 +10,8 @@ interface Containsable { /** - * @param mixed $value - * @psalm-param T $value + * @param mixed ...$value + * @psalm-param T ...$value */ - public function contains($value): bool; + public function contains(...$value): bool; } diff --git a/src/Transformation/Contains.php b/src/Transformation/Contains.php index 94e08f4db..94c2f8dbe 100644 --- a/src/Transformation/Contains.php +++ b/src/Transformation/Contains.php @@ -4,6 +4,7 @@ namespace loophp\collection\Transformation; +use ArrayIterator; use Iterator; use loophp\collection\Contract\Transformation; @@ -17,18 +18,18 @@ final class Contains implements Transformation { /** - * @var mixed - * @psalm-var T + * @var ArrayIterator + * @psalm-var \ArrayIterator */ private $value; /** - * @param mixed $value - * @psalm-param T $value + * @param mixed ...$value + * @psalm-param T ...$value */ - public function __construct($value) + public function __construct(...$value) { - $this->value = $value; + $this->value = new ArrayIterator($value); } /** @@ -40,21 +41,17 @@ public function __invoke(Iterator $collection) { $value = $this->value; - $hasCallback = - /** - * @param mixed $k - * @psalm-param TKey $k - * - * @param mixed $v - * @psalm-param T $v - * - * @return mixed - * @psalm-return T - */ - static function ($k, $v) use ($value) { - return $value; - }; - - return (bool) (new Transform(new Has($hasCallback)))($collection); + return (new FoldLeft( + static function (bool $carry, $item, $key) use ($collection) { + $hasCallback = static function ($k, $v) use ($item) { + return $item; + }; + + return ((new Transform(new Has($hasCallback)))($collection)) ? + $carry : + false; + }, + true + ))($value); } }