Total Complexity | 6 |
Total Lines | 37 |
Duplicated Lines | 0 % |
Coverage | 92.86% |
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 | 13 | public static function parse(string $xml) |
|
19 | { |
||
20 | 13 | if (!$obj = simplexml_load_string($xml)) { |
|
21 | return []; |
||
22 | } |
||
23 | 13 | if ($obj->attributes()) { |
|
24 | 6 | $items = []; |
|
25 | 6 | foreach ($obj->children() as $child) { |
|
26 | 6 | $items[] = static::flatten($child); |
|
27 | } |
||
28 | 6 | return Collection::make($items); |
|
29 | } |
||
30 | 7 | return (array) static::flatten($obj); |
|
31 | } |
||
32 | |||
33 | /** |
||
34 | * Removes attributes from SimpleXMLElement objects. |
||
35 | * |
||
36 | * @param SimpleXMLElement $item The SimpleXMLElement object. |
||
37 | * @return array |
||
38 | */ |
||
39 | 13 | public static function flatten(SimpleXMLElement $item) |
|
46 | } |
||
47 | } |
||
48 |