From: Vincent Vanwaelscappel Date: Mon, 19 Aug 2019 14:11:41 +0000 (+0200) Subject: wip #2941 X-Git-Url: http://git.cubedesigners.com/?a=commitdiff_plain;h=b182e67eb8e3f36189efb004aba0509e55488c68;p=cubist_util.git wip #2941 --- diff --git a/composer.json b/composer.json index 801a253..5766c83 100644 --- a/composer.json +++ b/composer.json @@ -23,6 +23,7 @@ ], "require": { "php": ">=7.0.0", + "ext-libxml": "*", "ext-mbstring": "*", "ext-dom": "*", "ext-simplexml": "*", diff --git a/src/Cubist/Util/XML/DOMSelector.php b/src/Cubist/Util/XML/DOMSelector.php index 6426fd1..a37b7de 100644 --- a/src/Cubist/Util/XML/DOMSelector.php +++ b/src/Cubist/Util/XML/DOMSelector.php @@ -1,23 +1,48 @@ xpath = new DOMXpath($data); + $this->_dom = $data; } else { - $dom = new DOMDocument(); - @$dom->loadHTML($data); - $this->xpath = new DOMXpath($dom); + libxml_use_internal_errors(true); + $this->_dom = new DOMDocument(); + @$this->_dom->loadHTML('' . $data); } + + $this->xpath = new DOMXpath($this->_data); } - public function select($selector, $as_array = true) { + /** + * @return DOMDocument + */ + public function getDOM() + { + return $this->_dom; + } + + public function select($selector, $as_array = true) + { $elements = $this->xpath->evaluate(self::selector_to_xpath($selector)); return $as_array ? self::elements_to_array($elements) : $elements; } @@ -34,39 +59,46 @@ class DOMSelector { * * Otherwise regular DOMElement's will be returned. */ - public static function select_elements($selector, $html, $as_array = true) { + public static function select_elements($selector, $html, $as_array = true) + { $dom = new DOMSelector($html); return $dom->select($selector, $as_array); } + /** * Convert $elements to an array. */ - public static function elements_to_array($elements) { + public static function elements_to_array($elements) + { $array = array(); for ($i = 0, $length = $elements->length; $i < $length; ++$i) if ($elements->item($i)->nodeType == XML_ELEMENT_NODE) array_push($array, self::element_to_array($elements->item($i))); return $array; } + /** * Convert $element to an array. */ - public static function element_to_array($element) { + public static function element_to_array($element) + { $array = array( 'name' => $element->nodeName, 'attributes' => array(), 'text' => $element->textContent, - 'children' =>self::elements_to_array($element->childNodes) + 'children' => self::elements_to_array($element->childNodes) ); if ($element->attributes->length) - foreach($element->attributes as $key => $attr) + foreach ($element->attributes as $key => $attr) $array['attributes'][$key] = $attr->value; return $array; } + /** * Convert $selector into an XPath string. */ - function selector_to_xpath($selector) { + function selector_to_xpath($selector) + { // remove spaces around operators $selector = preg_replace('/\s*>\s*/', '>', $selector); $selector = preg_replace('/\s*~\s*/', '~', $selector);