From eeb9b7061cd1286088b8ad5d20c452ead7eb9f24 Mon Sep 17 00:00:00 2001 From: Nicolas Reynis Date: Sun, 15 Dec 2019 14:27:58 +0100 Subject: [PATCH] Add includes (in_array) --- README.md | 1 + src/Chain.php | 2 ++ src/Link/Includes.php | 28 +++++++++++++++++++++++++++ tests/ChainTest.php | 1 + tests/Link/IncludesTest.php | 38 +++++++++++++++++++++++++++++++++++++ 5 files changed, 70 insertions(+) create mode 100644 src/Link/Includes.php create mode 100644 tests/Link/IncludesTest.php diff --git a/README.md b/README.md index 6563ecc..0f9cf9f 100644 --- a/README.md +++ b/README.md @@ -202,6 +202,7 @@ $chain->reduce(function ($current, $value) { - `->count()` - `->countValues()` - `->first()` +- `->includes(mixed[, array])` - `->join([$glue])` - `->last()` - `->reduce()` diff --git a/src/Chain.php b/src/Chain.php index 84860c2..d3bda75 100644 --- a/src/Chain.php +++ b/src/Chain.php @@ -12,6 +12,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; @@ -60,6 +61,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 acfecba..2a1606e 100644 --- a/tests/ChainTest.php +++ b/tests/ChainTest.php @@ -86,6 +86,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])); + } +}