Xml   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 12
c 1
b 0
f 0
dl 0
loc 31
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A xml2array() 0 4 1
A sxiToArray() 0 15 4
1
<?php
2
3
namespace ThallesDKoester\Entregas;
4
5
use SimpleXMLIterator;
6
7
/**
8
 * Thalles D. Koester | Trait Xml
9
 *
10
 * @author  Thalles D. koester <[email protected]>
11
 * @package ThallesDKoester\Entregas
12
 */
13
trait Xml
14
{
15
    /**
16
     * @param string $xml
17
     * @return array
18
     */
19
    private function xml2array(string $xml): array
20
    {
21
        $sxi = new SimpleXMLIterator($xml);
22
        return self::sxiToArray($sxi);
23
    }
24
25
    /**
26
     * @param SimpleXMLIterator $sxi
27
     * @return array
28
     */
29
    private static function sxiToArray(SimpleXMLIterator $sxi): array
30
    {
31
        $a = [];
32
        for ($sxi->rewind(); $sxi->valid(); $sxi->next()) {
33
            if (!array_key_exists($sxi->key(), $a)) {
34
                $a[$sxi->key()] = [];
35
            }
36
37
            if ($sxi->hasChildren()) {
38
                $a[$sxi->key()][] = self::sxiToArray($sxi->current());
39
            } else {
40
                $a[$sxi->key()][] = strval($sxi->current());
41
            }
42
        }
43
        return $a;
44
    }
45
}