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.
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
25 * @see Zend_Gdata_App_Entry
27 require_once 'Zend/Gdata/App/Entry.php';
30 * @see Zend_Gdata_App_FeedSourceParent
32 require_once 'Zend/Gdata/App/FeedSourceParent.php';
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
43 class Zend_Gdata_App_Feed extends Zend_Gdata_App_FeedSourceParent
44 implements Iterator, ArrayAccess
48 * The root xml element of this data element
52 protected $_rootElement = 'feed';
55 * Cache of feed entries.
59 protected $_entry = array();
62 * Current location in $_entry array
66 protected $_entryIndex = 0;
69 * Make accessing some individual elements of the feed easier.
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).
76 * @param string $var The property to get.
79 public function __get($var)
85 return parent::__get($var);
90 * Retrieves the DOM model representing this object and all children
92 * @param DOMDocument $doc
95 public function getDOM($doc = null, $majorVersion = 1, $minorVersion = null)
97 $element = parent::getDOM($doc, $majorVersion, $minorVersion);
98 foreach ($this->_entry as $entry) {
99 $element->appendChild($entry->getDOM($element->ownerDocument));
105 * Creates individual Entry objects of the appropriate type and
106 * stores them in the $_entry array based upon DOM data.
108 * @param DOMNode $child The DOMNode to process
110 protected function takeChildFromDOM($child)
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;
122 parent::takeChildFromDOM($child);
128 * Get the number of entries in this feed object.
130 * @return integer Entry count.
132 public function count()
134 return count($this->_entry);
138 * Required by the Iterator interface.
142 public function rewind()
144 $this->_entryIndex = 0;
148 * Required by the Iterator interface.
150 * @return mixed The current row, or null if no rows.
152 public function current()
154 return $this->_entry[$this->_entryIndex];
158 * Required by the Iterator interface.
160 * @return mixed The current row number (starts at 0), or NULL if no rows
162 public function key()
164 return $this->_entryIndex;
168 * Required by the Iterator interface.
170 * @return mixed The next row, or null if no more rows.
172 public function next()
174 ++$this->_entryIndex;
178 * Required by the Iterator interface.
180 * @return boolean Whether the iteration is valid
182 public function valid()
184 return 0 <= $this->_entryIndex && $this->_entryIndex < $this->count();
188 * Gets the array of atom:entry elements contained within this
189 * atom:feed representation
191 * @return array Zend_Gdata_App_Entry array
193 public function getEntry()
195 return $this->_entry;
199 * Sets the array of atom:entry elements contained within this
200 * atom:feed representation
202 * @param array $value The array of Zend_Gdata_App_Entry elements
203 * @return Zend_Gdata_App_Feed Provides a fluent interface
205 public function setEntry($value)
207 $this->_entry = $value;
212 * Adds an entry representation to the array of entries
213 * contained within this feed
215 * @param Zend_Gdata_App_Entry An individual entry to add.
216 * @return Zend_Gdata_App_Feed Provides a fluent interface
218 public function addEntry($value)
220 $this->_entry[] = $value;
225 * Required by the ArrayAccess interface
227 * @param int $key The index to set
228 * @param Zend_Gdata_App_Entry $value The value to set
231 public function offsetSet($key, $value)
233 $this->_entry[$key] = $value;
237 * Required by the ArrayAccess interface
239 * @param int $key The index to get
240 * @param Zend_Gdata_App_Entry $value The value to set
242 public function offsetGet($key)
244 if (array_key_exists($key, $this->_entry)) {
245 return $this->_entry[$key];
250 * Required by the ArrayAccess interface
252 * @param int $key The index to set
253 * @param Zend_Gdata_App_Entry $value The value to set
255 public function offsetUnset($key)
257 if (array_key_exists($key, $this->_entry)) {
258 unset($this->_entry[$key]);
263 * Required by the ArrayAccess interface
265 * @param int $key The index to check for existence
268 public function offsetExists($key)
270 return (array_key_exists($key, $this->_entry));
274 * Retrieve the next set of results from this feed.
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.
280 public function getNextFeed()
282 $nextLink = $this->getNextLink();
284 require_once 'Zend/Gdata/App/HttpException.php';
285 throw new Zend_Gdata_App_Exception('No link to next set ' .
286 'of results found.');
288 $nextLinkHref = $nextLink->getHref();
289 $service = new Zend_Gdata_App($this->getHttpClient());
291 return $service->getFeed($nextLinkHref, get_class($this));
295 * Retrieve the previous set of results from this feed.
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.
301 public function getPreviousFeed()
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.');
309 $previousLinkHref = $previousLink->getHref();
310 $service = new Zend_Gdata_App($this->getHttpClient());
312 return $service->getFeed($previousLinkHref, get_class($this));
316 * Set the major protocol version that should be used. Values < 1 will
317 * cause a Zend_Gdata_App_InvalidArgumentException to be thrown.
319 * This value will be propogated to all child entries.
321 * @see _majorProtocolVersion
322 * @param (int|NULL) $value The major protocol version to use.
323 * @throws Zend_Gdata_App_InvalidArgumentException
325 public function setMajorProtocolVersion($value)
327 parent::setMajorProtocolVersion($value);
328 foreach ($this->entries as $entry) {
329 $entry->setMajorProtocolVersion($value);
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.
338 * This value will be propogated to all child entries.
340 * @see _minorProtocolVersion
341 * @param (int|NULL) $value The minor protocol version to use.
342 * @throws Zend_Gdata_App_InvalidArgumentException
344 public function setMinorProtocolVersion($value)
346 parent::setMinorProtocolVersion($value);
347 foreach ($this->entries as $entry) {
348 $entry->setMinorProtocolVersion($value);