Elgg /
Elgg
| 1 | <?php |
||
| 2 | |||
| 3 | /** |
||
| 4 | * A parser for XML that uses SimpleXMLElement |
||
| 5 | * |
||
| 6 | * @package Elgg.Core |
||
| 7 | * @subpackage XML |
||
| 8 | * |
||
| 9 | * @see \Elgg\Integration\ElggCoreRegressionBugsTest |
||
| 10 | * @see \Elgg\Integration\ElggCorePluginsAPITest |
||
| 11 | */ |
||
| 12 | class ElggXMLElement implements Serializable { |
||
| 13 | /** |
||
| 14 | * @var SimpleXMLElement |
||
| 15 | */ |
||
| 16 | private $_element; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * Creates an \ElggXMLParser from a string or existing SimpleXMLElement |
||
| 20 | * |
||
| 21 | * @param string|SimpleXMLElement $xml The XML to parse |
||
| 22 | */ |
||
| 23 | 244 | public function __construct($xml) { |
|
| 24 | 244 | if ($xml instanceof SimpleXMLElement) { |
|
| 25 | 244 | $this->_element = $xml; |
|
| 26 | } else { |
||
| 27 | // do not load entities |
||
| 28 | 244 | $disable_load_entities = libxml_disable_entity_loader(true); |
|
| 29 | |||
| 30 | 244 | $this->_element = new SimpleXMLElement($xml); |
|
| 31 | |||
| 32 | 244 | libxml_disable_entity_loader($disable_load_entities); |
|
| 33 | } |
||
| 34 | 244 | } |
|
| 35 | |||
| 36 | /** |
||
| 37 | * @return string The name of the element |
||
| 38 | */ |
||
| 39 | 243 | public function getName() { |
|
| 40 | 243 | return $this->_element->getName(); |
|
| 41 | } |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @return string[] The attributes |
||
| 45 | */ |
||
| 46 | 243 | public function getAttributes() { |
|
| 47 | //include namespace declarations as attributes |
||
| 48 | 243 | $xmlnsRaw = $this->_element->getNamespaces(); |
|
| 49 | 243 | $xmlns = []; |
|
| 50 | 243 | foreach ($xmlnsRaw as $key => $val) { |
|
| 51 | 241 | $label = 'xmlns' . ($key ? ":$key" : $key); |
|
| 52 | 241 | $xmlns[$label] = $val; |
|
| 53 | } |
||
| 54 | //get attributes and merge with namespaces |
||
| 55 | 243 | $attrRaw = $this->_element->attributes(); |
|
| 56 | 243 | $attr = []; |
|
| 57 | 243 | foreach ($attrRaw as $key => $val) { |
|
| 58 | 1 | $attr[$key] = $val; |
|
| 59 | } |
||
| 60 | 243 | $attr = array_merge((array) $xmlns, (array) $attr); |
|
| 61 | 243 | $result = []; |
|
| 62 | 243 | foreach ($attr as $key => $val) { |
|
| 63 | 242 | $result[$key] = (string) $val; |
|
| 64 | } |
||
| 65 | |||
| 66 | 243 | return $result; |
|
| 67 | } |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @return string CData |
||
| 71 | */ |
||
| 72 | 244 | public function getContent() { |
|
| 73 | 244 | return (string) $this->_element; |
|
| 74 | } |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @return \ElggXMLElement[] Child elements |
||
| 78 | */ |
||
| 79 | 244 | public function getChildren() { |
|
| 80 | 244 | $children = $this->_element->children(); |
|
| 81 | 244 | $result = []; |
|
| 82 | 244 | foreach ($children as $val) { |
|
| 83 | 244 | $result[] = new \ElggXMLElement($val); |
|
| 84 | } |
||
| 85 | |||
| 86 | 244 | return $result; |
|
| 87 | } |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Override -> |
||
| 91 | * |
||
| 92 | * @param string $name Property name |
||
| 93 | * |
||
| 94 | * @return mixed |
||
| 95 | */ |
||
| 96 | 242 | public function __get($name) { |
|
| 97 | 242 | switch ($name) { |
|
| 98 | case 'name': |
||
| 99 | 242 | return $this->getName(); |
|
| 100 | |||
| 101 | case 'attributes': |
||
| 102 | 242 | return $this->getAttributes(); |
|
| 103 | |||
| 104 | case 'content': |
||
| 105 | 242 | return $this->getContent(); |
|
| 106 | |||
| 107 | case 'children': |
||
| 108 | 242 | return $this->getChildren(); |
|
| 109 | } |
||
| 110 | |||
| 111 | 1 | return null; |
|
| 112 | } |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Override isset |
||
| 116 | * |
||
| 117 | * @param string $name Property name |
||
| 118 | * |
||
| 119 | * @return boolean |
||
| 120 | */ |
||
| 121 | 242 | public function __isset($name) { |
|
| 122 | 242 | switch ($name) { |
|
| 123 | case 'name': |
||
| 124 | 1 | return $this->getName() !== null; |
|
| 125 | |||
| 126 | case 'attributes': |
||
| 127 | 242 | return $this->getAttributes() !== null; |
|
| 128 | |||
| 129 | case 'content': |
||
| 130 | 1 | return $this->getContent() !== null; |
|
| 131 | |||
| 132 | case 'children': |
||
| 133 | 242 | return $this->getChildren() !== null; |
|
| 134 | } |
||
| 135 | |||
| 136 | 1 | return false; |
|
| 137 | } |
||
| 138 | |||
| 139 | /** |
||
| 140 | * {@inheritdoc} |
||
| 141 | */ |
||
| 142 | public function serialize() { |
||
| 143 | return serialize($this->getContent()); |
||
| 144 | } |
||
| 145 | |||
| 146 | /** |
||
| 147 | * {@inheritdoc} |
||
| 148 | */ |
||
| 149 | public function unserialize($serialized) { |
||
| 150 | $element = $this->unserialize($serialized); |
||
| 151 | return new static($element); |
||
|
1 ignored issue
–
show
|
|||
| 152 | } |
||
| 153 | } |
||
| 154 |
In the issue above, the returned value is violating the contract defined by the mentioned interface.
Let's take a look at an example: