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(); |
|
|
|
|
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
|
|
|
|
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.