diff --git a/README.md b/README.md index dd7bf10..0062a5a 100644 --- a/README.md +++ b/README.md @@ -405,17 +405,14 @@ $a->each(); // [0 => 0, 'key' => 0, 1 => 'a', 'value' => 'a'] ### exists -A custom contains function where you can supply your own contains function as a closure. -The closure must take two parameters, a needle and an element it will be comparing. +A custom contains method where you can supply your own custom logic in any callable function. ``` php $a = A::create(['a', 'b', 'c']); -$closure = function($needle, $element) { - return $needle === $element; -}; - -$a->exists($closure, 'a'); // true +$a->exists(function($key, $value) { + return 1 === $key and 'b' === $value; +}); // true ``` ### export diff --git a/src/AbstractArray.php b/src/AbstractArray.php index f737f5c..623cce8 100644 --- a/src/AbstractArray.php +++ b/src/AbstractArray.php @@ -431,15 +431,14 @@ public function containsKey($key) /** * Find the given value in the array using a closure * - * @param callable $callable - * @param mixed $needle + * @param callable $func * * @return bool Returns true if the given value is found, false otherwise */ - public function exists(callable $callable, $needle) + public function exists(callable $func) { - foreach ($this->elements as $el) { - if ($callable($needle, $el)) { + foreach ($this->elements as $key => $value) { + if ($func($key, $value)) { return true; } } diff --git a/tests/AbstractArrayTest.php b/tests/AbstractArrayTest.php index 9e86bdb..3593a39 100644 --- a/tests/AbstractArrayTest.php +++ b/tests/AbstractArrayTest.php @@ -203,16 +203,14 @@ public function testEach(array $array) */ public function testExists(array $array) { - $key = 2; - - $closure = function($needle, $element){ - return $needle === $element; + $callable = function($key, $value) { + return 2 === $key and 'two' === $value; }; $arrayzy = $this->createArrayzy($array); - $isContains = in_array($key, $array); + $isExists = isset($array[2]) && 'two' === $array[2]; - $this->assertSame($isContains, $arrayzy->exists($closure, $key)); + $this->assertSame($isExists, $arrayzy->exists($callable)); } /**