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