ElggXMLElement.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. * Creates an \ElggXMLParser from a string or existing SimpleXMLElement
  15. *
  16. * @param string|SimpleXMLElement $xml The XML to parse
  17. */
  18. public function __construct($xml) {
  19. if ($xml instanceof SimpleXMLElement) {
  20. $this->_element = $xml;
  21. } else {
  22. // do not load entities
  23. $disable_load_entities = libxml_disable_entity_loader(true);
  24. $this->_element = new SimpleXMLElement($xml);
  25. libxml_disable_entity_loader($disable_load_entities);
  26. }
  27. }
  28. /**
  29. * @return string The name of the element
  30. */
  31. public function getName() {
  32. return $this->_element->getName();
  33. }
  34. /**
  35. * @return string[] The attributes
  36. */
  37. public function getAttributes() {
  38. //include namespace declarations as attributes
  39. $xmlnsRaw = $this->_element->getNamespaces();
  40. $xmlns = array();
  41. foreach ($xmlnsRaw as $key => $val) {
  42. $label = 'xmlns' . ($key ? ":$key" : $key);
  43. $xmlns[$label] = $val;
  44. }
  45. //get attributes and merge with namespaces
  46. $attrRaw = $this->_element->attributes();
  47. $attr = array();
  48. foreach ($attrRaw as $key => $val) {
  49. $attr[$key] = $val;
  50. }
  51. $attr = array_merge((array) $xmlns, (array) $attr);
  52. $result = array();
  53. foreach ($attr as $key => $val) {
  54. $result[$key] = (string) $val;
  55. }
  56. return $result;
  57. }
  58. /**
  59. * @return string CData
  60. */
  61. public function getContent() {
  62. return (string) $this->_element;
  63. }
  64. /**
  65. * @return \ElggXMLElement[] Child elements
  66. */
  67. public function getChildren() {
  68. $children = $this->_element->children();
  69. $result = array();
  70. foreach ($children as $val) {
  71. $result[] = new \ElggXMLElement($val);
  72. }
  73. return $result;
  74. }
  75. /**
  76. * Override ->
  77. *
  78. * @param string $name Property name
  79. * @return mixed
  80. */
  81. public function __get($name) {
  82. switch ($name) {
  83. case 'name':
  84. return $this->getName();
  85. break;
  86. case 'attributes':
  87. return $this->getAttributes();
  88. break;
  89. case 'content':
  90. return $this->getContent();
  91. break;
  92. case 'children':
  93. return $this->getChildren();
  94. break;
  95. }
  96. return null;
  97. }
  98. /**
  99. * Override isset
  100. *
  101. * @param string $name Property name
  102. * @return boolean
  103. */
  104. public function __isset($name) {
  105. switch ($name) {
  106. case 'name':
  107. return $this->getName() !== null;
  108. break;
  109. case 'attributes':
  110. return $this->getAttributes() !== null;
  111. break;
  112. case 'content':
  113. return $this->getContent() !== null;
  114. break;
  115. case 'children':
  116. return $this->getChildren() !== null;
  117. break;
  118. }
  119. return false;
  120. }
  121. }