Elgg  Version 1.9
ODDDocument.php
Go to the documentation of this file.
1 <?php
9 class ODDDocument implements Iterator {
15  private $ODDSupportedVersion = "1.0";
16 
20  private $elements;
21 
25  private $wrapperfactory;
26 
34  public function __construct(array $elements = null) {
35  if ($elements) {
36  if (is_array($elements)) {
37  $this->elements = $elements;
38  } else {
39  $this->addElement($elements);
40  }
41  } else {
42  $this->elements = array();
43  }
44  }
45 
51  public function getVersion() {
52  return $this->ODDSupportedVersion;
53  }
54 
60  public function getNumElements() {
61  return count($this->elements);
62  }
63 
71  public function addElement(ODD $element) {
72  if (!is_array($this->elements)) {
73  $this->elements = array();
74  }
75  $this->elements[] = $element;
76  }
77 
85  public function addElements(array $elements) {
86  foreach ($elements as $element) {
87  $this->addElement($element);
88  }
89  }
90 
96  public function getElements() {
97  return $this->elements;
98  }
99 
107  public function setWrapperFactory(ODDWrapperFactory $factory) {
108  $this->wrapperfactory = $factory;
109  }
110 
116  public function __toString() {
117  $xml = "";
118 
119  if ($this->wrapperfactory) {
120  // A wrapper has been provided
121  $wrapper = $this->wrapperfactory->getElementWrapper($this); // Get the wrapper for this element
122 
123  $xml = $wrapper->wrap($this); // Wrap this element (and subelements)
124  } else {
125  // Output begin tag
126  $generated = date("r");
127  $xml .= "<odd version=\"{$this->ODDSupportedVersion}\" generated=\"$generated\">\n";
128 
129  // Get XML for elements
130  foreach ($this->elements as $element) {
131  $xml .= "$element";
132  }
133 
134  // Output end tag
135  $xml .= "</odd>\n";
136  }
137 
138  return $xml;
139  }
140 
141  // ITERATOR INTERFACE //////////////////////////////////////////////////////////////
142  /*
143  * This lets an entity's attributes be displayed using foreach as a normal array.
144  * Example: http://www.sitepoint.com/print/php5-standard-library
145  */
146 
147  private $valid = false;
148 
156  function rewind() {
157  $this->valid = (false !== reset($this->elements));
158  }
159 
167  function current() {
168  return current($this->elements);
169  }
170 
178  function key() {
179  return key($this->elements);
180  }
181 
189  function next() {
190  $this->valid = (false !== next($this->elements));
191  }
192 
200  function valid() {
201  return $this->valid;
202  }
203 }
getNumElements()
Returns the number of elements.
Definition: ODDDocument.php:60
valid()
Iterator interface.
__toString()
Magic function to generate valid ODD XML for this item.
addElement(ODD $element)
Add an element.
Definition: ODDDocument.php:71
setWrapperFactory(ODDWrapperFactory $factory)
Set an optional wrapper factory to optionally embed the ODD document in another format.
getElements()
Return all elements.
Definition: ODDDocument.php:96
current()
Iterator interface.
key()
Iterator interface.
next()
Iterator interface.
__construct(array $elements=null)
Create a new ODD Document.
Definition: ODDDocument.php:34
Definition: ODD.php:9
addElements(array $elements)
Add multiple elements at once.
Definition: ODDDocument.php:85
getVersion()
Return the version of ODD being used.
Definition: ODDDocument.php:51
rewind()
Iterator interface.