UtilityTrait::filter()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 4
nc 4
nop 1
crap 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Kerox\Messenger\Helper;
6
7
trait UtilityTrait
8
{
9
    /**
10
     * Enhanced version of array_filter which allow to filter recursively.
11
     *
12
     * @param mixed $callback
13
     */
14 26
    public function arrayFilter(array $array, $callback = ['self', 'filter']): array
15
    {
16 26
        foreach ($array as $k => $v) {
17 26
            if (\is_array($v)) {
18 24
                $array[$k] = $this->arrayFilter($v, $callback);
19
            }
20
        }
21
22 26
        return array_filter($array, $callback);
23
    }
24
25
    /**
26
     * Callback function for filtering.
27
     *
28
     * @param mixed $var array to filter
29
     */
30 26
    protected static function filter($var): bool
31
    {
32 26
        return $var === 0 || $var === 0.0 || $var === '0' || !empty($var);
33
    }
34
}
35