annotate vendor/zendframework/zend-feed/src/Writer/Feed.php @ 3:e11175134f4e

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