1 | 1 |
deleted file mode 100644 |
... | ... |
@@ -1,434 +0,0 @@ |
1 |
-<?php declare(strict_types=1); |
|
2 |
-/** |
|
3 |
- * Part of Windwalker project. |
|
4 |
- * |
|
5 |
- * @copyright Copyright (C) 2019 LYRASOFT. |
|
6 |
- * @license LGPL-2.0-or-later |
|
7 |
- */ |
|
8 |
- |
|
9 |
-namespace Windwalker\Structure; |
|
10 |
- |
|
11 |
-/** |
|
12 |
- * Class StructureHelper |
|
13 |
- * |
|
14 |
- * @since 2.0 |
|
15 |
- */ |
|
16 |
-class StructureHelper |
|
17 |
-{ |
|
18 |
- /** |
|
19 |
- * Property objectStorage. |
|
20 |
- * |
|
21 |
- * @var \SplObjectStorage |
|
22 |
- */ |
|
23 |
- private static $objectStorage; |
|
24 |
- |
|
25 |
- /** |
|
26 |
- * Load the contents of a file into the structure |
|
27 |
- * |
|
28 |
- * @param string $file Path to file to load |
|
29 |
- * @param string $format Format of the file [optional: defaults to JSON] |
|
30 |
- * @param array $options Options used by the formatter |
|
31 |
- * |
|
32 |
- * @return array Return parsed array. |
|
33 |
- * |
|
34 |
- * @since 2.1 |
|
35 |
- */ |
|
36 |
- public static function loadFile($file, $format = Format::JSON, $options = []) |
|
37 |
- { |
|
38 |
- if (!is_file($file)) { |
|
39 |
- throw new \InvalidArgumentException('No such file: ' . $file); |
|
40 |
- } |
|
41 |
- |
|
42 |
- if (strtolower($format) == Format::PHP) { |
|
43 |
- $data = include $file; |
|
44 |
- } else { |
|
45 |
- $data = file_get_contents($file); |
|
46 |
- } |
|
47 |
- |
|
48 |
- return static::loadString($data, $format, $options); |
|
49 |
- } |
|
50 |
- |
|
51 |
- /** |
|
52 |
- * Load a string into the structure |
|
53 |
- * |
|
54 |
- * @param string $data String to load into the structure |
|
55 |
- * @param string $format Format of the string |
|
56 |
- * @param array $options Options used by the formatter |
|
57 |
- * |
|
58 |
- * @return array Return parsed array. |
|
59 |
- * |
|
60 |
- * @since 2.1 |
|
61 |
- */ |
|
62 |
- public static function loadString($data, $format = Format::JSON, $options = []) |
|
63 |
- { |
|
64 |
- // Load a string into the given namespace [or default namespace if not given] |
|
65 |
- $class = static::getFormatClass($format); |
|
66 |
- |
|
67 |
- return $class::stringToStruct($data, $options); |
|
68 |
- } |
|
69 |
- |
|
70 |
- /** |
|
71 |
- * Get a namespace in a given string format |
|
72 |
- * |
|
73 |
- * @param array|object $data The structure data to convert to markup string. |
|
74 |
- * @param string $format Format to return the string in |
|
75 |
- * @param mixed $options Parameters used by the formatter, see formatters for more info |
|
76 |
- * |
|
77 |
- * @return string Namespace in string format |
|
78 |
- * |
|
79 |
- * @since 2.1 |
|
80 |
- */ |
|
81 |
- public static function toString($data, $format = Format::JSON, $options = []) |
|
82 |
- { |
|
83 |
- $class = static::getFormatClass($format); |
|
84 |
- |
|
85 |
- return $class::structToString($data, $options); |
|
86 |
- } |
|
87 |
- |
|
88 |
- /** |
|
89 |
- * getFormatClass |
|
90 |
- * |
|
91 |
- * @param string $format |
|
92 |
- * |
|
93 |
- * @return string|\Windwalker\Structure\Format\FormatInterface |
|
94 |
- * |
|
95 |
- * @throws \DomainException |
|
96 |
- * |
|
97 |
- * @since 2.1 |
|
98 |
- */ |
|
99 |
- public static function getFormatClass($format) |
|
100 |
- { |
|
101 |
- // Return a namespace in a given format |
|
102 |
- $class = sprintf('%s\Format\%sFormat', __NAMESPACE__, ucfirst(strtolower($format))); |
|
103 |
- |
|
104 |
- if (!class_exists($class)) { |
|
105 |
- throw new \DomainException( |
|
106 |
- sprintf( |
|
107 |
- 'Structure format: %s not supported. Class: %s not found.', |
|
108 |
- $format, |
|
109 |
- $class |
|
110 |
- ) |
|
111 |
- ); |
|
112 |
- } |
|
113 |
- |
|
114 |
- return $class; |
|
115 |
- } |
|
116 |
- |
|
117 |
- /** |
|
118 |
- * Method to determine if an array is an associative array. |
|
119 |
- * |
|
120 |
- * @param array $array An array to test. |
|
121 |
- * |
|
122 |
- * @return boolean True if the array is an associative array. |
|
123 |
- */ |
|
124 |
- public static function isAssociativeArray($array) |
|
125 |
- { |
|
126 |
- if (is_array($array)) { |
|
127 |
- foreach (array_keys($array) as $k => $v) { |
|
128 |
- if ($k !== $v) { |
|
129 |
- return true; |
|
130 |
- } |
|
131 |
- } |
|
132 |
- } |
|
133 |
- |
|
134 |
- return false; |
|
135 |
- } |
|
136 |
- |
|
137 |
- /** |
|
138 |
- * getValue |
|
139 |
- * |
|
140 |
- * @param array $array |
|
141 |
- * @param string $name |
|
142 |
- * @param mixed $default |
|
143 |
- * |
|
144 |
- * @return mixed |
|
145 |
- */ |
|
146 |
- public static function getValue(array $array, $name, $default = null) |
|
147 |
- { |
|
148 |
- return isset($array[$name]) ? $array[$name] : $default; |
|
149 |
- } |
|
150 |
- |
|
151 |
- /** |
|
152 |
- * Utility function to map an array to a stdClass object. |
|
153 |
- * |
|
154 |
- * @param array $array The array to map. |
|
155 |
- * @param string $class Name of the class to create |
|
156 |
- * |
|
157 |
- * @return object The object mapped from the given array |
|
158 |
- * |
|
159 |
- * @since 2.0 |
|
160 |
- */ |
|
161 |
- public static function toObject($array, $class = 'stdClass') |
|
162 |
- { |
|
163 |
- $object = new $class(); |
|
164 |
- |
|
165 |
- foreach ($array as $k => $v) { |
|
166 |
- if (is_array($v)) { |
|
167 |
- $object->$k = static::toObject($v, $class); |
|
168 |
- } else { |
|
169 |
- $object->$k = $v; |
|
170 |
- } |
|
171 |
- } |
|
172 |
- |
|
173 |
- return $object; |
|
174 |
- } |
|
175 |
- |
|
176 |
- /** |
|
177 |
- * Get data from array or object by path. |
|
178 |
- * |
|
179 |
- * Example: `StructureHelper::getByPath($array, 'foo.bar.yoo')` equals to $array['foo']['bar']['yoo']. |
|
180 |
- * |
|
181 |
- * @param mixed $data An array or object to get value. |
|
182 |
- * @param mixed $path The key path. |
|
183 |
- * @param string $separator Separator of paths. |
|
184 |
- * |
|
185 |
- * @return mixed Found value, null if not exists. |
|
186 |
- * |
|
187 |
- * @since 2.1 |
|
188 |
- */ |
|
189 |
- public static function getByPath(array $data, $path, $separator = '.') |
|
190 |
- { |
|
191 |
- $nodes = static::getPathNodes($path, $separator); |
|
192 |
- |
|
193 |
- if (empty($nodes)) { |
|
194 |
- return null; |
|
195 |
- } |
|
196 |
- |
|
197 |
- $dataTmp = $data; |
|
198 |
- |
|
199 |
- foreach ($nodes as $arg) { |
|
200 |
- if (is_object($dataTmp) && isset($dataTmp->$arg)) { |
|
201 |
- $dataTmp = $dataTmp->$arg; |
|
202 |
- } elseif ($dataTmp instanceof \ArrayAccess && isset($dataTmp[$arg])) { |
|
203 |
- $dataTmp = $dataTmp[$arg]; |
|
204 |
- } elseif (is_array($dataTmp) && isset($dataTmp[$arg])) { |
|
205 |
- $dataTmp = $dataTmp[$arg]; |
|
206 |
- } else { |
|
207 |
- return null; |
|
208 |
- } |
|
209 |
- } |
|
210 |
- |
|
211 |
- return $dataTmp; |
|
212 |
- } |
|
213 |
- |
|
214 |
- /** |
|
215 |
- * setByPath |
|
216 |
- * |
|
217 |
- * @param mixed &$data |
|
218 |
- * @param string $path |
|
219 |
- * @param mixed $value |
|
220 |
- * @param string $separator |
|
221 |
- * |
|
222 |
- * @return boolean |
|
223 |
- * |
|
224 |
- * @since 2.1 |
|
225 |
- */ |
|
226 |
- public static function setByPath(array &$data, $path, $value, $separator = '.') |
|
227 |
- { |
|
228 |
- $nodes = static::getPathNodes($path, $separator); |
|
229 |
- |
|
230 |
- if (empty($nodes)) { |
|
231 |
- return false; |
|
232 |
- } |
|
233 |
- |
|
234 |
- $dataTmp = &$data; |
|
235 |
- |
|
236 |
- foreach ($nodes as $node) { |
|
237 |
- if (is_array($dataTmp)) { |
|
238 |
- if (!isset($dataTmp[$node])) { |
|
239 |
- $dataTmp[$node] = []; |
|
240 |
- } |
|
241 |
- |
|
242 |
- $dataTmp = &$dataTmp[$node]; |
|
243 |
- } else { |
|
244 |
- // If a node is value but path is not go to the end, we replace this value as a new store. |
|
245 |
- // Then next node can insert new value to this store. |
|
246 |
- $dataTmp = []; |
|
247 |
- } |
|
248 |
- } |
|
249 |
- |
|
250 |
- // Now, path go to the end, means we get latest node, set value to this node. |
|
251 |
- $dataTmp = $value; |
|
252 |
- |
|
253 |
- return true; |
|
254 |
- } |
|
255 |
- |
|
256 |
- /** |
|
257 |
- * removeByPath |
|
258 |
- * |
|
259 |
- * @param array $data |
|
260 |
- * @param string $path |
|
261 |
- * @param string $separator |
|
262 |
- * |
|
263 |
- * @return bool |
|
264 |
- */ |
|
265 |
- public static function removeByPath(array &$data, $path, $separator = '.') |
|
266 |
- { |
|
267 |
- $nodes = static::getPathNodes($path, $separator); |
|
268 |
- |
|
269 |
- if (empty($nodes)) { |
|
270 |
- return false; |
|
271 |
- } |
|
272 |
- |
|
273 |
- $previous = null; |
|
274 |
- $dataTmp = &$data; |
|
275 |
- |
|
276 |
- foreach ($nodes as $node) { |
|
277 |
- if (is_array($dataTmp)) { |
|
278 |
- if (empty($dataTmp[$node])) { |
|
279 |
- return false; |
|
280 |
- } |
|
281 |
- |
|
282 |
- $previous = &$dataTmp; |
|
283 |
- $dataTmp = &$dataTmp[$node]; |
|
284 |
- } else { |
|
285 |
- return false; |
|
286 |
- } |
|
287 |
- } |
|
288 |
- |
|
289 |
- // Now, path go to the end, means we get latest node, set value to this node. |
|
290 |
- unset($previous[$node]); |
|
291 |
- |
|
292 |
- return true; |
|
293 |
- } |
|
294 |
- |
|
295 |
- /** |
|
296 |
- * Explode the structure path into an array and remove empty |
|
297 |
- * nodes that occur as a result of a double dot. ex: windwalker..test |
|
298 |
- * Finally, re-key the array so they are sequential. |
|
299 |
- * |
|
300 |
- * @param string $path |
|
301 |
- * @param string $separator |
|
302 |
- * |
|
303 |
- * @return array |
|
304 |
- */ |
|
305 |
- public static function getPathNodes($path, $separator = '.') |
|
306 |
- { |
|
307 |
- return array_values(array_filter(explode($separator, $path), 'strlen')); |
|
308 |
- } |
|
309 |
- |
|
310 |
- /** |
|
311 |
- * Method to recursively convert data to one dimension array. |
|
312 |
- * |
|
313 |
- * @param array|object $array The array or object to convert. |
|
314 |
- * @param string $separator The key separator. |
|
315 |
- * @param string $prefix Last level key prefix. |
|
316 |
- * |
|
317 |
- * @return array |
|
318 |
- */ |
|
319 |
- public static function flatten($array, $separator = '.', $prefix = '') |
|
320 |
- { |
|
321 |
- $return = []; |
|
322 |
- |
|
323 |
- if ($array instanceof \Traversable) { |
|
324 |
- $array = iterator_to_array($array); |
|
325 |
- } elseif (is_object($array)) { |
|
326 |
- $array = get_object_vars($array); |
|
327 |
- } |
|
328 |
- |
|
329 |
- foreach ($array as $k => $v) { |
|
330 |
- $key = $prefix ? $prefix . $separator . $k : $k; |
|
331 |
- |
|
332 |
- if (is_object($v) || is_array($v)) { |
|
333 |
- $return = array_merge($return, static::flatten($v, $separator, $key)); |
|
334 |
- } else { |
|
335 |
- $return[$key] = $v; |
|
336 |
- } |
|
337 |
- } |
|
338 |
- |
|
339 |
- return $return; |
|
340 |
- } |
|
341 |
- |
|
342 |
- /** |
|
343 |
- * Utility function to convert all types to an array. |
|
344 |
- * |
|
345 |
- * @param mixed $data The data to convert. |
|
346 |
- * @param bool $recursive Recursive if data is nested. |
|
347 |
- * |
|
348 |
- * @return array The converted array. |
|
349 |
- */ |
|
350 |
- public static function toArray($data, $recursive = false) |
|
351 |
- { |
|
352 |
- if ($data instanceof ValueReference) { |
|
353 |
- return $data; |
|
354 |
- } |
|
355 |
- |
|
356 |
- // Ensure the input data is an array. |
|
357 |
- if ($data instanceof \Traversable) { |
|
358 |
- $data = iterator_to_array($data); |
|
359 |
- } elseif (is_object($data)) { |
|
360 |
- $data = get_object_vars($data); |
|
361 |
- } else { |
|
362 |
- $data = (array) $data; |
|
363 |
- } |
|
364 |
- |
|
365 |
- if ($recursive) { |
|
366 |
- foreach ($data as &$value) { |
|
367 |
- if (is_array($value) || is_object($value)) { |
|
368 |
- $value = static::toArray($value, $recursive); |
|
369 |
- } |
|
370 |
- } |
|
371 |
- } |
|
372 |
- |
|
373 |
- return $data; |
|
374 |
- } |
|
375 |
- |
|
376 |
- /** |
|
377 |
- * dumpObjectValues |
|
378 |
- * |
|
379 |
- * @param mixed $object |
|
380 |
- * |
|
381 |
- * @return array |
|
382 |
- */ |
|
383 |
- public static function dumpObjectValues($object) |
|
384 |
- { |
|
385 |
- $data = []; |
|
386 |
- |
|
387 |
- static::$objectStorage = new \SplObjectStorage(); |
|
388 |
- |
|
389 |
- static::doDump($data, $object); |
|
390 |
- |
|
391 |
- return $data; |
|
392 |
- } |
|
393 |
- |
|
394 |
- /** |
|
395 |
- * doDump |
|
396 |
- * |
|
397 |
- * @param array $data |
|
398 |
- * @param mixed $object |
|
399 |
- * |
|
400 |
- * @return void |
|
401 |
- */ |
|
402 |
- private static function doDump(&$data, $object) |
|
403 |
- { |
|
404 |
- if (is_object($object) && static::$objectStorage->contains($object)) { |
|
405 |
- $data = null; |
|
406 |
- |
|
407 |
- return; |
|
408 |
- } |
|
409 |
- |
|
410 |
- if (is_object($object)) { |
|
411 |
- static::$objectStorage->attach($object); |
|
412 |
- } |
|
413 |
- |
|
414 |
- if (is_array($object) || $object instanceof \Traversable) { |
|
415 |
- foreach ($object as $key => $value) { |
|
416 |
- static::doDump($data[$key], $value); |
|
417 |
- } |
|
418 |
- } elseif (is_object($object)) { |
|
419 |
- $ref = new \ReflectionObject($object); |
|
420 |
- |
|
421 |
- $properties = $ref->getProperties(); |
|
422 |
- |
|
423 |
- foreach ($properties as $property) { |
|
424 |
- $property->setAccessible(true); |
|
425 |
- |
|
426 |
- $value = $property->getValue($object); |
|
427 |
- |
|
428 |
- static::doDump($data[$property->getName()], $value); |
|
429 |
- } |
|
430 |
- } else { |
|
431 |
- $data = $object; |
|
432 |
- } |
|
433 |
- } |
|
434 |
-} |
1 | 1 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,434 @@ |
1 |
+<?php declare(strict_types=1); |
|
2 |
+/** |
|
3 |
+ * Part of Windwalker project. |
|
4 |
+ * |
|
5 |
+ * @copyright Copyright (C) 2019 LYRASOFT. |
|
6 |
+ * @license LGPL-2.0-or-later |
|
7 |
+ */ |
|
8 |
+ |
|
9 |
+namespace Windwalker\Structure; |
|
10 |
+ |
|
11 |
+/** |
|
12 |
+ * Class StructureHelper |
|
13 |
+ * |
|
14 |
+ * @since 2.0 |
|
15 |
+ */ |
|
16 |
+class StructureHelper |
|
17 |
+{ |
|
18 |
+ /** |
|
19 |
+ * Property objectStorage. |
|
20 |
+ * |
|
21 |
+ * @var \SplObjectStorage |
|
22 |
+ */ |
|
23 |
+ private static $objectStorage; |
|
24 |
+ |
|
25 |
+ /** |
|
26 |
+ * Load the contents of a file into the structure |
|
27 |
+ * |
|
28 |
+ * @param string $file Path to file to load |
|
29 |
+ * @param string $format Format of the file [optional: defaults to JSON] |
|
30 |
+ * @param array $options Options used by the formatter |
|
31 |
+ * |
|
32 |
+ * @return array Return parsed array. |
|
33 |
+ * |
|
34 |
+ * @since 2.1 |
|
35 |
+ */ |
|
36 |
+ public static function loadFile($file, $format = Format::JSON, $options = []) |
|
37 |
+ { |
|
38 |
+ if (!is_file($file)) { |
|
39 |
+ throw new \InvalidArgumentException('No such file: ' . $file); |
|
40 |
+ } |
|
41 |
+ |
|
42 |
+ if (strtolower($format) == Format::PHP) { |
|
43 |
+ $data = include $file; |
|
44 |
+ } else { |
|
45 |
+ $data = file_get_contents($file); |
|
46 |
+ } |
|
47 |
+ |
|
48 |
+ return static::loadString($data, $format, $options); |
|
49 |
+ } |
|
50 |
+ |
|
51 |
+ /** |
|
52 |
+ * Load a string into the structure |
|
53 |
+ * |
|
54 |
+ * @param string $data String to load into the structure |
|
55 |
+ * @param string $format Format of the string |
|
56 |
+ * @param array $options Options used by the formatter |
|
57 |
+ * |
|
58 |
+ * @return array Return parsed array. |
|
59 |
+ * |
|
60 |
+ * @since 2.1 |
|
61 |
+ */ |
|
62 |
+ public static function loadString($data, $format = Format::JSON, $options = []) |
|
63 |
+ { |
|
64 |
+ // Load a string into the given namespace [or default namespace if not given] |
|
65 |
+ $class = static::getFormatClass($format); |
|
66 |
+ |
|
67 |
+ return $class::stringToStruct($data, $options); |
|
68 |
+ } |
|
69 |
+ |
|
70 |
+ /** |
|
71 |
+ * Get a namespace in a given string format |
|
72 |
+ * |
|
73 |
+ * @param array|object $data The structure data to convert to markup string. |
|
74 |
+ * @param string $format Format to return the string in |
|
75 |
+ * @param mixed $options Parameters used by the formatter, see formatters for more info |
|
76 |
+ * |
|
77 |
+ * @return string Namespace in string format |
|
78 |
+ * |
|
79 |
+ * @since 2.1 |
|
80 |
+ */ |
|
81 |
+ public static function toString($data, $format = Format::JSON, $options = []) |
|
82 |
+ { |
|
83 |
+ $class = static::getFormatClass($format); |
|
84 |
+ |
|
85 |
+ return $class::structToString($data, $options); |
|
86 |
+ } |
|
87 |
+ |
|
88 |
+ /** |
|
89 |
+ * getFormatClass |
|
90 |
+ * |
|
91 |
+ * @param string $format |
|
92 |
+ * |
|
93 |
+ * @return string|\Windwalker\Structure\Format\FormatInterface |
|
94 |
+ * |
|
95 |
+ * @throws \DomainException |
|
96 |
+ * |
|
97 |
+ * @since 2.1 |
|
98 |
+ */ |
|
99 |
+ public static function getFormatClass($format) |
|
100 |
+ { |
|
101 |
+ // Return a namespace in a given format |
|
102 |
+ $class = sprintf('%s\Format\%sFormat', __NAMESPACE__, ucfirst(strtolower($format))); |
|
103 |
+ |
|
104 |
+ if (!class_exists($class)) { |
|
105 |
+ throw new \DomainException( |
|
106 |
+ sprintf( |
|
107 |
+ 'Structure format: %s not supported. Class: %s not found.', |
|
108 |
+ $format, |
|
109 |
+ $class |
|
110 |
+ ) |
|
111 |
+ ); |
|
112 |
+ } |
|
113 |
+ |
|
114 |
+ return $class; |
|
115 |
+ } |
|
116 |
+ |
|
117 |
+ /** |
|
118 |
+ * Method to determine if an array is an associative array. |
|
119 |
+ * |
|
120 |
+ * @param array $array An array to test. |
|
121 |
+ * |
|
122 |
+ * @return boolean True if the array is an associative array. |
|
123 |
+ */ |
|
124 |
+ public static function isAssociativeArray($array) |
|
125 |
+ { |
|
126 |
+ if (is_array($array)) { |
|
127 |
+ foreach (array_keys($array) as $k => $v) { |
|
128 |
+ if ($k !== $v) { |
|
129 |
+ return true; |
|
130 |
+ } |
|
131 |
+ } |
|
132 |
+ } |
|
133 |
+ |
|
134 |
+ return false; |
|
135 |
+ } |
|
136 |
+ |
|
137 |
+ /** |
|
138 |
+ * getValue |
|
139 |
+ * |
|
140 |
+ * @param array $array |
|
141 |
+ * @param string $name |
|
142 |
+ * @param mixed $default |
|
143 |
+ * |
|
144 |
+ * @return mixed |
|
145 |
+ */ |
|
146 |
+ public static function getValue(array $array, $name, $default = null) |
|
147 |
+ { |
|
148 |
+ return isset($array[$name]) ? $array[$name] : $default; |
|
149 |
+ } |
|
150 |
+ |
|
151 |
+ /** |
|
152 |
+ * Utility function to map an array to a stdClass object. |
|
153 |
+ * |
|
154 |
+ * @param array $array The array to map. |
|
155 |
+ * @param string $class Name of the class to create |
|
156 |
+ * |
|
157 |
+ * @return object The object mapped from the given array |
|
158 |
+ * |
|
159 |
+ * @since 2.0 |
|
160 |
+ */ |
|
161 |
+ public static function toObject($array, $class = 'stdClass') |
|
162 |
+ { |
|
163 |
+ $object = new $class(); |
|
164 |
+ |
|
165 |
+ foreach ($array as $k => $v) { |
|
166 |
+ if (is_array($v)) { |
|
167 |
+ $object->$k = static::toObject($v, $class); |
|
168 |
+ } else { |
|
169 |
+ $object->$k = $v; |
|
170 |
+ } |
|
171 |
+ } |
|
172 |
+ |
|
173 |
+ return $object; |
|
174 |
+ } |
|
175 |
+ |
|
176 |
+ /** |
|
177 |
+ * Get data from array or object by path. |
|
178 |
+ * |
|
179 |
+ * Example: `StructureHelper::getByPath($array, 'foo.bar.yoo')` equals to $array['foo']['bar']['yoo']. |
|
180 |
+ * |
|
181 |
+ * @param mixed $data An array or object to get value. |
|
182 |
+ * @param mixed $path The key path. |
|
183 |
+ * @param string $separator Separator of paths. |
|
184 |
+ * |
|
185 |
+ * @return mixed Found value, null if not exists. |
|
186 |
+ * |
|
187 |
+ * @since 2.1 |
|
188 |
+ */ |
|
189 |
+ public static function getByPath(array $data, $path, $separator = '.') |
|
190 |
+ { |
|
191 |
+ $nodes = static::getPathNodes($path, $separator); |
|
192 |
+ |
|
193 |
+ if (empty($nodes)) { |
|
194 |
+ return null; |
|
195 |
+ } |
|
196 |
+ |
|
197 |
+ $dataTmp = $data; |
|
198 |
+ |
|
199 |
+ foreach ($nodes as $arg) { |
|
200 |
+ if (is_object($dataTmp) && isset($dataTmp->$arg)) { |
|
201 |
+ $dataTmp = $dataTmp->$arg; |
|
202 |
+ } elseif ($dataTmp instanceof \ArrayAccess && isset($dataTmp[$arg])) { |
|
203 |
+ $dataTmp = $dataTmp[$arg]; |
|
204 |
+ } elseif (is_array($dataTmp) && isset($dataTmp[$arg])) { |
|
205 |
+ $dataTmp = $dataTmp[$arg]; |
|
206 |
+ } else { |
|
207 |
+ return null; |
|
208 |
+ } |
|
209 |
+ } |
|
210 |
+ |
|
211 |
+ return $dataTmp; |
|
212 |
+ } |
|
213 |
+ |
|
214 |
+ /** |
|
215 |
+ * setByPath |
|
216 |
+ * |
|
217 |
+ * @param mixed &$data |
|
218 |
+ * @param string $path |
|
219 |
+ * @param mixed $value |
|
220 |
+ * @param string $separator |
|
221 |
+ * |
|
222 |
+ * @return boolean |
|
223 |
+ * |
|
224 |
+ * @since 2.1 |
|
225 |
+ */ |
|
226 |
+ public static function setByPath(array &$data, $path, $value, $separator = '.') |
|
227 |
+ { |
|
228 |
+ $nodes = static::getPathNodes($path, $separator); |
|
229 |
+ |
|
230 |
+ if (empty($nodes)) { |
|
231 |
+ return false; |
|
232 |
+ } |
|
233 |
+ |
|
234 |
+ $dataTmp = &$data; |
|
235 |
+ |
|
236 |
+ foreach ($nodes as $node) { |
|
237 |
+ if (is_array($dataTmp)) { |
|
238 |
+ if (!isset($dataTmp[$node])) { |
|
239 |
+ $dataTmp[$node] = []; |
|
240 |
+ } |
|
241 |
+ |
|
242 |
+ $dataTmp = &$dataTmp[$node]; |
|
243 |
+ } else { |
|
244 |
+ // If a node is value but path is not go to the end, we replace this value as a new store. |
|
245 |
+ // Then next node can insert new value to this store. |
|
246 |
+ $dataTmp = []; |
|
247 |
+ } |
|
248 |
+ } |
|
249 |
+ |
|
250 |
+ // Now, path go to the end, means we get latest node, set value to this node. |
|
251 |
+ $dataTmp = $value; |
|
252 |
+ |
|
253 |
+ return true; |
|
254 |
+ } |
|
255 |
+ |
|
256 |
+ /** |
|
257 |
+ * removeByPath |
|
258 |
+ * |
|
259 |
+ * @param array $data |
|
260 |
+ * @param string $path |
|
261 |
+ * @param string $separator |
|
262 |
+ * |
|
263 |
+ * @return bool |
|
264 |
+ */ |
|
265 |
+ public static function removeByPath(array &$data, $path, $separator = '.') |
|
266 |
+ { |
|
267 |
+ $nodes = static::getPathNodes($path, $separator); |
|
268 |
+ |
|
269 |
+ if (empty($nodes)) { |
|
270 |
+ return false; |
|
271 |
+ } |
|
272 |
+ |
|
273 |
+ $previous = null; |
|
274 |
+ $dataTmp = &$data; |
|
275 |
+ |
|
276 |
+ foreach ($nodes as $node) { |
|
277 |
+ if (is_array($dataTmp)) { |
|
278 |
+ if (empty($dataTmp[$node])) { |
|
279 |
+ return false; |
|
280 |
+ } |
|
281 |
+ |
|
282 |
+ $previous = &$dataTmp; |
|
283 |
+ $dataTmp = &$dataTmp[$node]; |
|
284 |
+ } else { |
|
285 |
+ return false; |
|
286 |
+ } |
|
287 |
+ } |
|
288 |
+ |
|
289 |
+ // Now, path go to the end, means we get latest node, set value to this node. |
|
290 |
+ unset($previous[$node]); |
|
291 |
+ |
|
292 |
+ return true; |
|
293 |
+ } |
|
294 |
+ |
|
295 |
+ /** |
|
296 |
+ * Explode the structure path into an array and remove empty |
|
297 |
+ * nodes that occur as a result of a double dot. ex: windwalker..test |
|
298 |
+ * Finally, re-key the array so they are sequential. |
|
299 |
+ * |
|
300 |
+ * @param string $path |
|
301 |
+ * @param string $separator |
|
302 |
+ * |
|
303 |
+ * @return array |
|
304 |
+ */ |
|
305 |
+ public static function getPathNodes($path, $separator = '.') |
|
306 |
+ { |
|
307 |
+ return array_values(array_filter(explode($separator, $path), 'strlen')); |
|
308 |
+ } |
|
309 |
+ |
|
310 |
+ /** |
|
311 |
+ * Method to recursively convert data to one dimension array. |
|
312 |
+ * |
|
313 |
+ * @param array|object $array The array or object to convert. |
|
314 |
+ * @param string $separator The key separator. |
|
315 |
+ * @param string $prefix Last level key prefix. |
|
316 |
+ * |
|
317 |
+ * @return array |
|
318 |
+ */ |
|
319 |
+ public static function flatten($array, $separator = '.', $prefix = '') |
|
320 |
+ { |
|
321 |
+ $return = []; |
|
322 |
+ |
|
323 |
+ if ($array instanceof \Traversable) { |
|
324 |
+ $array = iterator_to_array($array); |
|
325 |
+ } elseif (is_object($array)) { |
|
326 |
+ $array = get_object_vars($array); |
|
327 |
+ } |
|
328 |
+ |
|
329 |
+ foreach ($array as $k => $v) { |
|
330 |
+ $key = $prefix ? $prefix . $separator . $k : $k; |
|
331 |
+ |
|
332 |
+ if (is_object($v) || is_array($v)) { |
|
333 |
+ $return = array_merge($return, static::flatten($v, $separator, $key)); |
|
334 |
+ } else { |
|
335 |
+ $return[$key] = $v; |
|
336 |
+ } |
|
337 |
+ } |
|
338 |
+ |
|
339 |
+ return $return; |
|
340 |
+ } |
|
341 |
+ |
|
342 |
+ /** |
|
343 |
+ * Utility function to convert all types to an array. |
|
344 |
+ * |
|
345 |
+ * @param mixed $data The data to convert. |
|
346 |
+ * @param bool $recursive Recursive if data is nested. |
|
347 |
+ * |
|
348 |
+ * @return array The converted array. |
|
349 |
+ */ |
|
350 |
+ public static function toArray($data, $recursive = false) |
|
351 |
+ { |
|
352 |
+ if ($data instanceof ValueReference) { |
|
353 |
+ return $data; |
|
354 |
+ } |
|
355 |
+ |
|
356 |
+ // Ensure the input data is an array. |
|
357 |
+ if ($data instanceof \Traversable) { |
|
358 |
+ $data = iterator_to_array($data); |
|
359 |
+ } elseif (is_object($data)) { |
|
360 |
+ $data = get_object_vars($data); |
|
361 |
+ } else { |
|
362 |
+ $data = (array) $data; |
|
363 |
+ } |
|
364 |
+ |
|
365 |
+ if ($recursive) { |
|
366 |
+ foreach ($data as &$value) { |
|
367 |
+ if (is_array($value) || is_object($value)) { |
|
368 |
+ $value = static::toArray($value, $recursive); |
|
369 |
+ } |
|
370 |
+ } |
|
371 |
+ } |
|
372 |
+ |
|
373 |
+ return $data; |
|
374 |
+ } |
|
375 |
+ |
|
376 |
+ /** |
|
377 |
+ * dumpObjectValues |
|
378 |
+ * |
|
379 |
+ * @param mixed $object |
|
380 |
+ * |
|
381 |
+ * @return array |
|
382 |
+ */ |
|
383 |
+ public static function dumpObjectValues($object) |
|
384 |
+ { |
|
385 |
+ $data = []; |
|
386 |
+ |
|
387 |
+ static::$objectStorage = new \SplObjectStorage(); |
|
388 |
+ |
|
389 |
+ static::doDump($data, $object); |
|
390 |
+ |
|
391 |
+ return $data; |
|
392 |
+ } |
|
393 |
+ |
|
394 |
+ /** |
|
395 |
+ * doDump |
|
396 |
+ * |
|
397 |
+ * @param array $data |
|
398 |
+ * @param mixed $object |
|
399 |
+ * |
|
400 |
+ * @return void |
|
401 |
+ */ |
|
402 |
+ private static function doDump(&$data, $object) |
|
403 |
+ { |
|
404 |
+ if (is_object($object) && static::$objectStorage->contains($object)) { |
|
405 |
+ $data = null; |
|
406 |
+ |
|
407 |
+ return; |
|
408 |
+ } |
|
409 |
+ |
|
410 |
+ if (is_object($object)) { |
|
411 |
+ static::$objectStorage->attach($object); |
|
412 |
+ } |
|
413 |
+ |
|
414 |
+ if (is_array($object) || $object instanceof \Traversable) { |
|
415 |
+ foreach ($object as $key => $value) { |
|
416 |
+ static::doDump($data[$key], $value); |
|
417 |
+ } |
|
418 |
+ } elseif (is_object($object)) { |
|
419 |
+ $ref = new \ReflectionObject($object); |
|
420 |
+ |
|
421 |
+ $properties = $ref->getProperties(); |
|
422 |
+ |
|
423 |
+ foreach ($properties as $property) { |
|
424 |
+ $property->setAccessible(true); |
|
425 |
+ |
|
426 |
+ $value = $property->getValue($object); |
|
427 |
+ |
|
428 |
+ static::doDump($data[$property->getName()], $value); |
|
429 |
+ } |
|
430 |
+ } else { |
|
431 |
+ $data = $object; |
|
432 |
+ } |
|
433 |
+ } |
|
434 |
+} |