1 | <?php |
||
11 | class DomHelper |
||
12 | { |
||
13 | 18 | public function loadDocument(string $pathToXml): DOMDocument |
|
14 | { |
||
15 | 18 | if (!file_exists($pathToXml)) { |
|
16 | throw new RuntimeException(sprintf('File %s does not exist', $pathToXml)); |
||
17 | } |
||
18 | |||
19 | 18 | $document = $this->createEmptyDomDocument(); |
|
20 | 18 | $this->loadXmlContents($document, $pathToXml); |
|
21 | 18 | return $document; |
|
22 | } |
||
23 | |||
24 | 6 | public function loadOrCreateDocument(string $pathToXml): DOMDocument |
|
25 | { |
||
26 | 6 | $document = $this->createEmptyDomDocument(); |
|
27 | |||
28 | 6 | if (!file_exists($pathToXml)) { |
|
29 | 3 | return $document; |
|
30 | } |
||
31 | |||
32 | 3 | $this->loadXmlContents($document, $pathToXml); |
|
33 | |||
34 | 3 | return $document; |
|
35 | } |
||
36 | |||
37 | 24 | private function createEmptyDomDocument(): DOMDocument |
|
44 | |||
45 | 21 | private function loadXmlContents(DOMDocument $document, string $pathToXml) |
|
54 | |||
55 | 24 | public function saveDocument(string $path, DOMDocument $document) |
|
59 | |||
60 | 18 | public function findNode(DOMNode $node, string $tagName, array $attributes = null): DOMNode |
|
69 | |||
70 | 24 | public function findOptionalNode(DOMNode $node, string $tagName, array $attributes = null) |
|
81 | |||
82 | 24 | public function findOrCreateChildNode(DOMNode $node, string $tagName, array $attributes = null): DOMElement |
|
93 | |||
94 | 22 | public function applyAttributesToElement(DOMElement $node, array $attributes) |
|
100 | |||
101 | /** |
||
102 | * @param DOMNode $node |
||
103 | * @param string $tagName |
||
104 | * @param array|null $attributes |
||
105 | * @return array|DomNode[] |
||
106 | */ |
||
107 | 24 | public function findNodes(DOMNode $node, string $tagName, array $attributes = null): array |
|
120 | |||
121 | 23 | private function matchesAttributes(DOMNode $childNode, array $attributes = null): bool |
|
137 | |||
138 | 11 | public function removeNodes(DOMElement $parentNode, string $tagName, array $attributes = null) |
|
139 | { |
||
145 | } |
||
146 |