]> gitweb.fluxo.info Git - lorea/elgg.git/commitdiff
Fixes 3468 - replaces xml_to_object function with MIT licensed implementation.
authorSrokap <srokap@gmail.com>
Mon, 30 Jul 2012 15:35:31 +0000 (17:35 +0200)
committerBrett Profitt <brett.profitt@gmail.com>
Wed, 5 Dec 2012 23:01:17 +0000 (18:01 -0500)
engine/classes/XmlElement.php
engine/lib/xml.php

index 280bba66472287ffd41a7b1ad2e9ca8ec070c168..eb798773184e4aa2189b792c79debd9996c3a4fc 100644 (file)
  * @subpackage XML
  */
 class XmlElement {
-       /** The name of the element */
-       public $name;
+       /**
+       * @var SimpleXMLElement
+        */
+       private $_element;
+       
+       /**
+        * Creates XmlElement from string or existing SimpleXMLElement
+        * @param string|SimpleXMLElement $xml
+        */
+       public function __construct($xml) {
+               if ($xml instanceof SimpleXMLElement) {
+                       $this->_element = $xml;
+               } else {
+                       $this->_element = new SimpleXMLElement($xml);
+               }
+       }
+       
+       /** 
+        * @return string The name of the element 
+        */
+       public function getName() {
+               return $this->_element->getName();
+       }
+       
+       /** 
+        * @return array:string The attributes 
+        */
+       public function getAttributes() {
+               //include namespace declarations as attributes
+               $xmlnsRaw = $this->_element->getNamespaces();
+               $xmlns = array();
+               foreach ($xmlnsRaw as $key => $val) {
+                       $label = 'xmlns'.($key?":$key":$key);
+                       $xmlns[$label] = $val;
+               }
+               //get attributes and merge with namespaces
+               $attrRaw = $this->_element->attributes();
+               $attr = array();
+               foreach ($attrRaw as $key => $val) {
+                       $attr[$key] = $val;
+               }
+               $attr = array_merge((array)$xmlns, (array)$attr);
+               $result = array();
+               foreach ($attr as $key => $val) {
+                       $result[$key] = (string)$val;
+               }
+               return $result;
+       }
+       
+       /** 
+        * @return string CData 
+        */
+       public function getContent() {
+               return (string)$this->_element;
+       }
+       
+       /** 
+        * @return array:XmlElement Child elements 
+        */
+       public function getChildren() {
+               $children = $this->_element->children();
+               $result = array();
+               foreach ($children as $val) {
+                       $result[] = new XmlElement($val);
+               }
+               return $result;
+       }
 
-       /** The attributes */
-       public $attributes;
-
-       /** CData */
-       public $content;
-
-       /** Child elements */
-       public $children;
+       function __get($name) {
+               switch ($name) {
+                       case 'name':
+                               return $this->getName();
+                               break;
+                       case 'attributes':
+                               return $this->getAttributes();
+                               break;
+                       case 'content':
+                               return $this->getContent();
+                               break;
+                       case 'children':
+                               return $this->getChildren();
+                               break;          
+               }
+               return null;
+       }
+       
+       function __isset($name) {
+               switch ($name) {
+                       case 'name':
+                               return $this->getName()!==null;
+                               break;
+                       case 'attributes':
+                               return $this->getAttributes()!==null;
+                               break;
+                       case 'content':
+                               return $this->getContent()!==null;
+                               break;
+                       case 'children':
+                               return $this->getChildren()!==null;
+                               break;
+               }
+               return false;
+       }
 };
index 813bc4ee0a83fdf7d08e67cfa684546c2ed3a6b1..4f1ae76a90503a7d902af30bde8b572755e2b799 100644 (file)
@@ -101,47 +101,11 @@ function serialise_array_to_xml(array $data, $n = 0) {
 
 /**
  * Parse an XML file into an object.
- * Based on code from http://de.php.net/manual/en/function.xml-parse-into-struct.php by
- * efredricksen at gmail dot com
  *
  * @param string $xml The XML
  *
  * @return object
  */
 function xml_to_object($xml) {
-       $parser = xml_parser_create();
-
-       // Parse $xml into a structure
-       xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
-       xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
-       xml_parse_into_struct($parser, $xml, $tags);
-
-       xml_parser_free($parser);
-
-       $elements = array();
-       $stack = array();
-
-       foreach ($tags as $tag) {
-               $index = count($elements);
-
-               if ($tag['type'] == "complete" || $tag['type'] == "open") {
-                       $elements[$index] = new XmlElement;
-                       $elements[$index]->name = $tag['tag'];
-                       $elements[$index]->attributes = elgg_extract('attributes', $tag, '');
-                       $elements[$index]->content = elgg_extract('value', $tag, '');
-
-                       if ($tag['type'] == "open") {
-                               $elements[$index]->children = array();
-                               $stack[count($stack)] = &$elements;
-                               $elements = &$elements[$index]->children;
-                       }
-               }
-
-               if ($tag['type'] == "close") {
-                       $elements = &$stack[count($stack) - 1];
-                       unset($stack[count($stack) - 1]);
-               }
-       }
-
-       return $elements[0];
+       return new XmlElement($xml);
 }