DumpTrait   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
lcom 0
cbo 0
dl 0
loc 50
ccs 20
cts 20
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A jsonSerialize() 0 4 1
A __toArray() 0 4 1
A __toXML() 0 11 1
A composeXML() 0 15 4
1
<?php
2
3
namespace Mhor\MediaInfo;
4
5
trait DumpTrait
6
{
7
    /**
8
     * Convert the object into json.
9
     *
10
     * @return array
11
     */
12 5
    public function jsonSerialize(): array
13
    {
14 5
        return get_object_vars($this);
15
    }
16
17
    /**
18
     * Dump object to array.
19
     *
20
     * @return array
21
     */
22 1
    public function __toArray(): array
23
    {
24 1
        return $this->jsonSerialize();
25
    }
26
27 2
    public function __toXML()
28
    {
29 2
        $components = explode('\\', get_class($this));
30 2
        $rootNode = end($components);
31 2
        $xml = new \SimpleXMLElement("<?xml version=\"1.0\"?><$rootNode></$rootNode>");
32 2
        $array = json_decode(json_encode($this), true);
33
34 2
        $this->composeXML($array, $xml);
35
36 2
        return $xml;
37
    }
38
39 2
    protected function composeXML($data, &$xml): void
40
    {
41 2
        foreach ($data as $key => $value) {
42 2
            if (is_array($value)) {
43 2
                if (is_numeric($key)) {
44 1
                    $key = 'item'.$key;
45
                }
46
47 2
                $subnode = $xml->addChild($key);
48 2
                $this->composeXML($value, $subnode);
49
            } else {
50 2
                $xml->addChild("$key", htmlspecialchars("$value"));
51
            }
52
        }
53 2
    }
54
}
55