PageRenderTime 402ms CodeModel.GetById 373ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/zendframework/zendframework/library/Zend/Feed/Writer/Feed.php

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