|
1 <?php |
|
2 |
|
3 /** |
|
4 * Zend Framework |
|
5 * |
|
6 * LICENSE |
|
7 * |
|
8 * This source file is subject to the new BSD license that is bundled |
|
9 * with this package in the file LICENSE.txt. |
|
10 * It is also available through the world-wide-web at this URL: |
|
11 * http://framework.zend.com/license/new-bsd |
|
12 * If you did not receive a copy of the license and are unable to |
|
13 * obtain it through the world-wide-web, please send an email |
|
14 * to license@zend.com so we can send you a copy immediately. |
|
15 * |
|
16 * @category Zend |
|
17 * @package Zend_Gdata |
|
18 * @subpackage App |
|
19 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) |
|
20 * @license http://framework.zend.com/license/new-bsd New BSD License |
|
21 * @version $Id: Feed.php 22880 2010-08-21 23:44:00Z ramon $ |
|
22 */ |
|
23 |
|
24 /** |
|
25 * @see Zend_Gdata_App_Entry |
|
26 */ |
|
27 require_once 'Zend/Gdata/App/Entry.php'; |
|
28 |
|
29 /** |
|
30 * @see Zend_Gdata_App_FeedSourceParent |
|
31 */ |
|
32 require_once 'Zend/Gdata/App/FeedSourceParent.php'; |
|
33 |
|
34 /** |
|
35 * Atom feed class |
|
36 * |
|
37 * @category Zend |
|
38 * @package Zend_Gdata |
|
39 * @subpackage App |
|
40 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) |
|
41 * @license http://framework.zend.com/license/new-bsd New BSD License |
|
42 */ |
|
43 class Zend_Gdata_App_Feed extends Zend_Gdata_App_FeedSourceParent |
|
44 implements Iterator, ArrayAccess, Countable |
|
45 { |
|
46 |
|
47 /** |
|
48 * The root xml element of this data element |
|
49 * |
|
50 * @var string |
|
51 */ |
|
52 protected $_rootElement = 'feed'; |
|
53 |
|
54 /** |
|
55 * Cache of feed entries. |
|
56 * |
|
57 * @var array |
|
58 */ |
|
59 protected $_entry = array(); |
|
60 |
|
61 /** |
|
62 * Current location in $_entry array |
|
63 * |
|
64 * @var int |
|
65 */ |
|
66 protected $_entryIndex = 0; |
|
67 |
|
68 /** |
|
69 * Make accessing some individual elements of the feed easier. |
|
70 * |
|
71 * Special accessors 'entry' and 'entries' are provided so that if |
|
72 * you wish to iterate over an Atom feed's entries, you can do so |
|
73 * using foreach ($feed->entries as $entry) or foreach |
|
74 * ($feed->entry as $entry). |
|
75 * |
|
76 * @param string $var The property to get. |
|
77 * @return mixed |
|
78 */ |
|
79 public function __get($var) |
|
80 { |
|
81 switch ($var) { |
|
82 case 'entries': |
|
83 return $this; |
|
84 default: |
|
85 return parent::__get($var); |
|
86 } |
|
87 } |
|
88 |
|
89 /** |
|
90 * Retrieves the DOM model representing this object and all children |
|
91 * |
|
92 * @param DOMDocument $doc |
|
93 * @return DOMElement |
|
94 */ |
|
95 public function getDOM($doc = null, $majorVersion = 1, $minorVersion = null) |
|
96 { |
|
97 $element = parent::getDOM($doc, $majorVersion, $minorVersion); |
|
98 foreach ($this->_entry as $entry) { |
|
99 $element->appendChild($entry->getDOM($element->ownerDocument)); |
|
100 } |
|
101 return $element; |
|
102 } |
|
103 |
|
104 /** |
|
105 * Creates individual Entry objects of the appropriate type and |
|
106 * stores them in the $_entry array based upon DOM data. |
|
107 * |
|
108 * @param DOMNode $child The DOMNode to process |
|
109 */ |
|
110 protected function takeChildFromDOM($child) |
|
111 { |
|
112 $absoluteNodeName = $child->namespaceURI . ':' . $child->localName; |
|
113 switch ($absoluteNodeName) { |
|
114 case $this->lookupNamespace('atom') . ':' . 'entry': |
|
115 $newEntry = new $this->_entryClassName($child); |
|
116 $newEntry->setHttpClient($this->getHttpClient()); |
|
117 $newEntry->setMajorProtocolVersion($this->getMajorProtocolVersion()); |
|
118 $newEntry->setMinorProtocolVersion($this->getMinorProtocolVersion()); |
|
119 $this->_entry[] = $newEntry; |
|
120 break; |
|
121 default: |
|
122 parent::takeChildFromDOM($child); |
|
123 break; |
|
124 } |
|
125 } |
|
126 |
|
127 /** |
|
128 * Get the number of entries in this feed object. |
|
129 * |
|
130 * @return integer Entry count. |
|
131 */ |
|
132 public function count() |
|
133 { |
|
134 return count($this->_entry); |
|
135 } |
|
136 |
|
137 /** |
|
138 * Required by the Iterator interface. |
|
139 * |
|
140 * @return void |
|
141 */ |
|
142 public function rewind() |
|
143 { |
|
144 $this->_entryIndex = 0; |
|
145 } |
|
146 |
|
147 /** |
|
148 * Required by the Iterator interface. |
|
149 * |
|
150 * @return mixed The current row, or null if no rows. |
|
151 */ |
|
152 public function current() |
|
153 { |
|
154 return $this->_entry[$this->_entryIndex]; |
|
155 } |
|
156 |
|
157 /** |
|
158 * Required by the Iterator interface. |
|
159 * |
|
160 * @return mixed The current row number (starts at 0), or NULL if no rows |
|
161 */ |
|
162 public function key() |
|
163 { |
|
164 return $this->_entryIndex; |
|
165 } |
|
166 |
|
167 /** |
|
168 * Required by the Iterator interface. |
|
169 * |
|
170 * @return mixed The next row, or null if no more rows. |
|
171 */ |
|
172 public function next() |
|
173 { |
|
174 ++$this->_entryIndex; |
|
175 } |
|
176 |
|
177 /** |
|
178 * Required by the Iterator interface. |
|
179 * |
|
180 * @return boolean Whether the iteration is valid |
|
181 */ |
|
182 public function valid() |
|
183 { |
|
184 return 0 <= $this->_entryIndex && $this->_entryIndex < $this->count(); |
|
185 } |
|
186 |
|
187 /** |
|
188 * Gets the array of atom:entry elements contained within this |
|
189 * atom:feed representation |
|
190 * |
|
191 * @return array Zend_Gdata_App_Entry array |
|
192 */ |
|
193 public function getEntry() |
|
194 { |
|
195 return $this->_entry; |
|
196 } |
|
197 |
|
198 /** |
|
199 * Sets the array of atom:entry elements contained within this |
|
200 * atom:feed representation |
|
201 * |
|
202 * @param array $value The array of Zend_Gdata_App_Entry elements |
|
203 * @return Zend_Gdata_App_Feed Provides a fluent interface |
|
204 */ |
|
205 public function setEntry($value) |
|
206 { |
|
207 $this->_entry = $value; |
|
208 return $this; |
|
209 } |
|
210 |
|
211 /** |
|
212 * Adds an entry representation to the array of entries |
|
213 * contained within this feed |
|
214 * |
|
215 * @param Zend_Gdata_App_Entry An individual entry to add. |
|
216 * @return Zend_Gdata_App_Feed Provides a fluent interface |
|
217 */ |
|
218 public function addEntry($value) |
|
219 { |
|
220 $this->_entry[] = $value; |
|
221 return $this; |
|
222 } |
|
223 |
|
224 /** |
|
225 * Required by the ArrayAccess interface |
|
226 * |
|
227 * @param int $key The index to set |
|
228 * @param Zend_Gdata_App_Entry $value The value to set |
|
229 * @return void |
|
230 */ |
|
231 public function offsetSet($key, $value) |
|
232 { |
|
233 $this->_entry[$key] = $value; |
|
234 } |
|
235 |
|
236 /** |
|
237 * Required by the ArrayAccess interface |
|
238 * |
|
239 * @param int $key The index to get |
|
240 * @param Zend_Gdata_App_Entry $value The value to set |
|
241 */ |
|
242 public function offsetGet($key) |
|
243 { |
|
244 if (array_key_exists($key, $this->_entry)) { |
|
245 return $this->_entry[$key]; |
|
246 } |
|
247 } |
|
248 |
|
249 /** |
|
250 * Required by the ArrayAccess interface |
|
251 * |
|
252 * @param int $key The index to set |
|
253 * @param Zend_Gdata_App_Entry $value The value to set |
|
254 */ |
|
255 public function offsetUnset($key) |
|
256 { |
|
257 if (array_key_exists($key, $this->_entry)) { |
|
258 unset($this->_entry[$key]); |
|
259 } |
|
260 } |
|
261 |
|
262 /** |
|
263 * Required by the ArrayAccess interface |
|
264 * |
|
265 * @param int $key The index to check for existence |
|
266 * @return boolean |
|
267 */ |
|
268 public function offsetExists($key) |
|
269 { |
|
270 return (array_key_exists($key, $this->_entry)); |
|
271 } |
|
272 |
|
273 /** |
|
274 * Retrieve the next set of results from this feed. |
|
275 * |
|
276 * @throws Zend_Gdata_App_Exception |
|
277 * @return mixed|null Returns the next set of results as a feed of the same |
|
278 * class as this feed, or null if no results exist. |
|
279 */ |
|
280 public function getNextFeed() |
|
281 { |
|
282 $nextLink = $this->getNextLink(); |
|
283 if (!$nextLink) { |
|
284 require_once 'Zend/Gdata/App/HttpException.php'; |
|
285 throw new Zend_Gdata_App_Exception('No link to next set ' . |
|
286 'of results found.'); |
|
287 } |
|
288 $nextLinkHref = $nextLink->getHref(); |
|
289 $service = new Zend_Gdata_App($this->getHttpClient()); |
|
290 |
|
291 return $service->getFeed($nextLinkHref, get_class($this)); |
|
292 } |
|
293 |
|
294 /** |
|
295 * Retrieve the previous set of results from this feed. |
|
296 * |
|
297 * @throws Zend_Gdata_App_Exception |
|
298 * @return mixed|null Returns the previous set of results as a feed of |
|
299 * the same class as this feed, or null if no results exist. |
|
300 */ |
|
301 public function getPreviousFeed() |
|
302 { |
|
303 $previousLink = $this->getPreviousLink(); |
|
304 if (!$previousLink) { |
|
305 require_once 'Zend/Gdata/App/HttpException.php'; |
|
306 throw new Zend_Gdata_App_Exception('No link to previous set ' . |
|
307 'of results found.'); |
|
308 } |
|
309 $previousLinkHref = $previousLink->getHref(); |
|
310 $service = new Zend_Gdata_App($this->getHttpClient()); |
|
311 |
|
312 return $service->getFeed($previousLinkHref, get_class($this)); |
|
313 } |
|
314 |
|
315 /** |
|
316 * Set the major protocol version that should be used. Values < 1 will |
|
317 * cause a Zend_Gdata_App_InvalidArgumentException to be thrown. |
|
318 * |
|
319 * This value will be propogated to all child entries. |
|
320 * |
|
321 * @see _majorProtocolVersion |
|
322 * @param (int|NULL) $value The major protocol version to use. |
|
323 * @throws Zend_Gdata_App_InvalidArgumentException |
|
324 */ |
|
325 public function setMajorProtocolVersion($value) |
|
326 { |
|
327 parent::setMajorProtocolVersion($value); |
|
328 foreach ($this->entries as $entry) { |
|
329 $entry->setMajorProtocolVersion($value); |
|
330 } |
|
331 } |
|
332 |
|
333 /** |
|
334 * Set the minor protocol version that should be used. If set to NULL, no |
|
335 * minor protocol version will be sent to the server. Values < 0 will |
|
336 * cause a Zend_Gdata_App_InvalidArgumentException to be thrown. |
|
337 * |
|
338 * This value will be propogated to all child entries. |
|
339 * |
|
340 * @see _minorProtocolVersion |
|
341 * @param (int|NULL) $value The minor protocol version to use. |
|
342 * @throws Zend_Gdata_App_InvalidArgumentException |
|
343 */ |
|
344 public function setMinorProtocolVersion($value) |
|
345 { |
|
346 parent::setMinorProtocolVersion($value); |
|
347 foreach ($this->entries as $entry) { |
|
348 $entry->setMinorProtocolVersion($value); |
|
349 } |
|
350 } |
|
351 |
|
352 } |