|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* A parser for XML that uses SimpleXMLElement |
|
4
|
|
|
* |
|
5
|
|
|
* @package Elgg.Core |
|
6
|
|
|
* @subpackage XML |
|
7
|
|
|
*/ |
|
8
|
|
|
class ElggXMLElement { |
|
9
|
|
|
/** |
|
10
|
|
|
* @var SimpleXMLElement |
|
11
|
|
|
*/ |
|
12
|
|
|
private $_element; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Creates an \ElggXMLParser from a string or existing SimpleXMLElement |
|
16
|
|
|
* |
|
17
|
|
|
* @param string|SimpleXMLElement $xml The XML to parse |
|
18
|
|
|
*/ |
|
19
|
|
|
public function __construct($xml) { |
|
20
|
|
|
if ($xml instanceof SimpleXMLElement) { |
|
21
|
|
|
$this->_element = $xml; |
|
22
|
|
|
} else { |
|
23
|
|
|
// do not load entities |
|
24
|
|
|
$disable_load_entities = libxml_disable_entity_loader(true); |
|
25
|
|
|
|
|
26
|
|
|
$this->_element = new SimpleXMLElement($xml); |
|
27
|
|
|
|
|
28
|
|
|
libxml_disable_entity_loader($disable_load_entities); |
|
29
|
|
|
} |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @return string The name of the element |
|
34
|
|
|
*/ |
|
35
|
|
|
public function getName() { |
|
36
|
|
|
return $this->_element->getName(); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @return string[] The attributes |
|
41
|
|
|
*/ |
|
42
|
|
|
public function getAttributes() { |
|
43
|
|
|
//include namespace declarations as attributes |
|
44
|
|
|
$xmlnsRaw = $this->_element->getNamespaces(); |
|
45
|
|
|
$xmlns = array(); |
|
46
|
|
|
foreach ($xmlnsRaw as $key => $val) { |
|
47
|
|
|
$label = 'xmlns' . ($key ? ":$key" : $key); |
|
48
|
|
|
$xmlns[$label] = $val; |
|
49
|
|
|
} |
|
50
|
|
|
//get attributes and merge with namespaces |
|
51
|
|
|
$attrRaw = $this->_element->attributes(); |
|
52
|
|
|
$attr = array(); |
|
53
|
|
|
foreach ($attrRaw as $key => $val) { |
|
54
|
|
|
$attr[$key] = $val; |
|
55
|
|
|
} |
|
56
|
|
|
$attr = array_merge((array) $xmlns, (array) $attr); |
|
57
|
|
|
$result = array(); |
|
58
|
|
|
foreach ($attr as $key => $val) { |
|
59
|
|
|
$result[$key] = (string) $val; |
|
60
|
|
|
} |
|
61
|
|
|
return $result; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @return string CData |
|
66
|
|
|
*/ |
|
67
|
|
|
public function getContent() { |
|
68
|
|
|
return (string) $this->_element; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @return \ElggXMLElement[] Child elements |
|
73
|
|
|
*/ |
|
74
|
|
|
public function getChildren() { |
|
75
|
|
|
$children = $this->_element->children(); |
|
76
|
|
|
$result = array(); |
|
77
|
|
|
foreach ($children as $val) { |
|
78
|
|
|
$result[] = new \ElggXMLElement($val); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
return $result; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Override -> |
|
86
|
|
|
* |
|
87
|
|
|
* @param string $name Property name |
|
88
|
|
|
* @return mixed |
|
89
|
|
|
*/ |
|
90
|
|
|
public function __get($name) { |
|
91
|
|
|
switch ($name) { |
|
92
|
|
|
case 'name': |
|
93
|
|
|
return $this->getName(); |
|
94
|
|
|
break; |
|
|
|
|
|
|
95
|
|
|
case 'attributes': |
|
96
|
|
|
return $this->getAttributes(); |
|
97
|
|
|
break; |
|
|
|
|
|
|
98
|
|
|
case 'content': |
|
99
|
|
|
return $this->getContent(); |
|
100
|
|
|
break; |
|
|
|
|
|
|
101
|
|
|
case 'children': |
|
102
|
|
|
return $this->getChildren(); |
|
103
|
|
|
break; |
|
|
|
|
|
|
104
|
|
|
} |
|
105
|
|
|
return null; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Override isset |
|
110
|
|
|
* |
|
111
|
|
|
* @param string $name Property name |
|
112
|
|
|
* @return boolean |
|
113
|
|
|
*/ |
|
114
|
|
|
public function __isset($name) { |
|
115
|
|
|
switch ($name) { |
|
116
|
|
|
case 'name': |
|
117
|
|
|
return $this->getName() !== null; |
|
118
|
|
|
break; |
|
|
|
|
|
|
119
|
|
|
case 'attributes': |
|
120
|
|
|
return $this->getAttributes() !== null; |
|
121
|
|
|
break; |
|
|
|
|
|
|
122
|
|
|
case 'content': |
|
123
|
|
|
return $this->getContent() !== null; |
|
124
|
|
|
break; |
|
|
|
|
|
|
125
|
|
|
case 'children': |
|
126
|
|
|
return $this->getChildren() !== null; |
|
127
|
|
|
break; |
|
|
|
|
|
|
128
|
|
|
} |
|
129
|
|
|
return false; |
|
130
|
|
|
} |
|
131
|
|
|
} |
|
132
|
|
|
|
The break statement is not necessary if it is preceded for example by a return statement:
If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.