Skip to content
This repository has been archived by the owner on Jan 1, 2021. It is now read-only.

Commit

Permalink
containsExactly() - PHP-specific variant of contains()
Browse files Browse the repository at this point in the history
  • Loading branch information
sanmai committed Jul 15, 2018
1 parent 1d1307b commit d0595df
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/LazyLINQ/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,17 @@ private function containsAny($value)
return false;
}

public function containsExactly($value): bool
{
foreach ($this->pipeline as $sample) {
if ($sample === $value) {
return true;
}
}

return false;
}

public function count(callable $predicate = null)
{
if ($predicate) {
Expand Down
9 changes: 9 additions & 0 deletions src/LazyLINQ/Interfaces/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,15 @@ public function concat($second);
*/
public function contains($value, callable $comparer = null);

/**
* Determines whether the selected elements include exactly specified element by using an identity comparer. PHP-specific.
*
* @param mixed $value the value to locate in the sequence
*
* @return bool
*/
public function containsExactly($value): bool;

/**
* Returns a number that represents how many elements in the specified sequence satisfy an optional condition.
*
Expand Down
5 changes: 5 additions & 0 deletions src/LazyLINQ/LazyCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,11 @@ public function contains($value, callable $comparer = null)
return $this->immediate()->contains($value, $comparer);
}

public function containsExactly($value): bool
{
return $this->immediate()->containsExactly($value);
}

public function selectMany(callable $selector = null)
{
return $this->defer(__FUNCTION__, $selector);
Expand Down
13 changes: 13 additions & 0 deletions tests/LazyLINQ/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,19 @@ public function testContains()
$this->assertFalse(static::from(['test', 'testing'])->contains('foo', function ($a, $b) {
return strlen($a) == strlen($b);
}));

$this->assertTrue(static::from([1, 2, 4])->contains(2.0));
$this->assertTrue(static::from([0, M_PI])->contains(M_PI));
}

/**
* @covers \LazyLINQ\Collection::containsExactly
*/
public function testContainsExactly()
{
$this->assertFalse(static::from([1, 2, 4])->containsExactly(2.0));
$this->assertTrue(static::from([1.0, 2.0, 4.0])->containsExactly(2.0));
$this->assertTrue(static::from([0, M_PI])->containsExactly(M_PI));
}

/**
Expand Down

0 comments on commit d0595df

Please sign in to comment.