ODDDocument::rewind()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 3
cp 0
crap 2
rs 10
c 0
b 0
f 0
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
	/**
18
	 * Elements of the document.
19
	 */
20
	private $elements;
21
22
	/**
23
	 * Optional wrapper factory.
24
	 */
25
	private $wrapperfactory;
26
27
	/**
28
	 * Create a new ODD Document.
29
	 *
30
	 * @param array $elements Elements to add
31
	 *
32
	 * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
33
	 */
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
46
	/**
47
	 * Return the version of ODD being used.
48
	 *
49
	 * @return string
50
	 */
51
	public function getVersion() {
52
		return $this->ODDSupportedVersion;
53
	}
54
55
	/**
56
	 * Returns the number of elements
57
	 *
58
	 * @return int
59
	 */
60
	public function getNumElements() {
61
		return count($this->elements);
62
	}
63
64
	/**
65
	 * Add an element
66
	 *
67
	 * @param ODD $element An ODD element
68
	 *
69
	 * @return void
70
	 */
71
	public function addElement(ODD $element) {
72
		if (!is_array($this->elements)) {
73
			$this->elements = array();
74
		}
75
		$this->elements[] = $element;
76
	}
77
78
	/**
79
	 * Add multiple elements at once
80
	 *
81
	 * @param array $elements Array of ODD elements
82
	 *
83
	 * @return void
84
	 */
85
	public function addElements(array $elements) {
86
		foreach ($elements as $element) {
87
			$this->addElement($element);
88
		}
89
	}
90
91
	/**
92
	 * Return all elements
93
	 *
94
	 * @return array
95
	 */
96
	public function getElements() {
97
		return $this->elements;
98
	}
99
100
	/**
101
	 * Set an optional wrapper factory to optionally embed the ODD document in another format.
102
	 *
103
	 * @param \ODDWrapperFactory $factory The factory
104
	 *
105
	 * @return void
106
	 */
107
	public function setWrapperFactory(\ODDWrapperFactory $factory) {
108
		$this->wrapperfactory = $factory;
109
	}
110
111
	/**
112
	 * Magic function to generate valid ODD XML for this item.
113
	 *
114
	 * @return string
115
	 */
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
149
	/**
150
	 * Iterator interface
151
	 *
152
	 * @see Iterator::rewind()
153
	 *
154
	 * @return void
155
	 */
156
	function rewind() {
157
		$this->valid = (false !== reset($this->elements));
158
	}
159
160
	/**
161
	 * Iterator interface
162
	 *
163
	 * @see Iterator::current()
164
	 *
165
	 * @return void
166
	 */
167
	function current() {
168
		return current($this->elements);
169
	}
170
171
	/**
172
	 * Iterator interface
173
	 *
174
	 * @see Iterator::key()
175
	 *
176
	 * @return void
177
	 */
178
	function key() {
179
		return key($this->elements);
180
	}
181
182
	/**
183
	 * Iterator interface
184
	 *
185
	 * @see Iterator::next()
186
	 *
187
	 * @return void
188
	 */
189
	function next() {
190
		$this->valid = (false !== next($this->elements));
191
	}
192
193
	/**
194
	 * Iterator interface
195
	 *
196
	 * @see Iterator::valid()
197
	 *
198
	 * @return void
199
	 */
200
	function valid() {
201
		return $this->valid;
202
	}
203
}
204