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

UtilityTrait   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 77.78%

Importance

Changes 0
Metric Value
wmc 8
lcom 0
cbo 0
dl 0
loc 44
ccs 7
cts 9
cp 0.7778
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A camelize() 0 4 1
A arrayFilter() 0 10 3
A filter() 0 4 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