Passed
Push — master ( ffa8d9...6a6a4c )
by Paul
03:54
created

Arr::unique()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
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 compareArrays(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 consolidateArray($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 convertDotNotationArray(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 convertStringToArray($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 7
    public static function flattenArray(array $array, $flattenValue = false, $prefix = '')
59
    {
60 7
        $result = [];
61 7
        foreach ($array as $key => $value) {
62 7
            $newKey = ltrim($prefix.'.'.$key, '.');
63 7
            if (static::isIndexedFlatArray($value)) {
64 7
                if ($flattenValue) {
65 7
                    $value = '['.implode(', ', $value).']';
66
                }
67 7
            } elseif (is_array($value)) {
68 7
                $result = array_merge($result, static::flattenArray($value, $flattenValue, $newKey));
69 7
                continue;
70
            }
71 7
            $result[$newKey] = $value;
72
        }
73 7
        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::consolidateArray($data);
86 8
        $keys = explode('.', $path);
87 8
        foreach ($keys as $key) {
88 8
            if (!isset($data[$key])) {
89 8
                return $fallback;
90
            }
91 8
            $data = $data[$key];
92
        }
93 8
        return $data;
94
    }
95
96
    /**
97
     * @param string $key
98
     * @param string $position
99
     * @return array
100
     */
101
    public static function insertInArray(array $array, array $insert, $key, $position = 'before')
102
    {
103
        $keyPosition = intval(array_search($key, array_keys($array)));
104
        if ('after' == $position) {
105
            ++$keyPosition;
106
        }
107
        if (false !== $keyPosition) {
108
            $result = array_slice($array, 0, $keyPosition);
109
            $result = array_merge($result, $insert);
110
            return array_merge($result, array_slice($array, $keyPosition));
111
        }
112
        return array_merge($array, $insert);
113
    }
114
115
    /**
116
     * @param mixed $array
117
     * @return bool
118
     */
119 8
    public static function isIndexedFlatArray($array)
120
    {
121 8
        if (!is_array($array) || array_filter($array, 'is_array')) {
122 8
            return false;
123
        }
124 8
        return wp_is_numeric_array($array);
125
    }
126
127
    /**
128
     * @param bool $prefixed
129
     * @return array
130
     */
131 1
    public static function prefixArrayKeys(array $values, $prefixed = true)
132
    {
133 1
        $trim = '_';
134 1
        $prefix = $prefixed
135 1
            ? $trim
136 1
            : '';
137 1
        $prefixed = [];
138 1
        foreach ($values as $key => $value) {
139 1
            $key = trim($key);
140 1
            if (0 === strpos($key, $trim)) {
141 1
                $key = substr($key, strlen($trim));
142
            }
143 1
            $prefixed[$prefix.$key] = $value;
144
        }
145 1
        return $prefixed;
146
    }
147
148
    /**
149
     * @return array
150
     */
151 8
    public static function removeEmptyArrayValues(array $array)
152
    {
153 8
        $result = [];
154 8
        foreach ($array as $key => $value) {
155 8
            if (!$value) {
156 8
                continue;
157
            }
158 8
            $result[$key] = is_array($value)
159 8
                ? static::removeEmptyArrayValues($value)
160 8
                : $value;
161
        }
162 8
        return $result;
163
    }
164
165
166
    /**
167
     * Set a value to an array of values using a dot-notation path as reference.
168
     * @param string $path
169
     * @param mixed $value
170
     * @return array
171
     */
172 9
    public static function set(array $data, $path, $value)
173
    {
174 9
        $token = strtok($path, '.');
175 9
        $ref = &$data;
176 9
        while (false !== $token) {
177 9
            $ref = static::consolidateArray($ref);
178 9
            $ref = &$ref[$token];
179 9
            $token = strtok('.');
180
        }
181 9
        $ref = $value;
182 9
        return $data;
183
    }
184
185
    /**
186
     * @return array
187
     */
188
    public static function unique(array $values)
189
    {
190
        return array_filter(array_unique($values));
191
    }
192
193
    /**
194
     * @return array
195
     */
196 1
    public static function unprefixArrayKeys(array $values)
197
    {
198 1
        return static::prefixArrayKeys($values, false);
199
    }
200
}
201