Feed.php 9.5 KB

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