Skip to content

Commit 005a62e

Browse files
Merge pull request #50 from nreynis/feature/add-includes
Add includes (in_array)
2 parents 5152d0f + eeb9b70 commit 005a62e

File tree

5 files changed

+70
-0
lines changed

5 files changed

+70
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@ $chain->reduce(function ($current, $value) {
203203
- `->countValues()`
204204
- `->every(callable)`
205205
- `->first()`
206+
- `->includes(mixed[, array])`
206207
- `->join([$glue])`
207208
- `->last()`
208209
- `->reduce()`

src/Chain.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Cocur\Chain\Link\Find;
1414
use Cocur\Chain\Link\FlatMap;
1515
use Cocur\Chain\Link\Flip;
16+
use Cocur\Chain\Link\Includes;
1617
use Cocur\Chain\Link\Intersect;
1718
use Cocur\Chain\Link\IntersectAssoc;
1819
use Cocur\Chain\Link\IntersectKey;
@@ -63,6 +64,7 @@ class Chain extends AbstractChain implements Countable
6364
use First;
6465
use FlatMap;
6566
use Flip;
67+
use Includes;
6668
use Intersect;
6769
use IntersectAssoc;
6870
use IntersectKey;

src/Link/Includes.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace Cocur\Chain\Link;
4+
5+
/**
6+
* Includes.
7+
*
8+
* @author Nicolas Reynis
9+
*/
10+
trait Includes
11+
{
12+
/**
13+
* Returns `true` if the given `needle` is in the array or `false` otherwise.
14+
*
15+
* @param $needle
16+
* @param array $options options, including `strict` to also check the type
17+
*
18+
* @return bool
19+
*/
20+
public function includes($needle, array $options = []): bool
21+
{
22+
if (!empty($options['strict'])) {
23+
return in_array($needle, $this->array, $options['strict']);
24+
} else {
25+
return in_array($needle, $this->array);
26+
}
27+
}
28+
}

tests/ChainTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ public function chainHasTraits(): void
8787
$this->assertTrue(method_exists($c, 'first'));
8888
$this->assertTrue(method_exists($c, 'flatMap'));
8989
$this->assertTrue(method_exists($c, 'flip'));
90+
$this->assertTrue(method_exists($c, 'includes'));
9091
$this->assertTrue(method_exists($c, 'intersect'));
9192
$this->assertTrue(method_exists($c, 'intersectAssoc'));
9293
$this->assertTrue(method_exists($c, 'intersectKey'));

tests/Link/IncludesTest.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace Cocur\Chain\Link;
4+
5+
/**
6+
* IncludesTest.
7+
*
8+
* @author Nicolas Reynis
9+
* @group unit
10+
*/
11+
class IncludesTest extends \PHPUnit\Framework\TestCase
12+
{
13+
/**
14+
* @test
15+
* @covers \Cocur\Chain\Link\Includes::includes()
16+
*/
17+
public function includesFindElement(): void
18+
{
19+
/** @var Includes $mock */
20+
$mock = $this->getMockForTrait(Includes::class);
21+
$mock->array = ['foobar', 'bar'];
22+
$this->assertTrue($mock->includes('bar'));
23+
$this->assertFalse($mock->includes('baz'));
24+
}
25+
26+
/**
27+
* @test
28+
* @covers \Cocur\Chain\Link\Includes::includes()
29+
*/
30+
public function includesFindElementStrictly(): void
31+
{
32+
/** @var Includes $mock */
33+
$mock = $this->getMockForTrait(Includes::class);
34+
$mock->array = ['42', '43'];
35+
$this->assertTrue($mock->includes('42', ['strict' => true]));
36+
$this->assertFalse($mock->includes(42, ['strict' => true]));
37+
}
38+
}

0 commit comments

Comments
 (0)