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@12
|
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@17
|
115 * @return Entry
|
Chris@0
|
116 * @throws Exception\InvalidArgumentException
|
Chris@0
|
117 */
|
Chris@0
|
118 public function getEntry($index = 0)
|
Chris@0
|
119 {
|
Chris@0
|
120 if (isset($this->entries[$index])) {
|
Chris@0
|
121 return $this->entries[$index];
|
Chris@0
|
122 }
|
Chris@0
|
123 throw new Exception\InvalidArgumentException('Undefined index: ' . $index . '. Entry does not exist.');
|
Chris@0
|
124 }
|
Chris@0
|
125
|
Chris@0
|
126 /**
|
Chris@0
|
127 * Orders all indexed entries by date, thus offering date ordered readable
|
Chris@0
|
128 * content where a parser (or Homo Sapien) ignores the generic rule that
|
Chris@0
|
129 * XML element order is irrelevant and has no intrinsic meaning.
|
Chris@0
|
130 *
|
Chris@0
|
131 * Using this method will alter the original indexation.
|
Chris@0
|
132 *
|
Chris@0
|
133 * @return Feed
|
Chris@0
|
134 */
|
Chris@0
|
135 public function orderByDate()
|
Chris@0
|
136 {
|
Chris@0
|
137 /**
|
Chris@0
|
138 * Could do with some improvement for performance perhaps
|
Chris@0
|
139 */
|
Chris@0
|
140 $timestamp = time();
|
Chris@0
|
141 $entries = [];
|
Chris@0
|
142 foreach ($this->entries as $entry) {
|
Chris@0
|
143 if ($entry->getDateModified()) {
|
Chris@0
|
144 $timestamp = (int) $entry->getDateModified()->getTimestamp();
|
Chris@0
|
145 } elseif ($entry->getDateCreated()) {
|
Chris@0
|
146 $timestamp = (int) $entry->getDateCreated()->getTimestamp();
|
Chris@0
|
147 }
|
Chris@0
|
148 $entries[$timestamp] = $entry;
|
Chris@0
|
149 }
|
Chris@0
|
150 krsort($entries, SORT_NUMERIC);
|
Chris@0
|
151 $this->entries = array_values($entries);
|
Chris@0
|
152
|
Chris@0
|
153 return $this;
|
Chris@0
|
154 }
|
Chris@0
|
155
|
Chris@0
|
156 /**
|
Chris@0
|
157 * Get the number of feed entries.
|
Chris@0
|
158 * Required by the Iterator interface.
|
Chris@0
|
159 *
|
Chris@0
|
160 * @return int
|
Chris@0
|
161 */
|
Chris@0
|
162 public function count()
|
Chris@0
|
163 {
|
Chris@0
|
164 return count($this->entries);
|
Chris@0
|
165 }
|
Chris@0
|
166
|
Chris@0
|
167 /**
|
Chris@0
|
168 * Return the current entry
|
Chris@0
|
169 *
|
Chris@0
|
170 * @return Entry
|
Chris@0
|
171 */
|
Chris@0
|
172 public function current()
|
Chris@0
|
173 {
|
Chris@0
|
174 return $this->entries[$this->key()];
|
Chris@0
|
175 }
|
Chris@0
|
176
|
Chris@0
|
177 /**
|
Chris@0
|
178 * Return the current feed key
|
Chris@0
|
179 *
|
Chris@0
|
180 * @return mixed
|
Chris@0
|
181 */
|
Chris@0
|
182 public function key()
|
Chris@0
|
183 {
|
Chris@0
|
184 return $this->entriesKey;
|
Chris@0
|
185 }
|
Chris@0
|
186
|
Chris@0
|
187 /**
|
Chris@0
|
188 * Move the feed pointer forward
|
Chris@0
|
189 *
|
Chris@0
|
190 * @return void
|
Chris@0
|
191 */
|
Chris@0
|
192 public function next()
|
Chris@0
|
193 {
|
Chris@0
|
194 ++$this->entriesKey;
|
Chris@0
|
195 }
|
Chris@0
|
196
|
Chris@0
|
197 /**
|
Chris@0
|
198 * Reset the pointer in the feed object
|
Chris@0
|
199 *
|
Chris@0
|
200 * @return void
|
Chris@0
|
201 */
|
Chris@0
|
202 public function rewind()
|
Chris@0
|
203 {
|
Chris@0
|
204 $this->entriesKey = 0;
|
Chris@0
|
205 }
|
Chris@0
|
206
|
Chris@0
|
207 /**
|
Chris@0
|
208 * Check to see if the iterator is still valid
|
Chris@0
|
209 *
|
Chris@0
|
210 * @return bool
|
Chris@0
|
211 */
|
Chris@0
|
212 public function valid()
|
Chris@0
|
213 {
|
Chris@0
|
214 return 0 <= $this->entriesKey && $this->entriesKey < $this->count();
|
Chris@0
|
215 }
|
Chris@0
|
216
|
Chris@0
|
217 /**
|
Chris@0
|
218 * Attempt to build and return the feed resulting from the data set
|
Chris@0
|
219 *
|
Chris@0
|
220 * @param string $type The feed type "rss" or "atom" to export as
|
Chris@0
|
221 * @param bool $ignoreExceptions
|
Chris@0
|
222 * @throws Exception\InvalidArgumentException
|
Chris@0
|
223 * @return string
|
Chris@0
|
224 */
|
Chris@0
|
225 public function export($type, $ignoreExceptions = false)
|
Chris@0
|
226 {
|
Chris@0
|
227 $this->setType(strtolower($type));
|
Chris@0
|
228 $type = ucfirst($this->getType());
|
Chris@0
|
229 if ($type !== 'Rss' && $type !== 'Atom') {
|
Chris@0
|
230 throw new Exception\InvalidArgumentException('Invalid feed type specified: ' . $type . '.'
|
Chris@0
|
231 . ' Should be one of "rss" or "atom".');
|
Chris@0
|
232 }
|
Chris@0
|
233 $renderClass = 'Zend\\Feed\\Writer\\Renderer\\Feed\\' . $type;
|
Chris@0
|
234 $renderer = new $renderClass($this);
|
Chris@0
|
235 if ($ignoreExceptions) {
|
Chris@0
|
236 $renderer->ignoreExceptions();
|
Chris@0
|
237 }
|
Chris@0
|
238 return $renderer->render()->saveXml();
|
Chris@0
|
239 }
|
Chris@0
|
240 }
|