/library/Zend/Feed/Writer/Feed.php

https://github.com/zucchi/zf2 · PHP · 239 lines · 105 code · 20 blank · 114 comment · 9 complexity · ef791911c907887ce344bf9904315a74 MD5 · raw file

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