Chris@0: getEncoding()) { Chris@0: $entry->setEncoding($this->getEncoding()); Chris@0: } Chris@0: $entry->setType($this->getType()); Chris@0: return $entry; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Appends a Zend\Feed\Writer\Deleted object representing a new entry tombstone Chris@0: * to the feed data container's internal group of entries. Chris@0: * Chris@0: * @param Deleted $deleted Chris@0: * @return void Chris@0: */ Chris@0: public function addTombstone(Deleted $deleted) Chris@0: { Chris@0: $this->entries[] = $deleted; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Creates a new Zend\Feed\Writer\Deleted data container for use. This is NOT Chris@0: * added to the current feed automatically, but is necessary to create a Chris@0: * container with some initial values preset based on the current feed data. Chris@0: * Chris@0: * @return Deleted Chris@0: */ Chris@0: public function createTombstone() Chris@0: { Chris@0: $deleted = new Deleted; Chris@0: if ($this->getEncoding()) { Chris@0: $deleted->setEncoding($this->getEncoding()); Chris@0: } Chris@0: $deleted->setType($this->getType()); Chris@0: return $deleted; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Appends a Zend\Feed\Writer\Entry object representing a new entry/item Chris@0: * the feed data container's internal group of entries. Chris@0: * Chris@0: * @param Entry $entry Chris@0: * @return Feed Chris@0: */ Chris@0: public function addEntry(Entry $entry) Chris@0: { Chris@0: $this->entries[] = $entry; Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Removes a specific indexed entry from the internal queue. Entries must be Chris@0: * added to a feed container in order to be indexed. Chris@0: * Chris@0: * @param int $index Chris@0: * @throws Exception\InvalidArgumentException Chris@0: * @return Feed Chris@0: */ Chris@0: public function removeEntry($index) Chris@0: { Chris@12: if (! isset($this->entries[$index])) { Chris@0: throw new Exception\InvalidArgumentException('Undefined index: ' . $index . '. Entry does not exist.'); Chris@0: } Chris@0: unset($this->entries[$index]); Chris@0: Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Retrieve a specific indexed entry from the internal queue. Entries must be Chris@0: * added to a feed container in order to be indexed. Chris@0: * Chris@0: * @param int $index Chris@17: * @return Entry Chris@0: * @throws Exception\InvalidArgumentException Chris@0: */ Chris@0: public function getEntry($index = 0) Chris@0: { Chris@0: if (isset($this->entries[$index])) { Chris@0: return $this->entries[$index]; Chris@0: } Chris@0: throw new Exception\InvalidArgumentException('Undefined index: ' . $index . '. Entry does not exist.'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Orders all indexed entries by date, thus offering date ordered readable Chris@0: * content where a parser (or Homo Sapien) ignores the generic rule that Chris@0: * XML element order is irrelevant and has no intrinsic meaning. Chris@0: * Chris@0: * Using this method will alter the original indexation. Chris@0: * Chris@0: * @return Feed Chris@0: */ Chris@0: public function orderByDate() Chris@0: { Chris@0: /** Chris@0: * Could do with some improvement for performance perhaps Chris@0: */ Chris@0: $timestamp = time(); Chris@0: $entries = []; Chris@0: foreach ($this->entries as $entry) { Chris@0: if ($entry->getDateModified()) { Chris@0: $timestamp = (int) $entry->getDateModified()->getTimestamp(); Chris@0: } elseif ($entry->getDateCreated()) { Chris@0: $timestamp = (int) $entry->getDateCreated()->getTimestamp(); Chris@0: } Chris@0: $entries[$timestamp] = $entry; Chris@0: } Chris@0: krsort($entries, SORT_NUMERIC); Chris@0: $this->entries = array_values($entries); Chris@0: Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Get the number of feed entries. Chris@0: * Required by the Iterator interface. Chris@0: * Chris@0: * @return int Chris@0: */ Chris@0: public function count() Chris@0: { Chris@0: return count($this->entries); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Return the current entry Chris@0: * Chris@0: * @return Entry Chris@0: */ Chris@0: public function current() Chris@0: { Chris@0: return $this->entries[$this->key()]; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Return the current feed key Chris@0: * Chris@0: * @return mixed Chris@0: */ Chris@0: public function key() Chris@0: { Chris@0: return $this->entriesKey; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Move the feed pointer forward Chris@0: * Chris@0: * @return void Chris@0: */ Chris@0: public function next() Chris@0: { Chris@0: ++$this->entriesKey; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Reset the pointer in the feed object Chris@0: * Chris@0: * @return void Chris@0: */ Chris@0: public function rewind() Chris@0: { Chris@0: $this->entriesKey = 0; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Check to see if the iterator is still valid Chris@0: * Chris@0: * @return bool Chris@0: */ Chris@0: public function valid() Chris@0: { Chris@0: return 0 <= $this->entriesKey && $this->entriesKey < $this->count(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Attempt to build and return the feed resulting from the data set Chris@0: * Chris@0: * @param string $type The feed type "rss" or "atom" to export as Chris@0: * @param bool $ignoreExceptions Chris@0: * @throws Exception\InvalidArgumentException Chris@0: * @return string Chris@0: */ Chris@0: public function export($type, $ignoreExceptions = false) Chris@0: { Chris@0: $this->setType(strtolower($type)); Chris@0: $type = ucfirst($this->getType()); Chris@0: if ($type !== 'Rss' && $type !== 'Atom') { Chris@0: throw new Exception\InvalidArgumentException('Invalid feed type specified: ' . $type . '.' Chris@0: . ' Should be one of "rss" or "atom".'); Chris@0: } Chris@0: $renderClass = 'Zend\\Feed\\Writer\\Renderer\\Feed\\' . $type; Chris@0: $renderer = new $renderClass($this); Chris@0: if ($ignoreExceptions) { Chris@0: $renderer->ignoreExceptions(); Chris@0: } Chris@0: return $renderer->render()->saveXml(); Chris@0: } Chris@0: }