Completed
Pull Request — master (#21)
by Greg
03:15
created

DomToArraySimplifier   A

Complexity

Total Complexity 33

Size/Duplication

Total Lines 125
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 33
c 1
b 0
f 0
lcom 1
cbo 1
dl 0
loc 125
rs 9.3999

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A simplifyToArray() 0 12 3
A elementToArray() 0 10 2
A getNodeAttributes() 0 11 3
A getNodeChildren() 0 13 3
A getNodeChildrenUnique() 0 11 4
B hasUniformChildren() 0 15 5
A simplifyUniformChildren() 0 8 2
B simplifyUniqueChildren() 0 16 6
A getNameOfSingleResultElement() 0 14 4
1
<?php
2
namespace Consolidation\OutputFormatters\Transformations;
3
4
use Consolidation\OutputFormatters\SimplifyToArrayInterface;
5
use Consolidation\OutputFormatters\FormatterOptions;
6
use Consolidation\OutputFormatters\StructuredData\Xml\DomDataInterface;
7
use Consolidation\OutputFormatters\StructuredData\Xml\XmlSchema;
8
9
/**
10
 * Simplify a DOMDocument to an array.
11
 */
12
class DomToArraySimplifier implements SimplifyToArrayInterface
13
{
14
    public function __construct()
15
    {
16
    }
17
18
    public function simplifyToArray($structuredData, FormatterOptions $options)
19
    {
20
        if ($structuredData instanceof DomDataInterface) {
21
            $structuredData = $structuredData->getDomData();
22
        }
23
        if ($structuredData instanceof \DOMDocument) {
24
            // $schema = $options->getXmlSchema();
0 ignored issues
show
Unused Code Comprehensibility introduced by
55% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
25
            $simplified = $this->elementToArray($structuredData);
26
            $structuredData = array_shift($simplified);
27
        }
28
        return $structuredData;
29
    }
30
31
    protected function elementToArray(\DOMNode $element)
32
    {
33
        if ($element->nodeType == XML_TEXT_NODE) {
34
            return $element->nodeValue;
35
        }
36
        $attributes = $this->getNodeAttributes($element);
37
        $children = $this->getNodeChildren($element);
38
39
        return array_merge($attributes, $children);
40
    }
41
42
    protected function getNodeAttributes($element)
43
    {
44
        if (empty($element->attributes)) {
45
            return [];
46
        }
47
        $attributes = [];
48
        foreach ($element->attributes as $key => $attribute) {
49
            $attributes[$key] = $attribute->nodeValue;
50
        }
51
        return $attributes;
52
    }
53
54
    protected function getNodeChildren($element)
55
    {
56
        if (empty($element->childNodes)) {
57
            return [];
58
        }
59
        $result = $this->getNodeChildrenUnique($element);
60
        if ($this->hasUniformChildren($result)) {
61
            $result = $this->simplifyUniformChildren($element->nodeName, $result);
62
        } else {
63
            $result = $this->simplifyUniqueChildren($result);
64
        }
65
        return $result;
66
    }
67
68
    protected function getNodeChildrenUnique($element)
69
    {
70
        $children = [];
71
        foreach ($element->childNodes as $key => $value) {
72
            $children[$key] = $this->elementToArray($value);
73
        }
74
        if ((count($children) == 1) && (is_string($children[0]))) {
75
            return [$element->nodeName => $children[0]];
76
        }
77
        return $children;
78
    }
79
80
    protected function hasUniformChildren($data)
81
    {
82
        $last = false;
83
        foreach ($data as $key => $value) {
84
            $name = $this->getNameOfSingleResultElement($key, $value);
85
            if (!$name) {
86
                return false;
87
            }
88
            if ($last && ($name != $last)) {
89
                return false;
90
            }
91
            $last = $name;
92
        }
93
        return true;
94
    }
95
96
    protected function simplifyUniformChildren($parentKey, $data)
97
    {
98
        $simplifiedChildren = [];
99
        foreach ($data as $key => $value) {
100
            $simplifiedChildren[$parentKey][] = array_shift($value);
101
        }
102
        return $simplifiedChildren;
103
    }
104
105
    protected function simplifyUniqueChildren($data)
106
    {
107
        $simplifiedChildren = [];
108
        foreach ($data as $key => $value) {
109
            if (is_numeric($key) && is_array($value) && (count($value) == 1)) {
110
                $valueKeys = array_keys($value);
111
                $key = $valueKeys[0];
112
                $value = array_shift($value);
113
            }
114
            if (array_key_exists($key, $simplifiedChildren)) {
115
                throw new \Exception("Cannot convert data from a DOM document to an array, because <$key> appears more than once, and is not wrapped in a <{$key}s> element.");
116
            }
117
            $simplifiedChildren[$key] = $value;
118
        }
119
        return $simplifiedChildren;
120
    }
121
122
    protected function getNameOfSingleResultElement($key, $value)
123
    {
124
        if (!is_numeric($key)) {
125
            return false;
126
        }
127
        if (!is_array($value)) {
128
            return false;
129
        }
130
        if (count($value) != 1) {
131
            return false;
132
        }
133
        $valueKeys = array_keys($value);
134
        return $valueKeys[0];
135
    }
136
}
137