1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ubiquity\utils\base; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Array utilities. |
7
|
|
|
* Ubiquity\utils\base$UArray |
8
|
|
|
* This class is part of Ubiquity |
9
|
|
|
* |
10
|
|
|
* @author jcheron <[email protected]> |
11
|
|
|
* @version 1.0.3 |
12
|
|
|
* |
13
|
|
|
*/ |
14
|
|
|
class UArray { |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Tests if array is associative |
18
|
|
|
* |
19
|
|
|
* @param array $array |
20
|
|
|
* @return boolean |
21
|
|
|
*/ |
22
|
1 |
|
public static function isAssociative($array) { |
23
|
1 |
|
return (\array_keys ( $array ) !== \range ( 0, \count ( $array ) - 1 )); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Returns a new array with the keys $keys |
28
|
|
|
* |
29
|
|
|
* @param array $array an associative array |
30
|
|
|
* @param array $keys some keys |
31
|
|
|
* @return array |
32
|
|
|
*/ |
33
|
1 |
|
public static function extractKeys($array, $keys) { |
34
|
1 |
|
$result = [ ]; |
35
|
1 |
|
foreach ( $keys as $key ) { |
36
|
1 |
|
if (isset ( $array [$key] )) { |
37
|
1 |
|
$result [$key] = $array [$key]; |
38
|
|
|
} |
39
|
|
|
} |
40
|
1 |
|
return $result; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* |
45
|
|
|
* @param array $array |
46
|
|
|
* @param string $key |
47
|
|
|
* @param int $pos |
48
|
|
|
* @return mixed|null |
49
|
|
|
*/ |
50
|
1 |
|
public static function getValue($array, $key, $pos) { |
51
|
1 |
|
if (\array_key_exists ( $key, $array )) { |
52
|
1 |
|
return $array [$key]; |
53
|
|
|
} |
54
|
1 |
|
$values = array_values ( $array ); |
55
|
1 |
|
if ($pos < \sizeof ( $values )) |
56
|
1 |
|
return $values [$pos]; |
57
|
1 |
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* |
61
|
|
|
* @param array $array |
62
|
|
|
* @param string|int $key |
63
|
|
|
* @param mixed $default |
64
|
|
|
* @return mixed |
65
|
|
|
*/ |
66
|
18 |
|
public static function getRecursive($array, $key, $default = null) { |
67
|
18 |
|
if (\array_key_exists ( $key, $array )) { |
68
|
18 |
|
return $array [$key]; |
69
|
|
|
} else { |
70
|
18 |
|
foreach ( $array as $item ) { |
71
|
18 |
|
if (is_array ( $item )) { |
72
|
18 |
|
return self::getRecursive ( $item, $key, $default ); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
} |
76
|
1 |
|
return $default; |
77
|
|
|
} |
78
|
|
|
|
79
|
1 |
|
public static function getDefaultValue($array, $key, $default) { |
80
|
1 |
|
return $array [$key] ?? $default; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Save a php array to the disk. |
85
|
|
|
* |
86
|
|
|
* @param array $array The array to save |
87
|
|
|
* @param string $filename The path of the file to save in |
88
|
|
|
* @return int |
89
|
|
|
*/ |
90
|
|
|
public static function save($array, $filename) { |
91
|
|
|
$content = "<?php\nreturn " . self::asPhpArray ( $array, "array", 1, true ) . ";"; |
92
|
|
|
return UFileSystem::save ( $filename, $content ); |
93
|
|
|
} |
94
|
|
|
|
95
|
39 |
|
public static function asPhpArray($array, $prefix = "", $depth = 1, $format = false) { |
96
|
39 |
|
$exts = array (); |
97
|
39 |
|
$extsStr = ""; |
98
|
39 |
|
$tab = ""; |
99
|
39 |
|
$nl = ""; |
100
|
39 |
|
if ($format) { |
101
|
5 |
|
$tab = \str_repeat ( "\t", $depth ); |
102
|
5 |
|
$nl = PHP_EOL; |
103
|
|
|
} |
104
|
39 |
|
foreach ( $array as $k => $v ) { |
105
|
37 |
|
if (is_string ( $k )) { |
106
|
37 |
|
$exts [] = "\"" . UString::doubleBackSlashes ( $k ) . "\"=>" . self::parseValue ( $v, 'array', $depth + 1, $format ); |
107
|
|
|
} else { |
108
|
37 |
|
$exts [] = self::parseValue ( $v, $prefix, $depth + 1, $format ); |
109
|
|
|
} |
110
|
|
|
} |
111
|
39 |
|
if (\sizeof ( $exts ) > 0 || $prefix !== "") { |
112
|
39 |
|
$extsStr = "(" . \implode ( "," . $nl . $tab, $exts ) . ")"; |
113
|
39 |
|
if (\sizeof ( $exts ) > 0) { |
114
|
37 |
|
$extsStr = "(" . $nl . $tab . \implode ( "," . $nl . $tab, $exts ) . $nl . $tab . ")"; |
115
|
|
|
} |
116
|
|
|
} |
117
|
39 |
|
return $prefix . $extsStr; |
118
|
|
|
} |
119
|
|
|
|
120
|
1 |
|
public static function asJSON($array) { |
121
|
1 |
|
return \json_encode ( $array, JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP | JSON_UNESCAPED_UNICODE ); |
122
|
|
|
} |
123
|
|
|
|
124
|
2 |
|
public static function remove($array, $search) { |
125
|
2 |
|
if (\is_array ( $search )) { |
126
|
1 |
|
foreach ( $search as $val ) { |
127
|
1 |
|
$array = self::removeOne ( $array, $val ); |
128
|
|
|
} |
129
|
|
|
} else { |
130
|
2 |
|
$array = self::removeOne ( $array, $search ); |
131
|
|
|
} |
132
|
2 |
|
return \array_values ( $array ); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Removes from array by key |
137
|
|
|
* |
138
|
|
|
* @param array $array |
139
|
|
|
* @param int|string $key |
140
|
|
|
* @return array |
141
|
|
|
*/ |
142
|
1 |
|
public static function removeByKey($array, $key) { |
143
|
1 |
|
if (isset ( $array [$key] )) { |
144
|
1 |
|
unset ( $array [$key] ); |
145
|
|
|
} |
146
|
1 |
|
return $array; |
147
|
|
|
} |
148
|
|
|
|
149
|
17 |
|
public static function removeRecursive(&$array, $key) { |
150
|
17 |
|
foreach ( $array as &$item ) { |
151
|
17 |
|
if (\array_key_exists ( $key, $item )) { |
152
|
17 |
|
unset ( $item [$key] ); |
153
|
17 |
|
} elseif (\is_array ( $item )) { |
154
|
17 |
|
self::removeRecursive ( $item, $key ); |
155
|
|
|
} |
156
|
|
|
} |
157
|
17 |
|
} |
158
|
|
|
|
159
|
|
|
public static function removeByKeys($array, $keys) { |
160
|
|
|
$assocKeys = [ ]; |
161
|
|
|
foreach ( $keys as $key ) { |
162
|
|
|
$assocKeys [$key] = true; |
163
|
|
|
} |
164
|
|
|
return \array_diff_key ( $array, $assocKeys ); |
165
|
|
|
} |
166
|
|
|
|
167
|
3 |
|
public static function removeOne($array, $search) { |
168
|
3 |
|
if (($key = \array_search ( $search, $array )) !== false) { |
169
|
3 |
|
unset ( $array [$key] ); |
170
|
|
|
} |
171
|
3 |
|
return $array; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
public static function update(&$array, $search, $newValue) { |
175
|
|
|
if (($key = \array_search ( $search, $array )) !== false) { |
176
|
|
|
$array [$key] = $newValue; |
177
|
|
|
} |
178
|
|
|
return $array; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* |
183
|
|
|
* @param array $array |
184
|
|
|
* @return boolean |
185
|
|
|
*/ |
186
|
|
|
public static function doubleBackSlashes(&$array) { |
187
|
|
|
return \array_walk ( $array, function (&$value) { |
188
|
|
|
$value = UString::doubleBackSlashes ( $value ); |
189
|
|
|
} ); |
190
|
|
|
} |
191
|
|
|
|
192
|
37 |
|
private static function parseValue($v, $prefix = "", $depth = 1, $format = false) { |
193
|
37 |
|
if (\is_numeric ( $v )) { |
194
|
32 |
|
$result = $v; |
195
|
36 |
|
} elseif ($v !== '' && UString::isBooleanStr ( $v )) { |
196
|
24 |
|
$result = UString::getBooleanStr ( $v ); |
197
|
36 |
|
} elseif (\is_array ( $v )) { |
198
|
34 |
|
$result = self::asPhpArray ( $v, $prefix, $depth + 1, $format ); |
199
|
36 |
|
} elseif (\is_string ( $v ) && (UString::startswith ( trim ( $v ), '$config' ) || UString::startswith ( \trim ( $v ), "function" ) || UString::startswith ( \trim ( $v ), "array(" ))) { |
200
|
9 |
|
$result = $v; |
201
|
36 |
|
} elseif ($v instanceof \Closure) { |
202
|
11 |
|
$result = UIntrospection::closure_dump ( $v ); |
203
|
36 |
|
} elseif ($v instanceof \DateTime) { |
204
|
|
|
$result = "\DateTime::createFromFormat('Y-m-d H:i:s','" . $v->format ( 'Y-m-d H:i:s' ) . "')"; |
205
|
|
|
} else { |
206
|
36 |
|
$result = "\"" . \str_replace ( '$', '\$', $v ) . "\""; |
207
|
36 |
|
$result = UString::doubleBackSlashes ( $result ); |
208
|
|
|
} |
209
|
37 |
|
return $result; |
210
|
|
|
} |
211
|
|
|
|
212
|
2 |
|
public static function iSearch($needle, $haystack, $strict = null) { |
213
|
2 |
|
return \array_search ( strtolower ( $needle ), array_map ( 'strtolower', $haystack ), $strict ); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
public static function iRemove($array, $search) { |
217
|
|
|
if (\is_array ( $search )) { |
218
|
|
|
foreach ( $search as $val ) { |
219
|
|
|
$array = self::iRemoveOne ( $array, $val ); |
220
|
|
|
} |
221
|
|
|
} else { |
222
|
|
|
$array = self::iRemoveOne ( $array, $search ); |
223
|
|
|
} |
224
|
|
|
return \array_values ( $array ); |
225
|
|
|
} |
226
|
|
|
|
227
|
2 |
|
public static function iRemoveOne($array, $search) { |
228
|
2 |
|
if (($key = self::iSearch ( $search, $array )) !== false) { |
229
|
2 |
|
unset ( $array [$key] ); |
230
|
|
|
} |
231
|
2 |
|
return $array; |
232
|
|
|
} |
233
|
|
|
|
234
|
7 |
|
public static function implodeAsso($array, $glue, $op = '=', $quoteKey = '"', $quoteValue = '"') { |
235
|
7 |
|
$res = [ ]; |
236
|
7 |
|
foreach ( $array as $k => $v ) { |
237
|
|
|
$res [] = "{$quoteKey}{$k}{$quoteKey}{$op}{$quoteValue}{$v}{$quoteValue}"; |
238
|
|
|
} |
239
|
7 |
|
return \implode ( $glue, $res ); |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
public static function toJSON($array) { |
243
|
|
|
$result = [ ]; |
244
|
|
|
foreach ( $array as $k => $v ) { |
245
|
|
|
if (\is_array ( $v )) { |
246
|
|
|
$v = self::toJSON ( $v ); |
247
|
|
|
} elseif ($v != null && UString::startswith ( $v, 'js:' )) { |
248
|
|
|
$v = \substr ( $v, 3 ); |
249
|
|
|
} else { |
250
|
|
|
$v = '"' . $v . '"'; |
251
|
|
|
} |
252
|
|
|
$result [] = '"' . $k . '": ' . $v; |
253
|
|
|
} |
254
|
|
|
return '{' . \implode ( ',', $result ) . '}'; |
255
|
|
|
} |
256
|
|
|
} |
257
|
|
|
|