ArrayHelper::merge()   C
last analyzed

Complexity

Conditions 8
Paths 6

Size

Total Lines 23
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 72

Importance

Changes 0
Metric Value
dl 0
loc 23
ccs 0
cts 22
cp 0
rs 6.1403
c 0
b 0
f 0
cc 8
eloc 16
nc 6
nop 2
crap 72
1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://github.com/flipbox/skeleton/blob/master/LICENSE
6
 * @link       https://github.com/flipbox/skeleton
7
 */
8
9
namespace Flipbox\Skeleton\Helpers;
10
11
/**
12
 * @author Flipbox Factory <[email protected]>
13
 * @since 2.0.0
14
 */
15
class ArrayHelper
16
{
17
    /**
18
     * Builds a map (key => value) from a multidimensional array or an array of objects.
19
     *
20
     * @param $array
21
     * @param string|\Closure $from
22
     * @param string|\Closure $to
23
     * @param string|\Closure|null $group
24
     * @return array
25
     */
26
    public static function map($array, $from, $to, $group = null)
27
    {
28
        $result = [];
29
        foreach ($array as $element) {
30
            $key = static::getValue($element, $from);
31
            $value = static::getValue($element, $to);
32
            if ($group !== null) {
33
                $result[static::getValue($element, $group)][$key] = $value;
34
            } else {
35
                $result[$key] = $value;
36
            }
37
        }
38
39
        return $result;
40
    }
41
42
    /**
43
     * Merges two or more arrays into one recursively.
44
     *
45
     * @param $a
46
     * @param $b
47
     * @return array
48
     */
49
    public static function merge($a, $b)
0 ignored issues
show
Unused Code introduced by
The parameter $a is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $b is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
50
    {
51
        $args = func_get_args();
52
        $res = array_shift($args);
53
        while (!empty($args)) {
54
            $next = array_shift($args);
55
            foreach ($next as $k => $v) {
56
                if (is_int($k)) {
57
                    if (isset($res[$k])) {
58
                        $res[] = $v;
59
                    } else {
60
                        $res[$k] = $v;
61
                    }
62
                } elseif (is_array($v) && isset($res[$k]) && is_array($res[$k])) {
63
                    $res[$k] = self::merge($res[$k], $v);
64
                } else {
65
                    $res[$k] = $v;
66
                }
67
            }
68
        }
69
70
        return $res;
71
    }
72
73
    /**
74
     * Checks if the given array contains the specified key.
75
     *
76
     * @param $key
77
     * @param $array
78
     * @param bool $caseSensitive
79
     * @return bool
80
     */
81
    public static function keyExists($key, $array, $caseSensitive = true)
82
    {
83
        if ($caseSensitive) {
84
            return array_key_exists($key, $array);
85
        } else {
86
            foreach (array_keys($array) as $k) {
87
                if (strcasecmp($key, $k) === 0) {
88
                    return true;
89
                }
90
            }
91
92
            return false;
93
        }
94
    }
95
96
    /**
97
     * Indexes an array according to a specified key.
98
     *
99
     * @param $array
100
     * @param $key
101
     * @return array
102
     */
103
    public static function index($array, $key)
104
    {
105
        $result = [];
106
        foreach ($array as $element) {
107
            $value = static::getValue($element, $key);
108
            $result[$value] = $element;
109
        }
110
111
        return $result;
112
    }
113
114
    /**
115
     * Converts an object or an array of objects into an array.
116
     *
117
     * @param $object
118
     * @param array $properties
119
     * @param bool $recursive
120
     * @return array
121
     */
122
    public static function toArray($object, $properties = [], $recursive = true)
123
    {
124
        if (is_array($object)) {
125
            if ($recursive) {
126
                foreach ($object as $key => $value) {
127
                    if (is_array($value) || is_object($value)) {
128
                        $object[$key] = static::toArray($value, $properties, true);
129
                    }
130
                }
131
            }
132
133
            return $object;
134
        } elseif (is_object($object)) {
135
            if (!empty($properties)) {
136
                $className = get_class($object);
137
                if (!empty($properties[$className])) {
138
                    $result = [];
139
                    foreach ($properties[$className] as $key => $name) {
140
                        if (is_int($key)) {
141
                            $result[$name] = $object->$name;
142
                        } else {
143
                            $result[$key] = static::getValue($object, $name);
144
                        }
145
                    }
146
147
                    return $recursive ? static::toArray($result, $properties) : $result;
148
                }
149
            }
150
151
            $result = [];
152
            foreach ($object as $key => $value) {
153
                $result[$key] = $value;
154
            }
155
156
            return $recursive ? static::toArray($result) : $result;
157
        } else {
158
            return [$object];
159
        }
160
    }
161
162
    /**
163
     * Retrieves the value of an array element or object property with the given key or property name.
164
     *
165
     * @param $array
166
     * @param $key
167
     * @param null $default
168
     * @return mixed|null
169
     */
170
    public static function getValue($array, $key, $default = null)
171
    {
172
        if ($key instanceof \Closure) {
173
            return $key($array, $default);
174
        }
175
176
        if (is_array($key)) {
177
            $lastKey = array_pop($key);
178
            foreach ($key as $keyPart) {
179
                $array = static::getValue($array, $keyPart);
180
            }
181
            $key = $lastKey;
182
        }
183
184
        if (is_array($array) && array_key_exists($key, $array)) {
185
            return $array[$key];
186
        }
187
188
        if (($pos = strrpos($key, '.')) !== false) {
189
            $array = static::getValue($array, substr($key, 0, $pos), $default);
190
            $key = substr($key, $pos + 1);
191
        }
192
193
        if (is_object($array)) {
194
            return $array->$key;
195
        } elseif (is_array($array)) {
196
            return array_key_exists($key, $array) ? $array[$key] : $default;
197
        } else {
198
            return $default;
199
        }
200
    }
201
202
    /**
203
     * Removes an item from an array and returns the value.
204
     *
205
     * @param $array
206
     * @param $key
207
     * @param null $default
208
     * @return mixed|null
209
     */
210
    public static function remove(&$array, $key, $default = null)
211
    {
212
        if (is_array($array) && (isset($array[$key]) || array_key_exists($key, $array))) {
213
            $value = $array[$key];
214
            unset($array[$key]);
215
216
            return $value;
217
        }
218
219
        return $default;
220
    }
221
222
    /**
223
     * Returns the first value in a given array.
224
     *
225
     * @param $arr
226
     * @return mixed
227
     */
228
    public static function getFirstValue($arr)
229
    {
230
        if (count($arr)) {
231
            if (isset($arr[0])) {
232
                return $arr[0];
233
            } else {
234
                $keys = array_keys($arr);
235
236
                return $arr[$keys[0]];
237
            }
238
        }
239
        return null;
240
    }
241
242
243
    /**
244
     * Finds matching values from two arrays
245
     *
246
     * @param array $keys
247
     * @param array $array
248
     * @return array
249
     */
250
    public static function matches($keys = [], $array = []): array
251
    {
252
        if (!empty($keys) && !empty($array)) {
253
            $matchingProperties = [];
254
255
            foreach ($keys as $property) {
256
                if ($matchedAttribute = static::getValue($array, $property)) {
257
                    $matchingProperties[$property] = $matchedAttribute;
258
                }
259
            }
260
261
            return $matchingProperties;
262
        }
263
264
        return $array;
265
    }
266
267
    /**
268
     * @param array $array
269
     * @param $findKey
270
     * @param $newKey
271
     * @param $newValue
272
     * @return array|bool
273
     */
274
    public static function insertBefore(array $array, $findKey, $newKey, $newValue)
275
    {
276
        if (array_key_exists($findKey, $array)) {
277
            $new = array();
278
            foreach ($array as $k => $value) {
279
                if ($k === $findKey) {
280
                    $new[$newKey] = $newValue;
281
                }
282
                $new[$k] = $value;
283
            }
284
            return $new;
285
        }
286
        return false;
287
    }
288
289
    /**
290
     * @param array $array
291
     * @param $findKey
292
     * @param $newKey
293
     * @param $newValue
294
     * @return array|bool
295
     */
296
    public static function insertAfter(array $array, $findKey, $newKey, $newValue)
297
    {
298
        if (array_key_exists($findKey, $array)) {
299
            $new = array();
300
            foreach ($array as $k => $value) {
301
                $new[$k] = $value;
302
                if ($k === $findKey) {
303
                    $new[$newKey] = $newValue;
304
                }
305
            }
306
            return $new;
307
        }
308
        return false;
309
    }
310
}
311