/lib/Zend/Feed/Writer/Feed.php

https://github.com/luisbraschi/ursp · PHP · 281 lines · 101 code · 26 blank · 154 comment · 9 complexity · f26a990903e22c1130fa10e7d26d9308 MD5 · raw file

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