Passed
Push — master ( 195aeb...469104 )
by Romain
36s
created

UtilityTrait::filter()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 4
Code Lines 2

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 9.2
c 0
b 0
f 0
cc 4
eloc 2
nc 4
nop 1
crap 4
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