Elgg  Version 3.0
ElggXMLElement.php
Go to the documentation of this file.
1 <?php
2 
12 class ElggXMLElement implements Serializable {
16  private $_element;
17 
23  public function __construct($xml) {
24  if ($xml instanceof SimpleXMLElement) {
25  $this->_element = $xml;
26  } else {
27  // do not load entities
28  $disable_load_entities = libxml_disable_entity_loader(true);
29 
30  $this->_element = new SimpleXMLElement($xml);
31 
32  libxml_disable_entity_loader($disable_load_entities);
33  }
34  }
35 
39  public function getName() {
40  return $this->_element->getName();
41  }
42 
46  public function getAttributes() {
47  //include namespace declarations as attributes
48  $xmlnsRaw = $this->_element->getNamespaces();
49  $xmlns = [];
50  foreach ($xmlnsRaw as $key => $val) {
51  $label = 'xmlns' . ($key ? ":$key" : $key);
52  $xmlns[$label] = $val;
53  }
54  //get attributes and merge with namespaces
55  $attrRaw = $this->_element->attributes();
56  $attr = [];
57  foreach ($attrRaw as $key => $val) {
58  $attr[$key] = $val;
59  }
60  $attr = array_merge((array) $xmlns, (array) $attr);
61  $result = [];
62  foreach ($attr as $key => $val) {
63  $result[$key] = (string) $val;
64  }
65 
66  return $result;
67  }
68 
72  public function getContent() {
73  return (string) $this->_element;
74  }
75 
79  public function getChildren() {
80  $children = $this->_element->children();
81  $result = [];
82  foreach ($children as $val) {
83  $result[] = new \ElggXMLElement($val);
84  }
85 
86  return $result;
87  }
88 
96  public function __get($name) {
97  switch ($name) {
98  case 'name':
99  return $this->getName();
100 
101  case 'attributes':
102  return $this->getAttributes();
103 
104  case 'content':
105  return $this->getContent();
106 
107  case 'children':
108  return $this->getChildren();
109  }
110 
111  return null;
112  }
113 
121  public function __isset($name) {
122  switch ($name) {
123  case 'name':
124  return $this->getName() !== null;
125 
126  case 'attributes':
127  return $this->getAttributes() !== null;
128 
129  case 'content':
130  return $this->getContent() !== null;
131 
132  case 'children':
133  return $this->getChildren() !== null;
134  }
135 
136  return false;
137  }
138 
142  public function serialize() {
143  return serialize($this->getContent());
144  }
145 
149  public function unserialize($serialized) {
150  $element = $this->unserialize($serialized);
151  return new static($element);
152  }
153 }
__get($name)
Override ->
if($view &&elgg_view_exists($view)) $label
Definition: field.php:26
if(!$user||!$user->canDelete()) $name
Definition: delete.php:22
$children
Definition: item.php:20
__isset($name)
Override isset.
if($container instanceof ElggGroup &&$container->guid!=elgg_get_page_owner_guid()) $key
Definition: summary.php:55
__construct($xml)
Creates an from a string or existing SimpleXMLElement.
unserialize($serialized)
{}