ODDDocument.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. <?php
  2. /**
  3. * This class is used during import and export to construct.
  4. *
  5. * @package Elgg.Core
  6. * @subpackage ODD
  7. * @deprecated 1.9
  8. */
  9. class ODDDocument implements \Iterator {
  10. /**
  11. * ODD Version
  12. *
  13. * @var string
  14. */
  15. private $ODDSupportedVersion = "1.0";
  16. /**
  17. * Elements of the document.
  18. */
  19. private $elements;
  20. /**
  21. * Optional wrapper factory.
  22. */
  23. private $wrapperfactory;
  24. /**
  25. * Create a new ODD Document.
  26. *
  27. * @param array $elements Elements to add
  28. *
  29. * @return void
  30. */
  31. public function __construct(array $elements = null) {
  32. if ($elements) {
  33. if (is_array($elements)) {
  34. $this->elements = $elements;
  35. } else {
  36. $this->addElement($elements);
  37. }
  38. } else {
  39. $this->elements = array();
  40. }
  41. }
  42. /**
  43. * Return the version of ODD being used.
  44. *
  45. * @return string
  46. */
  47. public function getVersion() {
  48. return $this->ODDSupportedVersion;
  49. }
  50. /**
  51. * Returns the number of elements
  52. *
  53. * @return int
  54. */
  55. public function getNumElements() {
  56. return count($this->elements);
  57. }
  58. /**
  59. * Add an element
  60. *
  61. * @param ODD $element An ODD element
  62. *
  63. * @return void
  64. */
  65. public function addElement(ODD $element) {
  66. if (!is_array($this->elements)) {
  67. $this->elements = array();
  68. }
  69. $this->elements[] = $element;
  70. }
  71. /**
  72. * Add multiple elements at once
  73. *
  74. * @param array $elements Array of ODD elements
  75. *
  76. * @return void
  77. */
  78. public function addElements(array $elements) {
  79. foreach ($elements as $element) {
  80. $this->addElement($element);
  81. }
  82. }
  83. /**
  84. * Return all elements
  85. *
  86. * @return array
  87. */
  88. public function getElements() {
  89. return $this->elements;
  90. }
  91. /**
  92. * Set an optional wrapper factory to optionally embed the ODD document in another format.
  93. *
  94. * @param \ODDWrapperFactory $factory The factory
  95. *
  96. * @return void
  97. */
  98. public function setWrapperFactory(\ODDWrapperFactory $factory) {
  99. $this->wrapperfactory = $factory;
  100. }
  101. /**
  102. * Magic function to generate valid ODD XML for this item.
  103. *
  104. * @return string
  105. */
  106. public function __toString() {
  107. $xml = "";
  108. if ($this->wrapperfactory) {
  109. // A wrapper has been provided
  110. $wrapper = $this->wrapperfactory->getElementWrapper($this); // Get the wrapper for this element
  111. $xml = $wrapper->wrap($this); // Wrap this element (and subelements)
  112. } else {
  113. // Output begin tag
  114. $generated = date("r");
  115. $xml .= "<odd version=\"{$this->ODDSupportedVersion}\" generated=\"$generated\">\n";
  116. // Get XML for elements
  117. foreach ($this->elements as $element) {
  118. $xml .= "$element";
  119. }
  120. // Output end tag
  121. $xml .= "</odd>\n";
  122. }
  123. return $xml;
  124. }
  125. // ITERATOR INTERFACE //////////////////////////////////////////////////////////////
  126. /*
  127. * This lets an entity's attributes be displayed using foreach as a normal array.
  128. * Example: http://www.sitepoint.com/print/php5-standard-library
  129. */
  130. private $valid = false;
  131. /**
  132. * Iterator interface
  133. *
  134. * @see Iterator::rewind()
  135. *
  136. * @return void
  137. */
  138. function rewind() {
  139. $this->valid = (false !== reset($this->elements));
  140. }
  141. /**
  142. * Iterator interface
  143. *
  144. * @see Iterator::current()
  145. *
  146. * @return void
  147. */
  148. function current() {
  149. return current($this->elements);
  150. }
  151. /**
  152. * Iterator interface
  153. *
  154. * @see Iterator::key()
  155. *
  156. * @return void
  157. */
  158. function key() {
  159. return key($this->elements);
  160. }
  161. /**
  162. * Iterator interface
  163. *
  164. * @see Iterator::next()
  165. *
  166. * @return void
  167. */
  168. function next() {
  169. $this->valid = (false !== next($this->elements));
  170. }
  171. /**
  172. * Iterator interface
  173. *
  174. * @see Iterator::valid()
  175. *
  176. * @return void
  177. */
  178. function valid() {
  179. return $this->valid;
  180. }
  181. }