]> _ Git - cubist_util.git/commitdiff
wip #2941
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Mon, 19 Aug 2019 14:11:41 +0000 (16:11 +0200)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Mon, 19 Aug 2019 14:11:41 +0000 (16:11 +0200)
composer.json
src/Cubist/Util/XML/DOMSelector.php

index 801a253a23e308d6712b1d6ae003321bdc9efdcf..5766c834c948aa0de6d01a79f4bd3cdda32ec8a0 100644 (file)
@@ -23,6 +23,7 @@
   ],
   "require": {
     "php": ">=7.0.0",
+    "ext-libxml": "*",
     "ext-mbstring": "*",
     "ext-dom": "*",
     "ext-simplexml": "*",
index 6426fd14781be1e7476212c97de23c45cd7827fd..a37b7debf8f8d068b9549f03adb7fbf6e2978368 100644 (file)
@@ -1,23 +1,48 @@
 <?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;
     }
@@ -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);