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.4 |
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 asPhpClass($array, $name, $namespace = '', $format = false) { |
121
|
1 |
|
$tab = ""; |
122
|
|
|
$nl = ""; |
123
|
|
|
if ($format) { |
124
|
2 |
|
$tab = "\t"; |
125
|
2 |
|
$nl = PHP_EOL; |
126
|
1 |
|
} |
127
|
1 |
|
$content = 'public static $value=' . self::asPhpArray ( $array, 'array', 1, true ) . ';'; |
128
|
|
|
if ($namespace != null) { |
129
|
|
|
$namespace = "namespace {$namespace};{$nl}"; |
130
|
2 |
|
} |
131
|
|
|
return "{$namespace}class {$name} {" . $nl . $tab . $content . $nl . $tab . "}"; |
132
|
2 |
|
} |
133
|
|
|
|
134
|
|
|
public static function asJSON($array) { |
135
|
|
|
return \json_encode ( $array, JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP | JSON_UNESCAPED_UNICODE ); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
public static function remove($array, $search) { |
139
|
|
|
if (\is_array ( $search )) { |
140
|
|
|
foreach ( $search as $val ) { |
141
|
|
|
$array = self::removeOne ( $array, $val ); |
142
|
1 |
|
} |
143
|
1 |
|
} else { |
144
|
1 |
|
$array = self::removeOne ( $array, $search ); |
145
|
|
|
} |
146
|
1 |
|
return \array_values ( $array ); |
147
|
|
|
} |
148
|
|
|
|
149
|
17 |
|
/** |
150
|
17 |
|
* Removes from array by key |
151
|
17 |
|
* |
152
|
17 |
|
* @param array $array |
153
|
17 |
|
* @param int|string $key |
154
|
17 |
|
* @return array |
155
|
|
|
*/ |
156
|
|
|
public static function removeByKey($array, $key) { |
157
|
17 |
|
if (isset ( $array [$key] )) { |
158
|
|
|
unset ( $array [$key] ); |
159
|
|
|
} |
160
|
|
|
return $array; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
public static function removeRecursive(&$array, $key) { |
164
|
|
|
foreach ( $array as &$item ) { |
165
|
|
|
if (\array_key_exists ( $key, $item )) { |
166
|
|
|
unset ( $item [$key] ); |
167
|
3 |
|
} elseif (\is_array ( $item )) { |
168
|
3 |
|
self::removeRecursive ( $item, $key ); |
169
|
3 |
|
} |
170
|
|
|
} |
171
|
3 |
|
} |
172
|
|
|
|
173
|
|
|
public static function removeByKeys($array, $keys) { |
174
|
|
|
$assocKeys = [ ]; |
175
|
|
|
foreach ( $keys as $key ) { |
176
|
|
|
$assocKeys [$key] = true; |
177
|
|
|
} |
178
|
|
|
return \array_diff_key ( $array, $assocKeys ); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
public static function removeOne($array, $search) { |
182
|
|
|
if (($key = \array_search ( $search, $array )) !== false) { |
183
|
|
|
unset ( $array [$key] ); |
184
|
|
|
} |
185
|
|
|
return $array; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
public static function update(&$array, $search, $newValue) { |
189
|
|
|
if (($key = \array_search ( $search, $array )) !== false) { |
190
|
|
|
$array [$key] = $newValue; |
191
|
|
|
} |
192
|
37 |
|
return $array; |
193
|
37 |
|
} |
194
|
32 |
|
|
195
|
36 |
|
/** |
196
|
24 |
|
* |
197
|
36 |
|
* @param array $array |
198
|
34 |
|
* @return boolean |
199
|
36 |
|
*/ |
200
|
9 |
|
public static function doubleBackSlashes(&$array) { |
201
|
36 |
|
return \array_walk ( $array, function (&$value) { |
202
|
11 |
|
$value = UString::doubleBackSlashes ( $value ); |
203
|
36 |
|
} ); |
204
|
|
|
} |
205
|
|
|
|
206
|
36 |
|
private static function parseValue($v, $prefix = "", $depth = 1, $format = false) { |
207
|
36 |
|
if (\is_numeric ( $v )) { |
208
|
|
|
$result = $v; |
209
|
37 |
|
} elseif ($v !== '' && UString::isBooleanStr ( $v )) { |
210
|
|
|
$result = UString::getBooleanStr ( $v ); |
211
|
|
|
} elseif (\is_array ( $v )) { |
212
|
2 |
|
$result = self::asPhpArray ( $v, $prefix, $depth + 1, $format ); |
213
|
2 |
|
} elseif (\is_string ( $v ) && (UString::startswith ( trim ( $v ), '$config' ) || UString::startswith ( \trim ( $v ), "function" ) || UString::startswith ( \trim ( $v ), "array(" ))) { |
214
|
|
|
$result = $v; |
215
|
|
|
} elseif ($v instanceof \Closure) { |
216
|
|
|
$result = UIntrospection::closure_dump ( $v ); |
217
|
|
|
} elseif ($v instanceof \DateTime) { |
218
|
|
|
$result = "\DateTime::createFromFormat('Y-m-d H:i:s','" . $v->format ( 'Y-m-d H:i:s' ) . "')"; |
219
|
|
|
} else { |
220
|
|
|
$result = UString::doubleBackSlashes ( $v ); |
221
|
|
|
$result = "\"" . \str_replace ( [ '$','"' ], [ '\$','\"' ], $result ) . "\""; |
222
|
|
|
} |
223
|
|
|
return $result; |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
public static function iSearch($needle, $haystack, $strict = null) { |
227
|
2 |
|
return \array_search ( strtolower ( $needle ), array_map ( 'strtolower', $haystack ), $strict ); |
228
|
2 |
|
} |
229
|
2 |
|
|
230
|
|
|
public static function iRemove($array, $search) { |
231
|
2 |
|
if (\is_array ( $search )) { |
232
|
|
|
foreach ( $search as $val ) { |
233
|
|
|
$array = self::iRemoveOne ( $array, $val ); |
234
|
7 |
|
} |
235
|
7 |
|
} else { |
236
|
7 |
|
$array = self::iRemoveOne ( $array, $search ); |
237
|
|
|
} |
238
|
|
|
return \array_values ( $array ); |
239
|
7 |
|
} |
240
|
|
|
|
241
|
|
|
public static function iRemoveOne($array, $search) { |
242
|
|
|
if (($key = self::iSearch ( $search, $array )) !== false) { |
243
|
|
|
unset ( $array [$key] ); |
244
|
|
|
} |
245
|
|
|
return $array; |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
public static function implodeAsso($array, $glue, $op = '=', $quoteKey = '"', $quoteValue = '"') { |
249
|
|
|
$res = [ ]; |
250
|
|
|
foreach ( $array as $k => $v ) { |
251
|
|
|
$res [] = "{$quoteKey}{$k}{$quoteKey}{$op}{$quoteValue}{$v}{$quoteValue}"; |
252
|
|
|
} |
253
|
|
|
return \implode ( $glue, $res ); |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
public static function toJSON($array) { |
257
|
|
|
$result = [ ]; |
258
|
|
|
foreach ( $array as $k => $v ) { |
259
|
|
|
if (\is_array ( $v )) { |
260
|
|
|
$v = self::toJSON ( $v ); |
261
|
|
|
} elseif ($v != null && UString::startswith ( $v, 'js:' )) { |
262
|
|
|
$v = \substr ( $v, 3 ); |
263
|
|
|
} else { |
264
|
|
|
$v = '"' . $v . '"'; |
265
|
|
|
} |
266
|
|
|
$result [] = '"' . $k . '": ' . $v; |
267
|
|
|
} |
268
|
|
|
return '{' . \implode ( ',', $result ) . '}'; |
269
|
|
|
} |
270
|
|
|
} |
271
|
|
|
|