Completed
Pull Request — master (#38)
by Romain
02:49
created

UtilityTrait::arrayFilter()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 5
nc 3
nop 2
crap 3
1
<?php
2
namespace Kerox\Messenger\Helper;
3
4
trait UtilityTrait
5
{
6
7
    /**
8
     * Returns the input lower_case_delimited_string as a CamelCasedString.
9
     *
10
     * @param string $string String to be camelize
11
     * @param string $delimiter The delimiter in the input string
12
     * @return string
13
     */
14
    public function camelize(string $string, string $delimiter = '_'): string
15
    {
16
        return implode('', array_map('ucfirst', array_map('strtolower', explode($delimiter, $string))));
17
    }
18
19
    /**
20
     * Enhanced version of array_filter which allow to filter recursively
21
     *
22
     * @param array $array
23
     * @param callable|array $callback
24
     * @return array
25
     */
26 1
    public function arrayFilter(array $array, $callback = ['self', 'filter']): array
27
    {
28 1
        foreach ($array as $k => $v) {
29 1
            if (is_array($v)) {
30 1
                $array[$k] = $this->arrayFilter($v, $callback);
31
            }
32
        }
33
34 1
        return array_filter($array, $callback);
35
    }
36
37
    /**
38
     * Callback function for filtering.
39
     *
40
     * @param mixed $var Array to filter.
41
     * @return bool
42
     */
43 1
    protected static function filter($var)
44
    {
45 1
        return $var === 0 || $var === 0.0 || $var === '0' || !empty($var);
46
    }
47
}
48