diff --git a/README.md b/README.md index b9389e9..84de644 100644 --- a/README.md +++ b/README.md @@ -203,6 +203,7 @@ $chain->reduce(function ($current, $value) { - `->countValues()` - `->every(callable)` - `->first()` +- `->includes(mixed[, array])` - `->join([$glue])` - `->last()` - `->reduce()` diff --git a/src/Chain.php b/src/Chain.php index 006894f..d445db7 100644 --- a/src/Chain.php +++ b/src/Chain.php @@ -13,6 +13,7 @@ use Cocur\Chain\Link\Find; use Cocur\Chain\Link\FlatMap; use Cocur\Chain\Link\Flip; +use Cocur\Chain\Link\Includes; use Cocur\Chain\Link\Intersect; use Cocur\Chain\Link\IntersectAssoc; use Cocur\Chain\Link\IntersectKey; @@ -63,6 +64,7 @@ class Chain extends AbstractChain implements Countable use First; use FlatMap; use Flip; + use Includes; use Intersect; use IntersectAssoc; use IntersectKey; diff --git a/src/Link/Includes.php b/src/Link/Includes.php new file mode 100644 index 0000000..d4a0f98 --- /dev/null +++ b/src/Link/Includes.php @@ -0,0 +1,28 @@ +array, $options['strict']); + } else { + return in_array($needle, $this->array); + } + } +} diff --git a/tests/ChainTest.php b/tests/ChainTest.php index 4d0e530..428e221 100644 --- a/tests/ChainTest.php +++ b/tests/ChainTest.php @@ -87,6 +87,7 @@ public function chainHasTraits(): void $this->assertTrue(method_exists($c, 'first')); $this->assertTrue(method_exists($c, 'flatMap')); $this->assertTrue(method_exists($c, 'flip')); + $this->assertTrue(method_exists($c, 'includes')); $this->assertTrue(method_exists($c, 'intersect')); $this->assertTrue(method_exists($c, 'intersectAssoc')); $this->assertTrue(method_exists($c, 'intersectKey')); diff --git a/tests/Link/IncludesTest.php b/tests/Link/IncludesTest.php new file mode 100644 index 0000000..e0f2207 --- /dev/null +++ b/tests/Link/IncludesTest.php @@ -0,0 +1,38 @@ +getMockForTrait(Includes::class); + $mock->array = ['foobar', 'bar']; + $this->assertTrue($mock->includes('bar')); + $this->assertFalse($mock->includes('baz')); + } + + /** + * @test + * @covers \Cocur\Chain\Link\Includes::includes() + */ + public function includesFindElementStrictly(): void + { + /** @var Includes $mock */ + $mock = $this->getMockForTrait(Includes::class); + $mock->array = ['42', '43']; + $this->assertTrue($mock->includes('42', ['strict' => true])); + $this->assertFalse($mock->includes(42, ['strict' => true])); + } +}