From 1d3d664071cc9caf454e90f947e8c06efb34c2d6 Mon Sep 17 00:00:00 2001 From: Nicolas Reynis Date: Fri, 8 May 2020 23:24:45 +0200 Subject: [PATCH] feat(some): add supports --- README.md | 1 + src/Chain.php | 2 + src/Link/Some.php | 27 +++++++++++++ tests/ChainTest.php | 1 + tests/Link/SomeTest.php | 87 +++++++++++++++++++++++++++++++++++++++++ 5 files changed, 118 insertions(+) create mode 100644 src/Link/Some.php create mode 100644 tests/Link/SomeTest.php diff --git a/README.md b/README.md index 6563ecc..c5b48cd 100644 --- a/README.md +++ b/README.md @@ -205,6 +205,7 @@ $chain->reduce(function ($current, $value) { - `->join([$glue])` - `->last()` - `->reduce()` +- `->some()` - `->sum()` ## Author diff --git a/src/Chain.php b/src/Chain.php index 84860c2..4aafa89 100644 --- a/src/Chain.php +++ b/src/Chain.php @@ -33,6 +33,7 @@ use Cocur\Chain\Link\Shift; use Cocur\Chain\Link\Shuffle; use Cocur\Chain\Link\Slice; +use Cocur\Chain\Link\Some; use Cocur\Chain\Link\Sort; use Cocur\Chain\Link\SortKeys; use Cocur\Chain\Link\Splice; @@ -81,6 +82,7 @@ class Chain extends AbstractChain implements Countable use Shift; use Shuffle; use Slice; + use Some; use Sort; use SortKeys; use Splice; diff --git a/src/Link/Some.php b/src/Link/Some.php new file mode 100644 index 0000000..f9e958b --- /dev/null +++ b/src/Link/Some.php @@ -0,0 +1,27 @@ +array as $index => $element) { + if ($callback($element, $index)) { + return true; + } + } + + return false; + } +} diff --git a/tests/ChainTest.php b/tests/ChainTest.php index acfecba..7f21940 100644 --- a/tests/ChainTest.php +++ b/tests/ChainTest.php @@ -106,6 +106,7 @@ public function chainHasTraits(): void $this->assertTrue(method_exists($c, 'shift')); $this->assertTrue(method_exists($c, 'shuffle')); $this->assertTrue(method_exists($c, 'slice')); + $this->assertTrue(method_exists($c, 'some')); $this->assertTrue(method_exists($c, 'sort')); $this->assertTrue(method_exists($c, 'sortKeys')); $this->assertTrue(method_exists($c, 'splice')); diff --git a/tests/Link/SomeTest.php b/tests/Link/SomeTest.php new file mode 100644 index 0000000..8021480 --- /dev/null +++ b/tests/Link/SomeTest.php @@ -0,0 +1,87 @@ +getMockForTrait(Some::class); + $mock->array = [1, 2, 3, 4]; + + $this->assertTrue($mock->some(function ($v) { + return $v > 0; + })); + } + + /** + * @test + * @covers \Cocur\Chain\Link\Some::some() + */ + public function someReturnFalseWhenConditionFail(): void + { + /** @var Some $mock */ + $mock = $this->getMockForTrait(Some::class); + $mock->array = [-1, -2, -3, -4]; + + $this->assertFalse($mock->some(function ($v) { + return $v > 0; + })); + } + + /** + * @test + * @covers \Cocur\Chain\Link\Some::some() + */ + public function someReturnTrueWhenConditionFailOnSomeElements(): void + { + /** @var Some $mock */ + $mock = $this->getMockForTrait(Some::class); + $mock->array = [1, 2, -3, 4]; + + $this->assertTrue($mock->some(function ($v) { + return $v > 0; + })); + } + + /** + * @test + * @covers \Cocur\Chain\Link\Some::some() + */ + public function someReturnFalseWithEmptyChain(): void + { + /** @var Some $mock */ + $mock = $this->getMockForTrait(Some::class); + $mock->array = []; + + $this->assertFalse($mock->some(function ($v) { + return true; + })); + } + + /** + * @test + * @covers \Cocur\Chain\Link\Some::some() + */ + public function someCallbackReceivesAlsoArrayKeys(): void + { + /** @var Some $mock */ + $mock = $this->getMockForTrait(Some::class); + $mock->array = ['foo' => 'fizz', 'bar' => 'buzz']; + + $this->assertTrue($mock->some(function ($v, $k) { + return 'foo' === $k || 'bar' === $k; + })); + } +}