Skip to content

Commit

Permalink
Update Contains operation.
Browse files Browse the repository at this point in the history
  • Loading branch information
drupol committed Jul 29, 2020
1 parent 5c69049 commit 4723545
Showing 1 changed file with 30 additions and 28 deletions.
58 changes: 30 additions & 28 deletions src/Transformation/Contains.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,47 +5,49 @@
namespace loophp\collection\Transformation;

use loophp\collection\Contract\Transformation;
use stdClass;

use function is_callable;
use function is_string;

/**
* @template TKey
* @psalm-template TKey of array-key
* @template T
*
* @implements Transformation<TKey, T>
*/
final class Contains implements Transformation
{
/**
* @var mixed
* @psalm-var T
*/
private $key;
private $value;

/**
* Contains constructor.
*
* @param mixed $key
* @param mixed $value
* @psalm-param T $value
*/
public function __construct($key)
public function __construct($value)
{
$this->key = $key;
$this->value = $value;
}

/**
* {@inheritdoc}
*/
public function __invoke(iterable $collection)
{
$key = $this->key;

if ((false === is_string($key)) && (true === is_callable($key))) {
$placeholder = new stdClass();

return (new First($key, $placeholder))($collection) !== $placeholder;
}

foreach ($collection as $value) {
if ($value === $key) {
return true;
}
}

return false;
$value = $this->value;

return (
new Transform(
new Has(
/**
* @param TKey $k
* @param T $v
*
* @return T
*/
static function ($k, $v) use ($value) {
return $value;
}
)
)
)($collection);
}
}

0 comments on commit 4723545

Please sign in to comment.