3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
10 namespace Zend\Feed\Writer;
17 class Feed extends AbstractFeed implements Iterator, Countable
20 * Contains all entry objects
24 protected $entries = [];
27 * A pointer for the iterator to keep track of the entries array
31 protected $entriesKey = 0;
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.
38 * @return \Zend\Feed\Writer\Entry
40 public function createEntry()
43 if ($this->getEncoding()) {
44 $entry->setEncoding($this->getEncoding());
46 $entry->setType($this->getType());
51 * Appends a Zend\Feed\Writer\Deleted object representing a new entry tombstone
52 * to the feed data container's internal group of entries.
54 * @param Deleted $deleted
57 public function addTombstone(Deleted $deleted)
59 $this->entries[] = $deleted;
63 * Creates a new Zend\Feed\Writer\Deleted data container for use. This is NOT
64 * added to the current feed automatically, but is necessary to create a
65 * container with some initial values preset based on the current feed data.
69 public function createTombstone()
71 $deleted = new Deleted;
72 if ($this->getEncoding()) {
73 $deleted->setEncoding($this->getEncoding());
75 $deleted->setType($this->getType());
80 * Appends a Zend\Feed\Writer\Entry object representing a new entry/item
81 * the feed data container's internal group of entries.
86 public function addEntry(Entry $entry)
88 $this->entries[] = $entry;
93 * Removes a specific indexed entry from the internal queue. Entries must be
94 * added to a feed container in order to be indexed.
97 * @throws Exception\InvalidArgumentException
100 public function removeEntry($index)
102 if (! isset($this->entries[$index])) {
103 throw new Exception\InvalidArgumentException('Undefined index: ' . $index . '. Entry does not exist.');
105 unset($this->entries[$index]);
111 * Retrieve a specific indexed entry from the internal queue. Entries must be
112 * added to a feed container in order to be indexed.
115 * @throws Exception\InvalidArgumentException
117 public function getEntry($index = 0)
119 if (isset($this->entries[$index])) {
120 return $this->entries[$index];
122 throw new Exception\InvalidArgumentException('Undefined index: ' . $index . '. Entry does not exist.');
126 * Orders all indexed entries by date, thus offering date ordered readable
127 * content where a parser (or Homo Sapien) ignores the generic rule that
128 * XML element order is irrelevant and has no intrinsic meaning.
130 * Using this method will alter the original indexation.
134 public function orderByDate()
137 * Could do with some improvement for performance perhaps
141 foreach ($this->entries as $entry) {
142 if ($entry->getDateModified()) {
143 $timestamp = (int) $entry->getDateModified()->getTimestamp();
144 } elseif ($entry->getDateCreated()) {
145 $timestamp = (int) $entry->getDateCreated()->getTimestamp();
147 $entries[$timestamp] = $entry;
149 krsort($entries, SORT_NUMERIC);
150 $this->entries = array_values($entries);
156 * Get the number of feed entries.
157 * Required by the Iterator interface.
161 public function count()
163 return count($this->entries);
167 * Return the current entry
171 public function current()
173 return $this->entries[$this->key()];
177 * Return the current feed key
181 public function key()
183 return $this->entriesKey;
187 * Move the feed pointer forward
191 public function next()
197 * Reset the pointer in the feed object
201 public function rewind()
203 $this->entriesKey = 0;
207 * Check to see if the iterator is still valid
211 public function valid()
213 return 0 <= $this->entriesKey && $this->entriesKey < $this->count();
217 * Attempt to build and return the feed resulting from the data set
219 * @param string $type The feed type "rss" or "atom" to export as
220 * @param bool $ignoreExceptions
221 * @throws Exception\InvalidArgumentException
224 public function export($type, $ignoreExceptions = false)
226 $this->setType(strtolower($type));
227 $type = ucfirst($this->getType());
228 if ($type !== 'Rss' && $type !== 'Atom') {
229 throw new Exception\InvalidArgumentException('Invalid feed type specified: ' . $type . '.'
230 . ' Should be one of "rss" or "atom".');
232 $renderClass = 'Zend\\Feed\\Writer\\Renderer\\Feed\\' . $type;
233 $renderer = new $renderClass($this);
234 if ($ignoreExceptions) {
235 $renderer->ignoreExceptions();
237 return $renderer->render()->saveXml();