Completed
Push — master ( cac582...74a788 )
by Karsten
02:10
created

ArrayUtil::clean()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 3
nop 1
crap 3
1
<?php
2
3
namespace PeekAndPoke\Component\Toolbox;
4
5
use PeekAndPoke\Component\Toolbox\Unit\ArrayUtilTest;
6
7
/**
8
 * @author Karsten J. Gerber <[email protected]>
9
 */
10
class ArrayUtil
11
{
12
    /**
13
     * @param array  $array
14
     * @param string $key
15
     * @param mixed  $default
16
     *
17
     * @return mixed
18
     */
19 13
    public static function get($array, $key, $default = null)
20
    {
21 13
        $array = self::ensureArray($array);
22
23 13
        if (array_key_exists($key, $array)) {
24 5
            return $array[$key];
25
        }
26
27 8
        return $default;
28
    }
29
30
    /**
31
     * @param array  $array
32
     * @param string $path e.g. "root.sub.item"
33
     * @param mixed  $default
34
     * @param string $separator
35
     *
36
     * @return mixed
37
     */
38 12
    public static function getNested($array, $path, $default = null, $separator = '.')
39
    {
40 12
        if (empty($array) ||
41 10
            empty($path) ||
42 12
            empty($separator)) {
43
44 4
            return $default;
45
        }
46
47 8
        $parts = explode($separator, (string) $path);
48
49 8
        foreach ($parts as $part) {
50 8
            if (! isset($array[$part])) {
51 4
                return $default;
52
            }
53
54 6
            $array = $array[$part];
55
        }
56
57 4
        return $array;
58
    }
59
60
    /**
61
     * @param $value
62
     *
63
     * @return array
64
     */
65 38
    public static function ensureArray($value)
66
    {
67 38
        if ($value === null) {
68 3
            return [];
69
        }
70
71 35
        if (\is_array($value) ||
72 35
            $value instanceof \ArrayObject
73
        ) {
74 23
            return (array) $value;
75
        }
76
77 12
        if ($value instanceof \Traversable) {
78 8
            $result = [];
79
80 8
            foreach ($value as $k => $v) {
81 7
                $result[$k] = $v;
82
            }
83
84 8
            return $result;
85
        }
86
87 4
        return [$value];
88
    }
89
90
    /**
91
     * @param $arr
92
     *
93
     * @return bool
94
     */
95
    public static function isAssoc($arr)
96
    {
97
        if (! \is_array($arr)) {
98
            return false;
99
        }
100
101
        return array_keys($arr) !== range(0, \count($arr) - 1);
102
    }
103
104
    /**
105
     * Removes all null values recursively
106
     *
107
     * Empty arrays will be kept.
108
     *
109
     * @see ArrayUtilTest::testClean()
110
     *
111
     * @param mixed $input
112
     *
113
     * @return mixed
114
     */
115 7
    public static function clean($input)
116
    {
117 7
        if (\is_array($input)) {
118 4
            return self::cleanRecurse($input);
119
        }
120
121 6
        if (\is_object($input)) {
122 2
            return (object) self::cleanRecurse((array) $input);
123
        }
124
125 5
        return $input;
126
    }
127
128 6
    private static function cleanRecurse(array $in)
129
    {
130 6
        $out = [];
131
132
        /** @var array $input */
133 6
        foreach ($in as $k => $v) {
134
135 4
            $cleaned = self::clean($v);
136
137 4
            if ($cleaned !== null) {
138 4
                $out[$k] = $cleaned;
139
            }
140
        }
141
142 6
        return $out;
143
    }
144
}
145