1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Mapado\RestClientSdk\Helper; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Some array helpers. |
7
|
|
|
* Greatly inspired by Laravel's helper: https://github.com/rappasoft/laravel-helpers |
8
|
|
|
* |
9
|
|
|
* @author Julien Deniau <[email protected]> |
10
|
|
|
*/ |
11
|
|
|
class ArrayHelper |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* Get an item from an array using "dot" notation. |
15
|
|
|
* |
16
|
|
|
* @param array $array |
17
|
|
|
* @param string $key |
18
|
|
|
* @param mixed $default |
19
|
|
|
* @return mixed |
20
|
|
|
*/ |
21
|
|
|
public static function arrayGet($array, $key, $default = null) |
22
|
|
|
{ |
23
|
|
|
if (is_null($key)) { |
24
|
|
|
return $array; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
if (isset($array[$key])) { |
28
|
|
|
return $array[$key]; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
View Code Duplication |
foreach (explode('.', $key) as $segment) { |
|
|
|
|
32
|
|
|
if (!is_array($array) || !array_key_exists($segment, $array)) { |
33
|
|
|
return self::value($default); |
34
|
|
|
} |
35
|
|
|
$array = $array[$segment]; |
36
|
|
|
} |
37
|
|
|
return $array; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Check if an item exists in an array using "dot" notation. |
42
|
|
|
* |
43
|
|
|
* @param array $array |
44
|
|
|
* @param string $key |
45
|
|
|
* @return bool |
46
|
|
|
*/ |
47
|
|
|
public static function arrayHas($array, $key) |
48
|
|
|
{ |
49
|
|
|
if (empty($array) || is_null($key)) { |
50
|
|
|
return false; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
if (array_key_exists($key, $array)) { |
54
|
|
|
return true; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
View Code Duplication |
foreach (explode('.', $key) as $segment) { |
|
|
|
|
58
|
|
|
if (!is_array($array) || !array_key_exists($segment, $array)) { |
59
|
|
|
return false; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
$array = $array[$segment]; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
return true; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Flatten a multi-dimensional associative array with dots. |
70
|
|
|
* |
71
|
|
|
* @param array $array |
72
|
|
|
* @param string $prepend |
73
|
|
|
* @return array |
74
|
|
|
*/ |
75
|
|
|
public static function arrayDot($array, $prepend = '') |
76
|
|
|
{ |
77
|
|
|
$results = []; |
78
|
|
|
foreach ($array as $key => $value) { |
79
|
|
|
if (is_array($value) && !empty($value)) { |
80
|
|
|
$results = array_merge($results, static::arrayDot($value, $prepend . $key . '.')); |
81
|
|
|
} else { |
82
|
|
|
$results[$prepend . $key] = $value; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return $results; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public static function arrayDiffAssocRecursive($array1, $array2) |
90
|
|
|
{ |
91
|
|
|
return array_diff_assoc(static::arrayDot($array1), static::arrayDot($array2)); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public static function arraySame($array1, $array2) |
95
|
|
|
{ |
96
|
|
|
return empty(static::arrayDiffAssocRecursive($array1, $array2)); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Return the default value of the given value. |
102
|
|
|
* |
103
|
|
|
* @param mixed $value |
104
|
|
|
* @return mixed |
105
|
|
|
*/ |
106
|
|
|
public static function value($value) |
107
|
|
|
{ |
108
|
|
|
return $value instanceof Closure ? $value() : $value; |
|
|
|
|
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.