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\Reader\Feed;
|
Chris@0
|
11
|
Chris@0
|
12 use DOMDocument;
|
Chris@0
|
13 use Zend\Feed\Reader;
|
Chris@0
|
14
|
Chris@0
|
15 /**
|
Chris@0
|
16 */
|
Chris@0
|
17 class Atom extends AbstractFeed
|
Chris@0
|
18 {
|
Chris@0
|
19 /**
|
Chris@0
|
20 * Constructor
|
Chris@0
|
21 *
|
Chris@0
|
22 * @param DOMDocument $dom
|
Chris@0
|
23 * @param string $type
|
Chris@0
|
24 */
|
Chris@0
|
25 public function __construct(DOMDocument $dom, $type = null)
|
Chris@0
|
26 {
|
Chris@0
|
27 parent::__construct($dom, $type);
|
Chris@0
|
28 $manager = Reader\Reader::getExtensionManager();
|
Chris@0
|
29
|
Chris@0
|
30 $atomFeed = $manager->get('Atom\Feed');
|
Chris@0
|
31 $atomFeed->setDomDocument($dom);
|
Chris@0
|
32 $atomFeed->setType($this->data['type']);
|
Chris@0
|
33 $atomFeed->setXpath($this->xpath);
|
Chris@0
|
34 $this->extensions['Atom\\Feed'] = $atomFeed;
|
Chris@0
|
35
|
Chris@0
|
36 $atomFeed = $manager->get('DublinCore\Feed');
|
Chris@0
|
37 $atomFeed->setDomDocument($dom);
|
Chris@0
|
38 $atomFeed->setType($this->data['type']);
|
Chris@0
|
39 $atomFeed->setXpath($this->xpath);
|
Chris@0
|
40 $this->extensions['DublinCore\\Feed'] = $atomFeed;
|
Chris@0
|
41
|
Chris@0
|
42 foreach ($this->extensions as $extension) {
|
Chris@0
|
43 $extension->setXpathPrefix('/atom:feed');
|
Chris@0
|
44 }
|
Chris@0
|
45 }
|
Chris@0
|
46
|
Chris@0
|
47 /**
|
Chris@0
|
48 * Get a single author
|
Chris@0
|
49 *
|
Chris@0
|
50 * @param int $index
|
Chris@0
|
51 * @return string|null
|
Chris@0
|
52 */
|
Chris@0
|
53 public function getAuthor($index = 0)
|
Chris@0
|
54 {
|
Chris@0
|
55 $authors = $this->getAuthors();
|
Chris@0
|
56
|
Chris@0
|
57 if (isset($authors[$index])) {
|
Chris@0
|
58 return $authors[$index];
|
Chris@0
|
59 }
|
Chris@0
|
60
|
Chris@0
|
61 return;
|
Chris@0
|
62 }
|
Chris@0
|
63
|
Chris@0
|
64 /**
|
Chris@0
|
65 * Get an array with feed authors
|
Chris@0
|
66 *
|
Chris@0
|
67 * @return array
|
Chris@0
|
68 */
|
Chris@0
|
69 public function getAuthors()
|
Chris@0
|
70 {
|
Chris@0
|
71 if (array_key_exists('authors', $this->data)) {
|
Chris@0
|
72 return $this->data['authors'];
|
Chris@0
|
73 }
|
Chris@0
|
74
|
Chris@0
|
75 $authors = $this->getExtension('Atom')->getAuthors();
|
Chris@0
|
76
|
Chris@0
|
77 $this->data['authors'] = $authors;
|
Chris@0
|
78
|
Chris@0
|
79 return $this->data['authors'];
|
Chris@0
|
80 }
|
Chris@0
|
81
|
Chris@0
|
82 /**
|
Chris@0
|
83 * Get the copyright entry
|
Chris@0
|
84 *
|
Chris@0
|
85 * @return string|null
|
Chris@0
|
86 */
|
Chris@0
|
87 public function getCopyright()
|
Chris@0
|
88 {
|
Chris@0
|
89 if (array_key_exists('copyright', $this->data)) {
|
Chris@0
|
90 return $this->data['copyright'];
|
Chris@0
|
91 }
|
Chris@0
|
92
|
Chris@0
|
93 $copyright = $this->getExtension('Atom')->getCopyright();
|
Chris@0
|
94
|
Chris@12
|
95 if (! $copyright) {
|
Chris@0
|
96 $copyright = null;
|
Chris@0
|
97 }
|
Chris@0
|
98
|
Chris@0
|
99 $this->data['copyright'] = $copyright;
|
Chris@0
|
100
|
Chris@0
|
101 return $this->data['copyright'];
|
Chris@0
|
102 }
|
Chris@0
|
103
|
Chris@0
|
104 /**
|
Chris@0
|
105 * Get the feed creation date
|
Chris@0
|
106 *
|
Chris@12
|
107 * @return \DateTime|null
|
Chris@0
|
108 */
|
Chris@0
|
109 public function getDateCreated()
|
Chris@0
|
110 {
|
Chris@0
|
111 if (array_key_exists('datecreated', $this->data)) {
|
Chris@0
|
112 return $this->data['datecreated'];
|
Chris@0
|
113 }
|
Chris@0
|
114
|
Chris@0
|
115 $dateCreated = $this->getExtension('Atom')->getDateCreated();
|
Chris@0
|
116
|
Chris@12
|
117 if (! $dateCreated) {
|
Chris@0
|
118 $dateCreated = null;
|
Chris@0
|
119 }
|
Chris@0
|
120
|
Chris@0
|
121 $this->data['datecreated'] = $dateCreated;
|
Chris@0
|
122
|
Chris@0
|
123 return $this->data['datecreated'];
|
Chris@0
|
124 }
|
Chris@0
|
125
|
Chris@0
|
126 /**
|
Chris@0
|
127 * Get the feed modification date
|
Chris@0
|
128 *
|
Chris@12
|
129 * @return \DateTime|null
|
Chris@0
|
130 */
|
Chris@0
|
131 public function getDateModified()
|
Chris@0
|
132 {
|
Chris@0
|
133 if (array_key_exists('datemodified', $this->data)) {
|
Chris@0
|
134 return $this->data['datemodified'];
|
Chris@0
|
135 }
|
Chris@0
|
136
|
Chris@0
|
137 $dateModified = $this->getExtension('Atom')->getDateModified();
|
Chris@0
|
138
|
Chris@12
|
139 if (! $dateModified) {
|
Chris@0
|
140 $dateModified = null;
|
Chris@0
|
141 }
|
Chris@0
|
142
|
Chris@0
|
143 $this->data['datemodified'] = $dateModified;
|
Chris@0
|
144
|
Chris@0
|
145 return $this->data['datemodified'];
|
Chris@0
|
146 }
|
Chris@0
|
147
|
Chris@0
|
148 /**
|
Chris@0
|
149 * Get the feed lastBuild date. This is not implemented in Atom.
|
Chris@0
|
150 *
|
Chris@0
|
151 * @return string|null
|
Chris@0
|
152 */
|
Chris@0
|
153 public function getLastBuildDate()
|
Chris@0
|
154 {
|
Chris@0
|
155 return;
|
Chris@0
|
156 }
|
Chris@0
|
157
|
Chris@0
|
158 /**
|
Chris@0
|
159 * Get the feed description
|
Chris@0
|
160 *
|
Chris@0
|
161 * @return string|null
|
Chris@0
|
162 */
|
Chris@0
|
163 public function getDescription()
|
Chris@0
|
164 {
|
Chris@0
|
165 if (array_key_exists('description', $this->data)) {
|
Chris@0
|
166 return $this->data['description'];
|
Chris@0
|
167 }
|
Chris@0
|
168
|
Chris@0
|
169 $description = $this->getExtension('Atom')->getDescription();
|
Chris@0
|
170
|
Chris@12
|
171 if (! $description) {
|
Chris@0
|
172 $description = null;
|
Chris@0
|
173 }
|
Chris@0
|
174
|
Chris@0
|
175 $this->data['description'] = $description;
|
Chris@0
|
176
|
Chris@0
|
177 return $this->data['description'];
|
Chris@0
|
178 }
|
Chris@0
|
179
|
Chris@0
|
180 /**
|
Chris@0
|
181 * Get the feed generator entry
|
Chris@0
|
182 *
|
Chris@0
|
183 * @return string|null
|
Chris@0
|
184 */
|
Chris@0
|
185 public function getGenerator()
|
Chris@0
|
186 {
|
Chris@0
|
187 if (array_key_exists('generator', $this->data)) {
|
Chris@0
|
188 return $this->data['generator'];
|
Chris@0
|
189 }
|
Chris@0
|
190
|
Chris@0
|
191 $generator = $this->getExtension('Atom')->getGenerator();
|
Chris@0
|
192
|
Chris@0
|
193 $this->data['generator'] = $generator;
|
Chris@0
|
194
|
Chris@0
|
195 return $this->data['generator'];
|
Chris@0
|
196 }
|
Chris@0
|
197
|
Chris@0
|
198 /**
|
Chris@0
|
199 * Get the feed ID
|
Chris@0
|
200 *
|
Chris@0
|
201 * @return string|null
|
Chris@0
|
202 */
|
Chris@0
|
203 public function getId()
|
Chris@0
|
204 {
|
Chris@0
|
205 if (array_key_exists('id', $this->data)) {
|
Chris@0
|
206 return $this->data['id'];
|
Chris@0
|
207 }
|
Chris@0
|
208
|
Chris@0
|
209 $id = $this->getExtension('Atom')->getId();
|
Chris@0
|
210
|
Chris@0
|
211 $this->data['id'] = $id;
|
Chris@0
|
212
|
Chris@0
|
213 return $this->data['id'];
|
Chris@0
|
214 }
|
Chris@0
|
215
|
Chris@0
|
216 /**
|
Chris@0
|
217 * Get the feed language
|
Chris@0
|
218 *
|
Chris@0
|
219 * @return string|null
|
Chris@0
|
220 */
|
Chris@0
|
221 public function getLanguage()
|
Chris@0
|
222 {
|
Chris@0
|
223 if (array_key_exists('language', $this->data)) {
|
Chris@0
|
224 return $this->data['language'];
|
Chris@0
|
225 }
|
Chris@0
|
226
|
Chris@0
|
227 $language = $this->getExtension('Atom')->getLanguage();
|
Chris@0
|
228
|
Chris@12
|
229 if (! $language) {
|
Chris@0
|
230 $language = $this->xpath->evaluate('string(//@xml:lang[1])');
|
Chris@0
|
231 }
|
Chris@0
|
232
|
Chris@12
|
233 if (! $language) {
|
Chris@0
|
234 $language = null;
|
Chris@0
|
235 }
|
Chris@0
|
236
|
Chris@0
|
237 $this->data['language'] = $language;
|
Chris@0
|
238
|
Chris@0
|
239 return $this->data['language'];
|
Chris@0
|
240 }
|
Chris@0
|
241
|
Chris@0
|
242 /**
|
Chris@0
|
243 * Get a link to the source website
|
Chris@0
|
244 *
|
Chris@0
|
245 * @return string|null
|
Chris@0
|
246 */
|
Chris@0
|
247 public function getBaseUrl()
|
Chris@0
|
248 {
|
Chris@0
|
249 if (array_key_exists('baseUrl', $this->data)) {
|
Chris@0
|
250 return $this->data['baseUrl'];
|
Chris@0
|
251 }
|
Chris@0
|
252
|
Chris@0
|
253 $baseUrl = $this->getExtension('Atom')->getBaseUrl();
|
Chris@0
|
254
|
Chris@0
|
255 $this->data['baseUrl'] = $baseUrl;
|
Chris@0
|
256
|
Chris@0
|
257 return $this->data['baseUrl'];
|
Chris@0
|
258 }
|
Chris@0
|
259
|
Chris@0
|
260 /**
|
Chris@0
|
261 * Get a link to the source website
|
Chris@0
|
262 *
|
Chris@0
|
263 * @return string|null
|
Chris@0
|
264 */
|
Chris@0
|
265 public function getLink()
|
Chris@0
|
266 {
|
Chris@0
|
267 if (array_key_exists('link', $this->data)) {
|
Chris@0
|
268 return $this->data['link'];
|
Chris@0
|
269 }
|
Chris@0
|
270
|
Chris@0
|
271 $link = $this->getExtension('Atom')->getLink();
|
Chris@0
|
272
|
Chris@0
|
273 $this->data['link'] = $link;
|
Chris@0
|
274
|
Chris@0
|
275 return $this->data['link'];
|
Chris@0
|
276 }
|
Chris@0
|
277
|
Chris@0
|
278 /**
|
Chris@0
|
279 * Get feed image data
|
Chris@0
|
280 *
|
Chris@0
|
281 * @return array|null
|
Chris@0
|
282 */
|
Chris@0
|
283 public function getImage()
|
Chris@0
|
284 {
|
Chris@0
|
285 if (array_key_exists('image', $this->data)) {
|
Chris@0
|
286 return $this->data['image'];
|
Chris@0
|
287 }
|
Chris@0
|
288
|
Chris@0
|
289 $link = $this->getExtension('Atom')->getImage();
|
Chris@0
|
290
|
Chris@0
|
291 $this->data['image'] = $link;
|
Chris@0
|
292
|
Chris@0
|
293 return $this->data['image'];
|
Chris@0
|
294 }
|
Chris@0
|
295
|
Chris@0
|
296 /**
|
Chris@0
|
297 * Get a link to the feed's XML Url
|
Chris@0
|
298 *
|
Chris@0
|
299 * @return string|null
|
Chris@0
|
300 */
|
Chris@0
|
301 public function getFeedLink()
|
Chris@0
|
302 {
|
Chris@0
|
303 if (array_key_exists('feedlink', $this->data)) {
|
Chris@0
|
304 return $this->data['feedlink'];
|
Chris@0
|
305 }
|
Chris@0
|
306
|
Chris@0
|
307 $link = $this->getExtension('Atom')->getFeedLink();
|
Chris@0
|
308
|
Chris@0
|
309 if ($link === null || empty($link)) {
|
Chris@0
|
310 $link = $this->getOriginalSourceUri();
|
Chris@0
|
311 }
|
Chris@0
|
312
|
Chris@0
|
313 $this->data['feedlink'] = $link;
|
Chris@0
|
314
|
Chris@0
|
315 return $this->data['feedlink'];
|
Chris@0
|
316 }
|
Chris@0
|
317
|
Chris@0
|
318 /**
|
Chris@0
|
319 * Get the feed title
|
Chris@0
|
320 *
|
Chris@0
|
321 * @return string|null
|
Chris@0
|
322 */
|
Chris@0
|
323 public function getTitle()
|
Chris@0
|
324 {
|
Chris@0
|
325 if (array_key_exists('title', $this->data)) {
|
Chris@0
|
326 return $this->data['title'];
|
Chris@0
|
327 }
|
Chris@0
|
328
|
Chris@0
|
329 $title = $this->getExtension('Atom')->getTitle();
|
Chris@0
|
330
|
Chris@0
|
331 $this->data['title'] = $title;
|
Chris@0
|
332
|
Chris@0
|
333 return $this->data['title'];
|
Chris@0
|
334 }
|
Chris@0
|
335
|
Chris@0
|
336 /**
|
Chris@0
|
337 * Get an array of any supported Pusubhubbub endpoints
|
Chris@0
|
338 *
|
Chris@0
|
339 * @return array|null
|
Chris@0
|
340 */
|
Chris@0
|
341 public function getHubs()
|
Chris@0
|
342 {
|
Chris@0
|
343 if (array_key_exists('hubs', $this->data)) {
|
Chris@0
|
344 return $this->data['hubs'];
|
Chris@0
|
345 }
|
Chris@0
|
346
|
Chris@0
|
347 $hubs = $this->getExtension('Atom')->getHubs();
|
Chris@0
|
348
|
Chris@0
|
349 $this->data['hubs'] = $hubs;
|
Chris@0
|
350
|
Chris@0
|
351 return $this->data['hubs'];
|
Chris@0
|
352 }
|
Chris@0
|
353
|
Chris@0
|
354 /**
|
Chris@0
|
355 * Get all categories
|
Chris@0
|
356 *
|
Chris@0
|
357 * @return Reader\Collection\Category
|
Chris@0
|
358 */
|
Chris@0
|
359 public function getCategories()
|
Chris@0
|
360 {
|
Chris@0
|
361 if (array_key_exists('categories', $this->data)) {
|
Chris@0
|
362 return $this->data['categories'];
|
Chris@0
|
363 }
|
Chris@0
|
364
|
Chris@0
|
365 $categoryCollection = $this->getExtension('Atom')->getCategories();
|
Chris@0
|
366
|
Chris@0
|
367 if (count($categoryCollection) == 0) {
|
Chris@0
|
368 $categoryCollection = $this->getExtension('DublinCore')->getCategories();
|
Chris@0
|
369 }
|
Chris@0
|
370
|
Chris@0
|
371 $this->data['categories'] = $categoryCollection;
|
Chris@0
|
372
|
Chris@0
|
373 return $this->data['categories'];
|
Chris@0
|
374 }
|
Chris@0
|
375
|
Chris@0
|
376 /**
|
Chris@0
|
377 * Read all entries to the internal entries array
|
Chris@0
|
378 *
|
Chris@0
|
379 * @return void
|
Chris@0
|
380 */
|
Chris@0
|
381 protected function indexEntries()
|
Chris@0
|
382 {
|
Chris@0
|
383 if ($this->getType() == Reader\Reader::TYPE_ATOM_10 ||
|
Chris@0
|
384 $this->getType() == Reader\Reader::TYPE_ATOM_03) {
|
Chris@0
|
385 $entries = $this->xpath->evaluate('//atom:entry');
|
Chris@0
|
386
|
Chris@0
|
387 foreach ($entries as $index => $entry) {
|
Chris@0
|
388 $this->entries[$index] = $entry;
|
Chris@0
|
389 }
|
Chris@0
|
390 }
|
Chris@0
|
391 }
|
Chris@0
|
392
|
Chris@0
|
393 /**
|
Chris@0
|
394 * Register the default namespaces for the current feed format
|
Chris@0
|
395 *
|
Chris@0
|
396 */
|
Chris@0
|
397 protected function registerNamespaces()
|
Chris@0
|
398 {
|
Chris@0
|
399 switch ($this->data['type']) {
|
Chris@0
|
400 case Reader\Reader::TYPE_ATOM_03:
|
Chris@0
|
401 $this->xpath->registerNamespace('atom', Reader\Reader::NAMESPACE_ATOM_03);
|
Chris@0
|
402 break;
|
Chris@0
|
403 case Reader\Reader::TYPE_ATOM_10:
|
Chris@0
|
404 default:
|
Chris@0
|
405 $this->xpath->registerNamespace('atom', Reader\Reader::NAMESPACE_ATOM_10);
|
Chris@0
|
406 }
|
Chris@0
|
407 }
|
Chris@0
|
408 }
|