1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Baruica\Xml\XmlReader; |
6
|
|
|
|
7
|
|
|
final class DomDocXmlReader implements XmlReader |
8
|
|
|
{ |
9
|
|
|
private $domXpath; |
10
|
|
|
|
11
|
|
|
private function __construct(\DOMDocument $domDocument) |
12
|
|
|
{ |
13
|
|
|
$this->domXpath = new \DOMXPath($domDocument); |
14
|
|
|
} |
15
|
|
|
|
16
|
|
View Code Duplication |
public static function fromFile(string $filePath): self |
|
|
|
|
17
|
|
|
{ |
18
|
|
|
$domDocument = new \DOMDocument(); |
19
|
|
|
|
20
|
|
|
try { |
21
|
|
|
if (false === file_exists($filePath) || false === $domDocument->load($filePath)) { |
22
|
|
|
throw new \RuntimeException(sprintf('Could not load xml file [%s].', $filePath)); |
23
|
|
|
} |
24
|
|
|
} catch (\Exception $e) { |
25
|
|
|
throw new \RuntimeException($e->getMessage()); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
return new self($domDocument); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
View Code Duplication |
public static function fromString(string $xmlStr): self |
|
|
|
|
32
|
|
|
{ |
33
|
|
|
$domDocument = new \DOMDocument(); |
34
|
|
|
|
35
|
|
|
try { |
36
|
|
|
if (false === $domDocument->loadXML($xmlStr)) { |
37
|
|
|
throw new \RuntimeException(sprintf('Could not load XML from string [%s]', $xmlStr)); |
38
|
|
|
} |
39
|
|
|
} catch (\Exception $e) { |
40
|
|
|
throw new \RuntimeException($e->getMessage()); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
return new self($domDocument); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function getList(string $xpath): \Generator |
47
|
|
|
{ |
48
|
|
|
if (null === $nodeList = $this->getNodeList($xpath)) { |
49
|
|
|
return []; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
foreach ($nodeList as $node) { |
53
|
|
|
yield $this->getNodeValue($node); |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function getNodeList(string $xpath, \DOMNode $contextNode = null): \DOMNodeList |
58
|
|
|
{ |
59
|
|
|
$nodeList = (null === $contextNode) |
60
|
|
|
? $this->domXpath->query($xpath) |
61
|
|
|
: $this->domXpath->query($xpath, $contextNode); |
62
|
|
|
|
63
|
|
|
if (false === $nodeList) { |
64
|
|
|
return new \DOMNodeList(); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
return $nodeList; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function getFirstNode(string $xpath, \DOMNode $contextNode = null): \DOMElement |
71
|
|
|
{ |
72
|
|
|
$nodeList = $this->getNodeList($xpath, $contextNode); |
73
|
|
|
|
74
|
|
|
if (0 === $nodeList->length) { |
75
|
|
|
throw new \RuntimeException(''); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
return $nodeList->item(0); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function getLastNode(string $xpath, \DOMNode $contextNode = null): \DOMElement |
82
|
|
|
{ |
83
|
|
|
$nodeList = $this->getNodeList($xpath, $contextNode); |
84
|
|
|
|
85
|
|
|
if (0 === $nodeList->length) { |
86
|
|
|
throw new \RuntimeException(''); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
$lastIndex = $nodeList->length - 1; |
90
|
|
|
|
91
|
|
|
return $nodeList->item($lastIndex); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function getNodeValue(\DOMElement $node = null): string |
95
|
|
|
{ |
96
|
|
|
if (null !== $node) { |
97
|
|
|
return $node->nodeValue; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
throw new \InvalidArgumentException(''); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function getNodeAttribute(string $att, \DOMElement $node = null): string |
104
|
|
|
{ |
105
|
|
|
if (null !== $node) { |
106
|
|
|
return $node->getAttribute($att); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
throw new \InvalidArgumentException(''); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
public function getNeighborNodeValue(string $neighborNodeName, \DOMElement $node = null): string |
113
|
|
|
{ |
114
|
|
|
if (null !== $node) { |
115
|
|
|
return $this->getNodeValue( |
116
|
|
|
$node->parentNode->getElementsByTagName($neighborNodeName)->item(0) |
117
|
|
|
); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
throw new \InvalidArgumentException(''); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
public function getValue(string $xpath, \DOMNode $contextNode = null): string |
124
|
|
|
{ |
125
|
|
|
return $this->getNodeValue($this->getFirstNode($xpath, $contextNode)); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
public function getLastValue(string $xpath, \DOMNode $contextNode = null): string |
129
|
|
|
{ |
130
|
|
|
return $this->getNodeValue($this->getLastNode($xpath, $contextNode)); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
public function getValues(\DOMNodeList $contextNodes, string $keyNodeName, array $valNodes = [], \Closure $fn = null, array $fnParams = []): array |
134
|
|
|
{ |
135
|
|
|
$values = []; |
136
|
|
|
|
137
|
|
|
foreach ($contextNodes as $node) { |
138
|
|
|
$keyNodeValue = $this->getValue($keyNodeName, $node); |
139
|
|
|
|
140
|
|
|
if (!array_key_exists($keyNodeValue, $values)) { |
141
|
|
|
$values[$keyNodeValue] = []; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
foreach ($valNodes as $valNodeName) { |
145
|
|
|
if (null !== $fn) { |
146
|
|
|
$params = $fnParams; |
147
|
|
|
array_unshift($params, $this->getValue($valNodeName, $node)); |
148
|
|
|
$values[$keyNodeValue] = $fn($params); |
149
|
|
|
} else { |
150
|
|
|
$values[$keyNodeValue] = $this->getValue($valNodeName, $node); |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
ksort($values); |
156
|
|
|
|
157
|
|
|
return $values; |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.