1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace GeminiLabs\SiteReviews\HelperTraits; |
4
|
|
|
|
5
|
|
|
trait Arr |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* @return bool |
9
|
|
|
*/ |
10
|
1 |
|
public 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 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 function convertDotNotationArray(array $array) |
32
|
|
|
{ |
33
|
8 |
|
$results = []; |
34
|
8 |
|
foreach ($array as $path => $value) { |
35
|
8 |
|
$results = $this->dataSet($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 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
|
|
|
* Get a value from an array of values using a dot-notation path as reference. |
55
|
|
|
* @param array $data |
56
|
|
|
* @param string $path |
57
|
|
|
* @param mixed $fallback |
58
|
|
|
* @return mixed |
59
|
|
|
*/ |
60
|
8 |
|
public function dataGet($data, $path = '', $fallback = '') |
61
|
|
|
{ |
62
|
8 |
|
$data = $this->consolidateArray($data); |
63
|
8 |
|
$keys = explode('.', $path); |
64
|
8 |
|
foreach ($keys as $key) { |
65
|
8 |
|
if (!isset($data[$key])) { |
66
|
8 |
|
return $fallback; |
67
|
|
|
} |
68
|
8 |
|
$data = $data[$key]; |
69
|
|
|
} |
70
|
8 |
|
return $data; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Set a value to an array of values using a dot-notation path as reference. |
75
|
|
|
* @param string $path |
76
|
|
|
* @param mixed $value |
77
|
|
|
* @return array |
78
|
|
|
*/ |
79
|
9 |
|
public function dataSet(array $data, $path, $value) |
80
|
|
|
{ |
81
|
9 |
|
$token = strtok($path, '.'); |
82
|
9 |
|
$ref = &$data; |
83
|
9 |
|
while (false !== $token) { |
84
|
9 |
|
$ref = $this->consolidateArray($ref); |
85
|
9 |
|
$ref = &$ref[$token]; |
86
|
9 |
|
$token = strtok('.'); |
87
|
|
|
} |
88
|
9 |
|
$ref = $value; |
89
|
9 |
|
return $data; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param bool $flattenValue |
94
|
|
|
* @param string $prefix |
95
|
|
|
* @return array |
96
|
|
|
*/ |
97
|
7 |
|
public function flattenArray(array $array, $flattenValue = false, $prefix = '') |
98
|
|
|
{ |
99
|
7 |
|
$result = []; |
100
|
7 |
|
foreach ($array as $key => $value) { |
101
|
7 |
|
$newKey = ltrim($prefix.'.'.$key, '.'); |
102
|
7 |
|
if ($this->isIndexedFlatArray($value)) { |
103
|
7 |
|
if ($flattenValue) { |
104
|
7 |
|
$value = '['.implode(', ', $value).']'; |
105
|
|
|
} |
106
|
7 |
|
} elseif (is_array($value)) { |
107
|
7 |
|
$result = array_merge($result, $this->flattenArray($value, $flattenValue, $newKey)); |
108
|
7 |
|
continue; |
109
|
|
|
} |
110
|
7 |
|
$result[$newKey] = $value; |
111
|
|
|
} |
112
|
7 |
|
return $result; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @param string $key |
117
|
|
|
* @param string $position |
118
|
|
|
* @return array |
119
|
|
|
*/ |
120
|
|
|
public function insertInArray(array $array, array $insert, $key, $position = 'before') |
121
|
|
|
{ |
122
|
|
|
$keyPosition = intval(array_search($key, array_keys($array))); |
123
|
|
|
if ('after' == $position) { |
124
|
|
|
++$keyPosition; |
125
|
|
|
} |
126
|
|
|
if (false !== $keyPosition) { |
127
|
|
|
$result = array_slice($array, 0, $keyPosition); |
128
|
|
|
$result = array_merge($result, $insert); |
129
|
|
|
return array_merge($result, array_slice($array, $keyPosition)); |
130
|
|
|
} |
131
|
|
|
return array_merge($array, $insert); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @param mixed $array |
136
|
|
|
* @return bool |
137
|
|
|
*/ |
138
|
8 |
|
public function isIndexedFlatArray($array) |
139
|
|
|
{ |
140
|
8 |
|
if (!is_array($array) || array_filter($array, 'is_array')) { |
141
|
8 |
|
return false; |
142
|
|
|
} |
143
|
8 |
|
return wp_is_numeric_array($array); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @param bool $prefixed |
148
|
|
|
* @return array |
149
|
|
|
*/ |
150
|
1 |
|
public function prefixArrayKeys(array $values, $prefixed = true) |
151
|
|
|
{ |
152
|
1 |
|
$trim = '_'; |
153
|
1 |
|
$prefix = $prefixed |
154
|
1 |
|
? $trim |
155
|
1 |
|
: ''; |
156
|
1 |
|
$prefixed = []; |
157
|
1 |
|
foreach ($values as $key => $value) { |
158
|
1 |
|
$key = trim($key); |
159
|
1 |
|
if (0 === strpos($key, $trim)) { |
160
|
1 |
|
$key = substr($key, strlen($trim)); |
161
|
|
|
} |
162
|
1 |
|
$prefixed[$prefix.$key] = $value; |
163
|
|
|
} |
164
|
1 |
|
return $prefixed; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* @return array |
169
|
|
|
*/ |
170
|
8 |
|
public function removeEmptyArrayValues(array $array) |
171
|
|
|
{ |
172
|
8 |
|
$result = []; |
173
|
8 |
|
foreach ($array as $key => $value) { |
174
|
8 |
|
if (!$value) { |
175
|
8 |
|
continue; |
176
|
|
|
} |
177
|
8 |
|
$result[$key] = is_array($value) |
178
|
7 |
|
? $this->removeEmptyArrayValues($value) |
179
|
8 |
|
: $value; |
180
|
|
|
} |
181
|
8 |
|
return $result; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* @return array |
186
|
|
|
*/ |
187
|
1 |
|
public function unprefixArrayKeys(array $values) |
188
|
|
|
{ |
189
|
1 |
|
return $this->prefixArrayKeys($values, false); |
190
|
|
|
} |
191
|
|
|
} |
192
|
|
|
|