Feed.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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_Feed_Writer
  17. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id$
  20. */
  21. /**
  22. * @see Zend_Date
  23. */
  24. require_once 'Zend/Date.php';
  25. /**
  26. * @see Zend_Date
  27. */
  28. require_once 'Zend/Uri.php';
  29. /**
  30. * @see Zend_Feed_Writer
  31. */
  32. require_once 'Zend/Feed/Writer.php';
  33. /**
  34. * @see Zend_Feed_Writer_Entry
  35. */
  36. require_once 'Zend/Feed/Writer/Entry.php';
  37. /**
  38. * @see Zend_Feed_Writer_Renderer_Feed_Atom
  39. */
  40. require_once 'Zend/Feed/Writer/Renderer/Feed/Atom.php';
  41. /**
  42. * @see Zend_Feed_Writer_Renderer_Feed_Rss
  43. */
  44. require_once 'Zend/Feed/Writer/Renderer/Feed/Rss.php';
  45. require_once 'Zend/Feed/Writer/Feed/FeedAbstract.php';
  46. /**
  47. * @category Zend
  48. * @package Zend_Feed_Writer
  49. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  50. * @license http://framework.zend.com/license/new-bsd New BSD License
  51. */
  52. class Zend_Feed_Writer_Feed extends Zend_Feed_Writer_Feed_FeedAbstract
  53. implements Iterator, Countable
  54. {
  55. /**
  56. * Contains all entry objects
  57. *
  58. * @var array
  59. */
  60. protected $_entries = array();
  61. /**
  62. * A pointer for the iterator to keep track of the entries array
  63. *
  64. * @var int
  65. */
  66. protected $_entriesKey = 0;
  67. /**
  68. * Creates a new Zend_Feed_Writer_Entry data container for use. This is NOT
  69. * added to the current feed automatically, but is necessary to create a
  70. * container with some initial values preset based on the current feed data.
  71. *
  72. * @return Zend_Feed_Writer_Entry
  73. */
  74. public function createEntry()
  75. {
  76. $entry = new Zend_Feed_Writer_Entry;
  77. if ($this->getEncoding()) {
  78. $entry->setEncoding($this->getEncoding());
  79. }
  80. $entry->setType($this->getType());
  81. return $entry;
  82. }
  83. /**
  84. * Appends a Zend_Feed_Writer_Entry object representing a new entry/item
  85. * the feed data container's internal group of entries.
  86. *
  87. * @param Zend_Feed_Writer_Entry $entry
  88. */
  89. public function addEntry(Zend_Feed_Writer_Entry $entry)
  90. {
  91. $this->_entries[] = $entry;
  92. }
  93. /**
  94. * Removes a specific indexed entry from the internal queue. Entries must be
  95. * added to a feed container in order to be indexed.
  96. *
  97. * @param int $index
  98. */
  99. public function removeEntry($index)
  100. {
  101. if (isset($this->_entries[$index])) {
  102. unset($this->_entries[$index]);
  103. }
  104. require_once 'Zend/Feed/Exception.php';
  105. throw new Zend_Feed_Exception('Undefined index: ' . $index . '. Entry does not exist.');
  106. }
  107. /**
  108. * Retrieve a specific indexed entry from the internal queue. Entries must be
  109. * added to a feed container in order to be indexed.
  110. *
  111. * @param int $index
  112. */
  113. public function getEntry($index = 0)
  114. {
  115. if (isset($this->_entries[$index])) {
  116. return $this->_entries[$index];
  117. }
  118. require_once 'Zend/Feed/Exception.php';
  119. throw new Zend_Feed_Exception('Undefined index: ' . $index . '. Entry does not exist.');
  120. }
  121. /**
  122. * Orders all indexed entries by date, thus offering date ordered readable
  123. * content where a parser (or Homo Sapien) ignores the generic rule that
  124. * XML element order is irrelevant and has no intrinsic meaning.
  125. *
  126. * Using this method will alter the original indexation.
  127. *
  128. * @return void
  129. */
  130. public function orderByDate()
  131. {
  132. /**
  133. * Could do with some improvement for performance perhaps
  134. */
  135. $timestamp = time();
  136. $entries = array();
  137. foreach ($this->_entries as $entry) {
  138. if ($entry->getDateModified()) {
  139. $timestamp = (int) $entry->getDateModified()->get(Zend_Date::TIMESTAMP);
  140. } elseif ($entry->getDateCreated()) {
  141. $timestamp = (int) $entry->getDateCreated()->get(Zend_Date::TIMESTAMP);
  142. }
  143. $entries[$timestamp] = $entry;
  144. }
  145. krsort($entries, SORT_NUMERIC);
  146. $this->_entries = array_values($entries);
  147. }
  148. /**
  149. * Get the number of feed entries.
  150. * Required by the Iterator interface.
  151. *
  152. * @return int
  153. */
  154. public function count()
  155. {
  156. return count($this->_entries);
  157. }
  158. /**
  159. * Return the current entry
  160. *
  161. * @return Zend_Feed_Reader_Entry_Interface
  162. */
  163. public function current()
  164. {
  165. return $this->_entries[$this->key()];
  166. }
  167. /**
  168. * Return the current feed key
  169. *
  170. * @return unknown
  171. */
  172. public function key()
  173. {
  174. return $this->_entriesKey;
  175. }
  176. /**
  177. * Move the feed pointer forward
  178. *
  179. * @return void
  180. */
  181. public function next()
  182. {
  183. ++$this->_entriesKey;
  184. }
  185. /**
  186. * Reset the pointer in the feed object
  187. *
  188. * @return void
  189. */
  190. public function rewind()
  191. {
  192. $this->_entriesKey = 0;
  193. }
  194. /**
  195. * Check to see if the iterator is still valid
  196. *
  197. * @return boolean
  198. */
  199. public function valid()
  200. {
  201. return 0 <= $this->_entriesKey && $this->_entriesKey < $this->count();
  202. }
  203. /**
  204. * Attempt to build and return the feed resulting from the data set
  205. *
  206. * @param $type The feed type "rss" or "atom" to export as
  207. * @return string
  208. */
  209. public function export($type, $ignoreExceptions = false)
  210. {
  211. $this->setType(strtolower($type));
  212. $type = ucfirst($this->getType());
  213. if ($type !== 'Rss' && $type !== 'Atom') {
  214. require_once 'Zend/Feed/Exception.php';
  215. throw new Zend_Feed_Exception('Invalid feed type specified: ' . $type . '.'
  216. . ' Should be one of "rss" or "atom".');
  217. }
  218. $renderClass = 'Zend_Feed_Writer_Renderer_Feed_' . $type;
  219. $renderer = new $renderClass($this);
  220. if ($ignoreExceptions) {
  221. $renderer->ignoreExceptions();
  222. }
  223. return $renderer->render()->saveXml();
  224. }
  225. }