aboutsummaryrefslogtreecommitdiff
path: root/engine/classes/XmlElement.php
diff options
context:
space:
mode:
authorSrokap <srokap@gmail.com>2012-07-30 17:35:31 +0200
committerBrett Profitt <brett.profitt@gmail.com>2012-12-05 18:01:17 -0500
commit00b2501721a25acec48c9c9561844e2db86e39dd (patch)
tree52887be000d9656cd0d6530cfb95d977c1ab7efd /engine/classes/XmlElement.php
parentccf7abb4b2b94781f9b67a6cf8798994aa1cca0d (diff)
downloadelgg-00b2501721a25acec48c9c9561844e2db86e39dd.tar.gz
elgg-00b2501721a25acec48c9c9561844e2db86e39dd.tar.bz2
Fixes 3468 - replaces xml_to_object function with MIT licensed implementation.
Diffstat (limited to 'engine/classes/XmlElement.php')
-rw-r--r--engine/classes/XmlElement.php112
1 files changed, 102 insertions, 10 deletions
diff --git a/engine/classes/XmlElement.php b/engine/classes/XmlElement.php
index 280bba664..eb7987731 100644
--- a/engine/classes/XmlElement.php
+++ b/engine/classes/XmlElement.php
@@ -6,15 +6,107 @@
* @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;
+ }
};