123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132 |
- <?php
- class ElggXMLElement {
-
- private $_element;
-
- public function __construct($xml) {
- if ($xml instanceof SimpleXMLElement) {
- $this->_element = $xml;
- } else {
-
- $disable_load_entities = libxml_disable_entity_loader(true);
- $this->_element = new SimpleXMLElement($xml);
- libxml_disable_entity_loader($disable_load_entities);
- }
- }
-
- public function getName() {
- return $this->_element->getName();
- }
-
- public function getAttributes() {
-
- $xmlnsRaw = $this->_element->getNamespaces();
- $xmlns = array();
- foreach ($xmlnsRaw as $key => $val) {
- $label = 'xmlns' . ($key ? ":$key" : $key);
- $xmlns[$label] = $val;
- }
-
- $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;
- }
-
- public function getContent() {
- return (string) $this->_element;
- }
-
- public function getChildren() {
- $children = $this->_element->children();
- $result = array();
- foreach ($children as $val) {
- $result[] = new \ElggXMLElement($val);
- }
- return $result;
- }
-
- public 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;
- }
-
- public 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;
- }
- }
|