Skip to content

Commit

Permalink
Update Contains transformation, make it variadic.
Browse files Browse the repository at this point in the history
  • Loading branch information
drupol committed Aug 10, 2020
1 parent fa5a396 commit b78a704
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 27 deletions.
8 changes: 8 additions & 0 deletions spec/loophp/collection/CollectionSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions src/Contract/Transformation/Containsable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
41 changes: 19 additions & 22 deletions src/Transformation/Contains.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace loophp\collection\Transformation;

use ArrayIterator;
use Iterator;
use loophp\collection\Contract\Transformation;

Expand All @@ -17,18 +18,18 @@
final class Contains implements Transformation
{
/**
* @var mixed
* @psalm-var T
* @var ArrayIterator<int, mixed>
* @psalm-var \ArrayIterator<int, T>
*/
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);
}

/**
Expand All @@ -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);
}
}

0 comments on commit b78a704

Please sign in to comment.