Skip to content

Commit 3145bd9

Browse files
committed
Added wrapper around array_filter
1 parent 0152ed4 commit 3145bd9

File tree

3 files changed

+62
-7
lines changed

3 files changed

+62
-7
lines changed

src/Arr.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -671,6 +671,28 @@ public static function filterByKeys(array $array, $keys, bool $exclude = false):
671671
return $exclude ? array_diff_key($array, array_flip($keysArray)) : array_intersect_key($array, array_flip($keysArray));
672672
}
673673

674+
/**
675+
* Wrapper around PHP built-in <b>array_filter</b> method.<br/>
676+
* <br/>
677+
* Iterates over each value in the array passing them to the <b>callback</b> function. If the <b>callback</b> function returns true, the current value from <b>array</b> is returned into the result array. Array keys are preserved.
678+
*
679+
* @param array $array The array to iterate over
680+
* @param callable|null $callback <p>[optional]</p>
681+
* <p>The callback function to use</p>
682+
* <p>If no callback is supplied, all entries of input equal to false (see converting to boolean) will be removed.</p>
683+
* @param int $flag <p>[optional]</p>
684+
* <p>Flag determining what arguments are sent to callback:</p>
685+
* <ul>
686+
* <li><b>ARRAY_FILTER_USE_KEY</b> - pass key as the only argument to callback instead of the value</li>
687+
* <li><b>ARRAY_FILTER_USE_BOTH</b> - pass both value and key as arguments to callback instead of the value</li>
688+
* @return array
689+
* @see array_filter()
690+
*/
691+
public static function filter(array $array, ?callable $callback = null, int $flag = 0): array
692+
{
693+
return is_null($callback) ? array_filter($array) : array_filter($array, $callback, $flag);
694+
}
695+
674696
/**
675697
* Filter objects array using return value of specified method.<br>
676698
* <br>

src/ArrObj.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@
6868
*
6969
* ---------------------------------------------------------------------------------
7070
*
71+
* @method ArrObj filter(?callable $callback = null, int $flag = 0)
72+
* @see Arr::filter()
73+
*
7174
* @method ArrObj filterByKeys(mixed $keys, bool $exclude = false)
7275
* @see Arr::filterByKeys()
7376
*
@@ -174,6 +177,7 @@ class ArrObj implements IteratorAggregate, ArrayAccess, Countable
174177
'map',
175178
'mapObjects',
176179
'each',
180+
'filter',
177181
'filterByKeys',
178182
'filterObjects',
179183
'group',
@@ -206,6 +210,7 @@ class ArrObj implements IteratorAggregate, ArrayAccess, Countable
206210
'map',
207211
'mapObjects',
208212
'each',
213+
'filter',
209214
'filterByKeys',
210215
'filterObjects',
211216
'group',

test/Arr/ArrTest.php

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ public function testSetNestedElement($class)
149149

150150
$this->assertSame([[['test']]], $this->callMethod([$class, 'set'], [], '[].[].[]', 'test'));
151151
$this->assertSame([[[[]]]], $this->callMethod([$class, 'set'], [], '[].[].[]', []));
152-
$this->assertSame([[[[]]]], $this->callMethod([$class, 'set'], [], ['[]','[]','[]'], []));
152+
$this->assertSame([[[[]]]], $this->callMethod([$class, 'set'], [], ['[]', '[]', '[]'], []));
153153
$this->assertSame([], $this->callMethod([$class, 'set'], [], [], 'test'));
154154
$this->assertSame([], $this->callMethod([$class, 'set'], [], [null], 'test'));
155155

@@ -661,8 +661,7 @@ public function testMapObjects($class)
661661
$this->assertSame(['test', 2, 4, '2'], $this->callMethod([$class, 'mapObjects'], $array, 'test', 2));
662662
$this->assertSame([], $this->callMethod([$class, 'mapObjects'], [], 'test'));
663663

664-
$object = new class()
665-
{
664+
$object = new class() {
666665
function test($arg = 0)
667666
{
668667
return 1 + $arg;
@@ -744,6 +743,35 @@ public function testFilterByKeys($class)
744743
$this->assertSame($array, $this->callMethod([$class, 'filterByKeys'], $array, [null, 0], true));
745744
}
746745

746+
/**
747+
* @param $class string|ArrObj
748+
*
749+
* @dataProvider arrayClassProvider
750+
*/
751+
public function testFilter($class)
752+
{
753+
$callback1 = function ($value) {
754+
return boolval($value);
755+
};
756+
$callback2 = function ($value) {
757+
return !$value;
758+
};
759+
$callback3 = function ($value, $key) {
760+
return $key == $value;
761+
};
762+
$callback4 = function ($key) {
763+
return boolval($key);
764+
};
765+
766+
foreach ($this->arrayProvider() as [$array]) {
767+
$this->assertSame(array_filter($array), $this->callMethod([$class, 'filter'], $array));
768+
$this->assertSame(array_filter($array, $callback1), $this->callMethod([$class, 'filter'], $array, $callback1));
769+
$this->assertSame(array_filter($array, $callback2), $this->callMethod([$class, 'filter'], $array, $callback2));
770+
$this->assertSame(array_filter($array, $callback3, ARRAY_FILTER_USE_BOTH), $this->callMethod([$class, 'filter'], $array, $callback3, ARRAY_FILTER_USE_BOTH));
771+
$this->assertSame(array_filter($array, $callback4, ARRAY_FILTER_USE_KEY), $this->callMethod([$class, 'filter'], $array, $callback4, ARRAY_FILTER_USE_KEY));
772+
}
773+
}
774+
747775
/**
748776
* @param $class string|ArrObj
749777
*
@@ -1019,7 +1047,8 @@ public function testSortObjects($class)
10191047
$object1 = new class() {
10201048
public $i = 1;
10211049

1022-
public function getValue(bool $reverse = false) {
1050+
public function getValue(bool $reverse = false)
1051+
{
10231052
return $reverse ? 1 / $this->i : $this->i;
10241053
}
10251054
};
@@ -1039,7 +1068,7 @@ public function getValue(bool $reverse = false) {
10391068
$array = $proto;
10401069

10411070
$this->assertSame([1, 2, 3, 4, 5], $this->callMethod([$class, 'mapObjects'], $array, 'getValue'));
1042-
$this->assertSame([1, 1/2, 1/3, 1/4, 1/5], $this->callMethod([$class, 'mapObjects'], $array, 'getValue', true));
1071+
$this->assertSame([1, 1 / 2, 1 / 3, 1 / 4, 1 / 5], $this->callMethod([$class, 'mapObjects'], $array, 'getValue', true));
10431072

10441073
// Ensure order is not the same
10451074
do {
@@ -1465,8 +1494,7 @@ public function testClone($class)
14651494
{
14661495
$object = new stdClass();
14671496
$object2 = new ArrayObject();
1468-
$object3 = new class()
1469-
{
1497+
$object3 = new class() {
14701498
public $counter = 1;
14711499

14721500
function __clone()

0 commit comments

Comments
 (0)