comparison vendor/zendframework/zend-feed/src/Writer/Feed.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children 7a779792577d
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
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-2015 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
8 */
9
10 namespace Zend\Feed\Writer;
11
12 use Countable;
13 use Iterator;
14
15 /**
16 */
17 class Feed extends AbstractFeed implements Iterator, Countable
18 {
19 /**
20 * Contains all entry objects
21 *
22 * @var array
23 */
24 protected $entries = [];
25
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 /**
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 /**
51 * Appends a Zend\Feed\Writer\Deleted object representing a new entry tombstone
52 * to the feed data container's internal group of entries.
53 *
54 * @param Deleted $deleted
55 * @return void
56 */
57 public function addTombstone(Deleted $deleted)
58 {
59 $this->entries[] = $deleted;
60 }
61
62 /**
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.
66 *
67 * @return Deleted
68 */
69 public function createTombstone()
70 {
71 $deleted = new Deleted;
72 if ($this->getEncoding()) {
73 $deleted->setEncoding($this->getEncoding());
74 }
75 $deleted->setType($this->getType());
76 return $deleted;
77 }
78
79 /**
80 * Appends a Zend\Feed\Writer\Entry object representing a new entry/item
81 * the feed data container's internal group of entries.
82 *
83 * @param Entry $entry
84 * @return Feed
85 */
86 public function addEntry(Entry $entry)
87 {
88 $this->entries[] = $entry;
89 return $this;
90 }
91
92 /**
93 * Removes a specific indexed entry from the internal queue. Entries must be
94 * added to a feed container in order to be indexed.
95 *
96 * @param int $index
97 * @throws Exception\InvalidArgumentException
98 * @return Feed
99 */
100 public function removeEntry($index)
101 {
102 if (!isset($this->entries[$index])) {
103 throw new Exception\InvalidArgumentException('Undefined index: ' . $index . '. Entry does not exist.');
104 }
105 unset($this->entries[$index]);
106
107 return $this;
108 }
109
110 /**
111 * Retrieve a specific indexed entry from the internal queue. Entries must be
112 * added to a feed container in order to be indexed.
113 *
114 * @param int $index
115 * @throws Exception\InvalidArgumentException
116 */
117 public function getEntry($index = 0)
118 {
119 if (isset($this->entries[$index])) {
120 return $this->entries[$index];
121 }
122 throw new Exception\InvalidArgumentException('Undefined index: ' . $index . '. Entry does not exist.');
123 }
124
125 /**
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.
129 *
130 * Using this method will alter the original indexation.
131 *
132 * @return Feed
133 */
134 public function orderByDate()
135 {
136 /**
137 * Could do with some improvement for performance perhaps
138 */
139 $timestamp = time();
140 $entries = [];
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();
146 }
147 $entries[$timestamp] = $entry;
148 }
149 krsort($entries, SORT_NUMERIC);
150 $this->entries = array_values($entries);
151
152 return $this;
153 }
154
155 /**
156 * Get the number of feed entries.
157 * Required by the Iterator interface.
158 *
159 * @return int
160 */
161 public function count()
162 {
163 return count($this->entries);
164 }
165
166 /**
167 * Return the current entry
168 *
169 * @return Entry
170 */
171 public function current()
172 {
173 return $this->entries[$this->key()];
174 }
175
176 /**
177 * Return the current feed key
178 *
179 * @return mixed
180 */
181 public function key()
182 {
183 return $this->entriesKey;
184 }
185
186 /**
187 * Move the feed pointer forward
188 *
189 * @return void
190 */
191 public function next()
192 {
193 ++$this->entriesKey;
194 }
195
196 /**
197 * Reset the pointer in the feed object
198 *
199 * @return void
200 */
201 public function rewind()
202 {
203 $this->entriesKey = 0;
204 }
205
206 /**
207 * Check to see if the iterator is still valid
208 *
209 * @return bool
210 */
211 public function valid()
212 {
213 return 0 <= $this->entriesKey && $this->entriesKey < $this->count();
214 }
215
216 /**
217 * Attempt to build and return the feed resulting from the data set
218 *
219 * @param string $type The feed type "rss" or "atom" to export as
220 * @param bool $ignoreExceptions
221 * @throws Exception\InvalidArgumentException
222 * @return string
223 */
224 public function export($type, $ignoreExceptions = false)
225 {
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".');
231 }
232 $renderClass = 'Zend\\Feed\\Writer\\Renderer\\Feed\\' . $type;
233 $renderer = new $renderClass($this);
234 if ($ignoreExceptions) {
235 $renderer->ignoreExceptions();
236 }
237 return $renderer->render()->saveXml();
238 }
239 }