Added DOM helper method
authorAlexander Ebert <ebert@woltlab.com>
Sun, 16 Oct 2016 13:33:24 +0000 (15:33 +0200)
committerAlexander Ebert <ebert@woltlab.com>
Sun, 16 Oct 2016 13:33:24 +0000 (15:33 +0200)
wcfsetup/install/files/lib/util/DOMUtil.class.php

index 21ac28d4d7d87f029078cdff908c1929db4ebe87..22f59845cce521f0457d230c0c9b3d455348b62f 100644 (file)
@@ -1,5 +1,6 @@
 <?php
 namespace wcf\util;
+use wcf\system\exception\SystemException;
 
 /**
  * Provides helper methods to work with PHP's DOM implementation.
@@ -98,6 +99,27 @@ final class DOMUtil {
                return null;
        }
        
+       /**
+        * Returns a non-live collection of elements.
+        * 
+        * @param       (\DOMDocument|\DOMElement)      $context        context element
+        * @param       string                          $tagName        tag name
+        * @return      \DOMElement[]                   list of elements
+        * @throws      SystemException
+        */
+       public static function getElements($context, $tagName) {
+               if (!($context instanceof \DOMDocument) && !($context instanceof \DOMElement)) {
+                       throw new SystemException("Expected context to be either of type \\DOMDocument or \\DOMElement.");
+               }
+               
+               $elements = [];
+               foreach ($context->getElementsByTagName($tagName) as $element) {
+                       $elements[] = $element;
+               }
+               
+               return $elements;
+       }
+       
        /**
         * Returns the immediate parent element before provided ancestor element. Returns null if
         * the ancestor element is the direct parent of provided node.