Skip to content

Commit d5d7252

Browse files
committed
Merge pull request bocharsky-bw#25 from m1/exists-function
Add exists() method in order to check existence using callable function
2 parents 25608da + cc0ef10 commit d5d7252

File tree

5 files changed

+56
-2
lines changed

5 files changed

+56
-2
lines changed

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ $a = $a->shuffle(); // override instance you operates on, because $a !== $a->shu
6161
* [debug](#debug)
6262
* [diffWith](#diffwith)
6363
* [each](#each)
64+
* [exists](#exists)
6465
* [export](#export)
6566
* [filter](#filter)
6667
* [find](#find)
@@ -402,6 +403,21 @@ $a = A::create(['a', 'b', 'c']);
402403
$a->each(); // [0 => 0, 'key' => 0, 1 => 'a', 'value' => 'a']
403404
```
404405

406+
### exists
407+
408+
A custom contains function where you can supply your own contains function as a closure.
409+
The closure must take two parameters, a needle and an element it will be comparing.
410+
411+
``` php
412+
$a = A::create(['a', 'b', 'c']);
413+
414+
$closure = function($needle, $element) {
415+
return $needle === $element;
416+
};
417+
418+
$a->exists($closure, 'a'); // true
419+
```
420+
405421
### export
406422

407423
``` php

src/AbstractArray.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,25 @@ public function containsKey($key)
428428
return array_key_exists($key, $this->elements);
429429
}
430430

431+
/**
432+
* Find the given value in the array using a closure
433+
*
434+
* @param callable $callable
435+
* @param mixed $needle
436+
*
437+
* @return bool Returns true if the given value is found, false otherwise
438+
*/
439+
public function exists(callable $callable, $needle)
440+
{
441+
foreach ($this->elements as $el) {
442+
if ($callable($needle, $el)) {
443+
return true;
444+
}
445+
}
446+
447+
return false;
448+
}
449+
431450
/**
432451
* Search for a given element and return the index of its first occurrence.
433452
*

tests/AbstractArrayTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,25 @@ public function testEach(array $array)
196196
$this->assertSame($each, $arrayzy->each());
197197
}
198198

199+
/**
200+
* @dataProvider simpleArrayProvider
201+
*
202+
* @param array $array
203+
*/
204+
public function testExists(array $array)
205+
{
206+
$key = 2;
207+
208+
$closure = function($needle, $element){
209+
return $needle === $element;
210+
};
211+
212+
$arrayzy = $this->createArrayzy($array);
213+
$isContains = in_array($key, $array);
214+
215+
$this->assertSame($isContains, $arrayzy->exists($closure, $key));
216+
}
217+
199218
/**
200219
* @dataProvider simpleArrayProvider
201220
*

tests/ImmutableArrayTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -850,4 +850,4 @@ public function testWalkRecursively(array $array)
850850

851851
$this->assertImmutable($arrayzy, $resultArrayzy, $array, $resultArray);
852852
}
853-
}
853+
}

tests/MutableArrayTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -865,4 +865,4 @@ public function testWalkRecursively(array $array)
865865

866866
$this->assertMutable($arrayzy, $resultArrayzy, $resultArray);
867867
}
868-
}
868+
}

0 commit comments

Comments
 (0)