<?php
+/**
+ * @see https://github.com/tj/php-selector
+ */
namespace Cubist\Util\XML;
use DOMDocument;
use DOMXPath;
-class DOMSelector {
- public function __construct($data) {
+class DOMSelector
+{
+ /**
+ * @var DOMDocument
+ */
+ protected $_dom;
+
+ /**
+ * DOMSelector constructor.
+ * @param $data DOMDocument|string
+ */
+ public function __construct($data)
+ {
if ($data instanceof DOMDocument) {
- $this->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('<?xml encoding="utf-8" ?>' . $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;
}
*
* 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);