@@ -23,43 +23,21 @@ public function __construct(DOMDocument $domDocument)
2323 $ this ->initRootElement ();
2424 }
2525
26- /**
27- * Find valid root node (not a comment, at least a DOMElement node)
28- * @throws InvalidArgumentException
29- */
30- protected function initRootElement ()
31- {
32- if ($ this ->domDocument ->hasChildNodes ()) {
33- foreach ($ this ->domDocument ->childNodes as $ node ) {
34- if ($ node instanceof DOMElement) {
35- $ this ->rootElement = $ this ->getElementHandler ($ node , $ this );
36- break ;
37- }
38- }
39- } else {
40- throw new InvalidArgumentException ('Document seems to be invalid ' , __LINE__ );
41- }
42- }
43-
4426 public function getHandler ($ node , int $ index = -1 ): AbstractNodeHandler
4527 {
4628 if ($ node instanceof DOMElement) {
4729 return $ this ->getElementHandler ($ node , $ this , $ index );
48- } elseif ($ node instanceof DOMAttr) {
30+ }
31+ if ($ node instanceof DOMAttr) {
4932 return $ this ->getAttributeHandler ($ node , $ this , $ index );
50- } elseif ($ node instanceof DOMNameSpaceNode) {
33+ }
34+ if ($ node instanceof DOMNameSpaceNode) {
5135 return new NameSpaceHandler ($ node , $ this , $ index );
5236 }
5337
5438 return $ this ->getNodeHandler ($ node , $ this , $ index );
5539 }
5640
57- abstract protected function getNodeHandler (DOMNode $ node , AbstractDomDocumentHandler $ domDocument , int $ index = -1 ): NodeHandler ;
58-
59- abstract protected function getElementHandler (DOMElement $ element , AbstractDomDocumentHandler $ domDocument , int $ index = -1 ): ElementHandler ;
60-
61- abstract protected function getAttributeHandler (DOMAttr $ attribute , AbstractDomDocumentHandler $ domDocument , int $ index = -1 ): AttributeHandler ;
62-
6341 public function getNodeByName (string $ name ): ?NodeHandler
6442 {
6543 return $ this ->domDocument ->getElementsByTagName ($ name )->length > 0 ? $ this ->getNodeHandler ($ this ->domDocument ->getElementsByTagName ($ name )->item (0 ), $ this ) : null ;
@@ -77,7 +55,7 @@ public function getElementByName(string $name): ?ElementHandler
7755
7856 public function getNodesByName (string $ name , ?string $ checkInstance = null ): array
7957 {
80- $ nodes = array () ;
58+ $ nodes = [] ;
8159 if ($ this ->domDocument ->getElementsByTagName ($ name )->length > 0 ) {
8260 foreach ($ this ->domDocument ->getElementsByTagName ($ name ) as $ node ) {
8361 if (is_null ($ checkInstance ) || $ node instanceof $ checkInstance ) {
@@ -110,13 +88,13 @@ public function getElementsByNameAndAttributes(string $name, array $attributes,
11088
11189 public function getElementsHandlers (DOMNodeList $ nodeList ): array
11290 {
113- $ nodes = array () ;
91+ $ nodes = [] ;
11492 if (!empty ($ nodeList )) {
11593 $ index = 0 ;
11694 foreach ($ nodeList as $ node ) {
11795 if ($ node instanceof DOMElement) {
11896 $ nodes [] = $ this ->getElementHandler ($ node , $ this , $ index );
119- $ index ++ ;
97+ ++ $ index ;
12098 }
12199 }
122100 }
@@ -129,7 +107,7 @@ public function searchTagsByXpath(string $name, array $attributes, ?DOMNode $nod
129107 $ xpath = new DOMXPath ($ node ? $ node ->ownerDocument : $ this ->domDocument );
130108 $ xQuery = sprintf ("%s//*[local-name()='%s'] " , $ node instanceof DOMNode ? '. ' : '' , $ name );
131109 foreach ($ attributes as $ attributeName => $ attributeValue ) {
132- if (strpos ($ attributeValue , '* ' ) !== false ) {
110+ if (false !== strpos ($ attributeValue , '* ' )) {
133111 $ xQuery .= sprintf ("[contains(@%s, '%s')] " , $ attributeName , str_replace ('* ' , '' , $ attributeValue ));
134112 } else {
135113 $ xQuery .= sprintf ("[@%s='%s'] " , $ attributeName , $ attributeValue );
@@ -145,4 +123,30 @@ public function getElementByNameAndAttributes(string $name, array $attributes):
145123
146124 return array_shift ($ elements );
147125 }
126+
127+ /**
128+ * Find valid root node (not a comment, at least a DOMElement node).
129+ *
130+ * @throws InvalidArgumentException
131+ */
132+ protected function initRootElement ()
133+ {
134+ if ($ this ->domDocument ->hasChildNodes ()) {
135+ foreach ($ this ->domDocument ->childNodes as $ node ) {
136+ if ($ node instanceof DOMElement) {
137+ $ this ->rootElement = $ this ->getElementHandler ($ node , $ this );
138+
139+ break ;
140+ }
141+ }
142+ } else {
143+ throw new InvalidArgumentException ('Document seems to be invalid ' , __LINE__ );
144+ }
145+ }
146+
147+ abstract protected function getNodeHandler (DOMNode $ node , AbstractDomDocumentHandler $ domDocument , int $ index = -1 ): NodeHandler ;
148+
149+ abstract protected function getElementHandler (DOMElement $ element , AbstractDomDocumentHandler $ domDocument , int $ index = -1 ): ElementHandler ;
150+
151+ abstract protected function getAttributeHandler (DOMAttr $ attribute , AbstractDomDocumentHandler $ domDocument , int $ index = -1 ): AttributeHandler ;
148152}
0 commit comments