Completed
Push — master ( af7add...6e606c )
by Karsten
02:11
created

ArrayUtil::isAccessible()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 3
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 8
    public static function isAssoc($arr)
96
    {
97 8
        if (! \is_array($arr)) {
98 2
            return false;
99
        }
100
101 6
        return array_keys($arr) !== range(0, \count($arr) - 1);
102
    }
103
104
    /**
105
     * Checks if the given subject can be used as an array, like $subject['someKey'] or $subject[123]
106
     *
107
     * @param mixed $subject
108
     *
109
     * @return bool
110
     */
111 11
    public static function isAccessible($subject)
112
    {
113 11
        return $subject !== null &&
114 11
               ($subject instanceof \ArrayAccess || \is_array($subject));
115
    }
116
117
    /**
118
     * Removes all null values recursively
119
     *
120
     * Empty arrays will be kept.
121
     *
122
     * @see ArrayUtilTest::testClean()
123
     *
124
     * @param mixed $input
125
     *
126
     * @return mixed
127
     */
128 7
    public static function clean($input)
129
    {
130 7
        if (\is_array($input)) {
131 4
            return self::cleanRecurse($input);
132
        }
133
134 6
        if (\is_object($input)) {
135 2
            return (object) self::cleanRecurse((array) $input);
136
        }
137
138 5
        return $input;
139
    }
140
141 6
    private static function cleanRecurse(array $in)
142
    {
143 6
        $out = [];
144
145
        /** @var array $input */
146 6
        foreach ($in as $k => $v) {
147
148 4
            $cleaned = self::clean($v);
149
150 4
            if ($cleaned !== null) {
151 4
                $out[$k] = $cleaned;
152
            }
153
        }
154
155 6
        return $out;
156
    }
157
}
158