Passed
Push — master ( ccb079...7906b4 )
by Paul
04:39
created

Arr::convertFromString()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.032

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 2
dl 0
loc 6
ccs 4
cts 5
cp 0.8
crap 2.032
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Helpers;
4
5
class Arr
6
{
7
    /**
8
     * @return bool
9
     */
10 1
    public static function compare(array $arr1, array $arr2)
11
    {
12 1
        sort($arr1);
13 1
        sort($arr2);
14 1
        return $arr1 == $arr2;
15
    }
16
17
    /**
18
     * @param mixed $array
19
     * @return array
20
     */
21 10
    public static function consolidate($array)
22
    {
23 10
        return is_array($array) || is_object($array)
24 10
            ? (array) $array
25 10
            : [];
26
    }
27
28
    /**
29
     * @return array
30
     */
31 8
    public static function convertFromDotNotation(array $array)
32
    {
33 8
        $results = [];
34 8
        foreach ($array as $path => $value) {
35 8
            $results = static::set($results, $path, $value);
36
        }
37 8
        return $results;
38
    }
39
40
    /**
41
     * @param string $string
42
     * @param mixed $callback
43
     * @return array
44
     */
45 1
    public static function convertFromString($string, $callback = null)
46
    {
47 1
        $array = array_map('trim', explode(',', $string));
48 1
        return $callback
49
            ? array_filter($array, $callback)
50 1
            : array_filter($array);
51
    }
52
53
    /**
54
     * @param bool $flattenValue
55
     * @param string $prefix
56
     * @return array
57
     */
58 2
    public static function flatten(array $array, $flattenValue = false, $prefix = '')
59
    {
60 2
        $result = [];
61 2
        foreach ($array as $key => $value) {
62 1
            $newKey = ltrim($prefix.'.'.$key, '.');
63 1
            if (static::isIndexedAndFlat($value)) {
64 1
                if ($flattenValue) {
65 1
                    $value = '['.implode(', ', $value).']';
66
                }
67 1
            } elseif (is_array($value)) {
68 1
                $result = array_merge($result, static::flatten($value, $flattenValue, $newKey));
69 1
                continue;
70
            }
71 1
            $result[$newKey] = $value;
72
        }
73 2
        return $result;
74
    }
75
76
    /**
77
     * Get a value from an array of values using a dot-notation path as reference.
78
     * @param mixed $data
79
     * @param string $path
80
     * @param mixed $fallback
81
     * @return mixed
82
     */
83 8
    public static function get($data, $path = '', $fallback = '')
84
    {
85 8
        $data = static::consolidate($data);
86 8
        $keys = explode('.', $path);
87 8
        foreach ($keys as $key) {
88 8
            if (!isset($data[$key])) {
89 4
                return $fallback;
90
            }
91 8
            $data = $data[$key];
92
        }
93 8
        return $data;
94
    }
95
96
    /**
97
     * @param string $key
98
     * @return array
99
     */
100
    public static function insertAfter($key, array $array, array $insert)
101
    {
102
        return static::insert($array, $insert, $key, 'after');
103
    }
104
105
    /**
106
     * @param string $key
107
     * @return array
108
     */
109
    public static function insertBefore($key, array $array, array $insert)
110
    {
111
        return static::insert($array, $insert, $key, 'before');
112
    }
113
114
    /**
115
     * @param string $key
116
     * @param string $position
117
     * @return array
118
     */
119
    public static function insert(array $array, array $insert, $key, $position = 'before')
120
    {
121
        $keyPosition = intval(array_search($key, array_keys($array)));
122
        if ('after' == $position) {
123
            ++$keyPosition;
124
        }
125
        if (false !== $keyPosition) {
126
            $result = array_slice($array, 0, $keyPosition);
127
            $result = array_merge($result, $insert);
128
            return array_merge($result, array_slice($array, $keyPosition));
129
        }
130
        return array_merge($array, $insert);
131
    }
132
133
    /**
134
     * @param mixed $array
135
     * @return bool
136
     */
137 2
    public static function isIndexedAndFlat($array)
138
    {
139 2
        if (!is_array($array) || array_filter($array, 'is_array')) {
140 2
            return false;
141
        }
142 2
        return wp_is_numeric_array($array);
143
    }
144
145
    /**
146
     * @param bool $prefixed
147
     * @return array
148
     */
149 1
    public static function prefixKeys(array $values, $prefixed = true)
150
    {
151 1
        $trim = '_';
152 1
        $prefix = $prefixed
153 1
            ? $trim
154 1
            : '';
155 1
        $prefixed = [];
156 1
        foreach ($values as $key => $value) {
157 1
            $key = trim($key);
158 1
            if (0 === strpos($key, $trim)) {
159 1
                $key = substr($key, strlen($trim));
160
            }
161 1
            $prefixed[$prefix.$key] = $value;
162
        }
163 1
        return $prefixed;
164
    }
165
166
    /**
167
     * @return array
168
     */
169 2
    public static function removeEmptyValues(array $array)
170
    {
171 2
        $result = [];
172 2
        foreach ($array as $key => $value) {
173 2
            if (!$value) {
174 2
                continue;
175
            }
176 2
            $result[$key] = is_array($value)
177 2
                ? static::removeEmptyValues($value)
178 2
                : $value;
179
        }
180 2
        return $result;
181
    }
182
183
184
    /**
185
     * Set a value to an array of values using a dot-notation path as reference.
186
     * @param string $path
187
     * @param mixed $value
188
     * @return array
189
     */
190 9
    public static function set(array $data, $path, $value)
191
    {
192 9
        $token = strtok($path, '.');
193 9
        $ref = &$data;
194 9
        while (false !== $token) {
195 9
            $ref = static::consolidate($ref);
196 9
            $ref = &$ref[$token];
197 9
            $token = strtok('.');
198
        }
199 9
        $ref = $value;
200 9
        return $data;
201
    }
202
203
    /**
204
     * @return array
205
     */
206
    public static function unique(array $values)
207
    {
208
        return array_filter(array_unique($values));
209
    }
210
211
    /**
212
     * @return array
213
     */
214 1
    public static function unprefixKeys(array $values)
215
    {
216 1
        return static::prefixKeys($values, false);
217
    }
218
}
219