1 | <?php |
||
5 | abstract class AbstractDomDocumentHandler |
||
6 | { |
||
7 | /** |
||
8 | * @var \DOMDocument |
||
9 | */ |
||
10 | protected $domDocument; |
||
11 | /** |
||
12 | * @var ElementHandler |
||
13 | */ |
||
14 | protected $rootElement; |
||
15 | /** |
||
16 | * @param \DOMDocument $domDocument |
||
17 | */ |
||
18 | 16 | public function __construct(\DOMDocument $domDocument) |
|
23 | /** |
||
24 | * Find valid root node (not a comment, at least a DOMElement node) |
||
25 | * @throws \InvalidArgumentException |
||
26 | */ |
||
27 | 16 | protected function initRootElement() |
|
40 | /** |
||
41 | * Return the matching node handler based on current \DomNode type |
||
42 | * @param \DOMNode|\DOMNameSpaceNode $node |
||
43 | * @param int $index |
||
44 | * @return NodeHandler|ElementHandler|AttributeHandler|NameSpaceHandler |
||
45 | */ |
||
46 | 156 | public function getHandler($node, $index = -1) |
|
57 | /** |
||
58 | * @param \DOMNode $node |
||
59 | * @param AbstractDomDocumentHandler $domDocument |
||
60 | * @param int $index |
||
61 | * @return NodeHandler |
||
62 | */ |
||
63 | abstract protected function getNodeHandler(\DOMNode $node, AbstractDomDocumentHandler $domDocument, $index = -1); |
||
64 | /** |
||
65 | * @param \DOMElement $element |
||
66 | * @param AbstractDomDocumentHandler $domDocument |
||
67 | * @param int $index |
||
68 | * @return ElementHandler |
||
69 | */ |
||
70 | abstract protected function getElementHandler(\DOMElement $element, AbstractDomDocumentHandler $domDocument, $index = -1); |
||
71 | /** |
||
72 | * @param \DOMAttr $attribute |
||
73 | * @param AbstractDomDocumentHandler $domDocument |
||
74 | * @param int $index |
||
75 | * @return AttributeHandler |
||
76 | */ |
||
77 | abstract protected function getAttributeHandler(\DOMAttr $attribute, AbstractDomDocumentHandler $domDocument, $index = -1); |
||
78 | /** |
||
79 | * @param string $name |
||
80 | * @return NodeHandler |
||
81 | */ |
||
82 | 84 | public function getNodeByName($name) |
|
86 | /** |
||
87 | * @param string $name |
||
88 | * @return ElementHandler |
||
89 | */ |
||
90 | 52 | public function getElementByName($name) |
|
91 | { |
||
92 | 52 | $node = $this->getNodeByName($name); |
|
93 | 52 | if ($node instanceof AbstractNodeHandler && $node->getNode() instanceof \DOMElement) { |
|
94 | 48 | return $this->getElementHandler($node->getNode(), $this); |
|
95 | } |
||
96 | 4 | return null; |
|
97 | } |
||
98 | /** |
||
99 | * @param string $name |
||
100 | * @param string $checkInstance |
||
101 | * @return NodeHandler[] |
||
102 | */ |
||
103 | 96 | public function getNodesByName($name, $checkInstance = null) |
|
115 | /** |
||
116 | * @param string $name |
||
117 | * @return ElementHandler[] |
||
118 | */ |
||
119 | 92 | public function getElementsByName($name) |
|
123 | /** |
||
124 | * @param string $name |
||
125 | * @param array $attributes |
||
126 | * @param \DOMNode $node |
||
127 | * @return ElementHandler[] |
||
128 | */ |
||
129 | 88 | public function getElementsByNameAndAttributes($name, array $attributes, \DOMNode $node = null) |
|
140 | /** |
||
141 | * @param \DOMNodeList $nodeList |
||
142 | * @return ElementHandler[] |
||
143 | */ |
||
144 | 92 | public function getElementsHandlers(\DOMNodeList $nodeList) |
|
158 | /** |
||
159 | * @param string $name |
||
160 | * @param array $attributes |
||
161 | * @param \DOMNode $node |
||
162 | * @return \DOMNodeList |
||
163 | */ |
||
164 | 88 | public function searchTagsByXpath($name, array $attributes, \DOMNode $node = null) |
|
165 | { |
||
166 | 88 | $xpath = new \DOMXPath($this->domDocument); |
|
167 | 88 | $xQuery = sprintf("%s//*[local-name()='%s']", $node instanceof \DOMNode ? '.' : '', $name); |
|
168 | 88 | foreach ($attributes as $attributeName => $attributeValue) { |
|
169 | 88 | if (strpos($attributeValue, '*') !== false) { |
|
170 | 4 | $xQuery .= sprintf("[contains(@%s, '%s')]", $attributeName, str_replace('*', '', $attributeValue)); |
|
171 | 3 | } else { |
|
172 | 88 | $xQuery .= sprintf("[@%s='%s']", $attributeName, $attributeValue); |
|
173 | } |
||
174 | 66 | } |
|
175 | 88 | return $xpath->query($xQuery, $node); |
|
176 | } |
||
177 | /** |
||
178 | * @param string $name |
||
179 | * @param array $attributes |
||
180 | * @return null|ElementHandler |
||
181 | */ |
||
182 | 80 | public function getElementByNameAndAttributes($name, array $attributes) |
|
187 | } |
||
188 |