Total Complexity | 6 |
Total Lines | 38 |
Duplicated Lines | 0 % |
Coverage | 100% |
Changes | 0 |
1 | <?php |
||
9 | final class Xml |
||
10 | { |
||
11 | /** |
||
12 | * Converts an XML string into either an associative array or an array of |
||
13 | * associative arrays. |
||
14 | * |
||
15 | * @param string $xml The XML string. |
||
16 | * @return array|CollectionInterface |
||
17 | */ |
||
18 | 14 | public static function parse(string $xml) |
|
19 | { |
||
20 | 14 | libxml_use_internal_errors(true); |
|
21 | 14 | if (!$obj = simplexml_load_string($xml)) { |
|
22 | 1 | return []; |
|
23 | } |
||
24 | 13 | if ($obj->attributes()) { |
|
25 | 6 | $items = []; |
|
26 | 6 | foreach ($obj->children() as $child) { |
|
27 | 6 | $items[] = static::flatten($child); |
|
28 | } |
||
29 | 6 | return Collection::make($items); |
|
30 | } |
||
31 | 7 | return (array) static::flatten($obj); |
|
32 | } |
||
33 | |||
34 | /** |
||
35 | * Removes attributes from SimpleXMLElement objects. |
||
36 | * |
||
37 | * @param SimpleXMLElement $item The SimpleXMLElement object. |
||
38 | * @return array |
||
39 | */ |
||
40 | 13 | public static function flatten(SimpleXMLElement $item) |
|
47 | } |
||
48 | } |
||
49 |