Version 1
[yaffs-website] / vendor / zendframework / zend-feed / src / Reader / Entry / Atom.php
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\Reader\Entry;
11
12 use DOMElement;
13 use DOMXPath;
14 use Zend\Feed\Reader;
15
16 class Atom extends AbstractEntry implements EntryInterface
17 {
18     /**
19      * XPath query
20      *
21      * @var string
22      */
23     protected $xpathQuery = '';
24
25     /**
26      * Constructor
27      *
28      * @param  DOMElement $entry
29      * @param  int $entryKey
30      * @param  string $type
31      */
32     public function __construct(DOMElement $entry, $entryKey, $type = null)
33     {
34         parent::__construct($entry, $entryKey, $type);
35
36         // Everyone by now should know XPath indices start from 1 not 0
37         $this->xpathQuery = '//atom:entry[' . ($this->entryKey + 1) . ']';
38
39         $manager    = Reader\Reader::getExtensionManager();
40         $extensions = ['Atom\Entry', 'Thread\Entry', 'DublinCore\Entry'];
41
42         foreach ($extensions as $name) {
43             $extension = $manager->get($name);
44             $extension->setEntryElement($entry);
45             $extension->setEntryKey($entryKey);
46             $extension->setType($type);
47             $this->extensions[$name] = $extension;
48         }
49     }
50
51     /**
52      * Get the specified author
53      *
54      * @param  int $index
55      * @return string|null
56      */
57     public function getAuthor($index = 0)
58     {
59         $authors = $this->getAuthors();
60
61         if (isset($authors[$index])) {
62             return $authors[$index];
63         }
64
65         return;
66     }
67
68     /**
69      * Get an array with feed authors
70      *
71      * @return array
72      */
73     public function getAuthors()
74     {
75         if (array_key_exists('authors', $this->data)) {
76             return $this->data['authors'];
77         }
78
79         $people = $this->getExtension('Atom')->getAuthors();
80
81         $this->data['authors'] = $people;
82
83         return $this->data['authors'];
84     }
85
86     /**
87      * Get the entry content
88      *
89      * @return string
90      */
91     public function getContent()
92     {
93         if (array_key_exists('content', $this->data)) {
94             return $this->data['content'];
95         }
96
97         $content = $this->getExtension('Atom')->getContent();
98
99         $this->data['content'] = $content;
100
101         return $this->data['content'];
102     }
103
104     /**
105      * Get the entry creation date
106      *
107      * @return \DateTime
108      */
109     public function getDateCreated()
110     {
111         if (array_key_exists('datecreated', $this->data)) {
112             return $this->data['datecreated'];
113         }
114
115         $dateCreated = $this->getExtension('Atom')->getDateCreated();
116
117         $this->data['datecreated'] = $dateCreated;
118
119         return $this->data['datecreated'];
120     }
121
122     /**
123      * Get the entry modification date
124      *
125      * @return \DateTime
126      */
127     public function getDateModified()
128     {
129         if (array_key_exists('datemodified', $this->data)) {
130             return $this->data['datemodified'];
131         }
132
133         $dateModified = $this->getExtension('Atom')->getDateModified();
134
135         $this->data['datemodified'] = $dateModified;
136
137         return $this->data['datemodified'];
138     }
139
140     /**
141      * Get the entry description
142      *
143      * @return string
144      */
145     public function getDescription()
146     {
147         if (array_key_exists('description', $this->data)) {
148             return $this->data['description'];
149         }
150
151         $description = $this->getExtension('Atom')->getDescription();
152
153         $this->data['description'] = $description;
154
155         return $this->data['description'];
156     }
157
158     /**
159      * Get the entry enclosure
160      *
161      * @return string
162      */
163     public function getEnclosure()
164     {
165         if (array_key_exists('enclosure', $this->data)) {
166             return $this->data['enclosure'];
167         }
168
169         $enclosure = $this->getExtension('Atom')->getEnclosure();
170
171         $this->data['enclosure'] = $enclosure;
172
173         return $this->data['enclosure'];
174     }
175
176     /**
177      * Get the entry ID
178      *
179      * @return string
180      */
181     public function getId()
182     {
183         if (array_key_exists('id', $this->data)) {
184             return $this->data['id'];
185         }
186
187         $id = $this->getExtension('Atom')->getId();
188
189         $this->data['id'] = $id;
190
191         return $this->data['id'];
192     }
193
194     /**
195      * Get a specific link
196      *
197      * @param  int $index
198      * @return string
199      */
200     public function getLink($index = 0)
201     {
202         if (! array_key_exists('links', $this->data)) {
203             $this->getLinks();
204         }
205
206         if (isset($this->data['links'][$index])) {
207             return $this->data['links'][$index];
208         }
209
210         return;
211     }
212
213     /**
214      * Get all links
215      *
216      * @return array
217      */
218     public function getLinks()
219     {
220         if (array_key_exists('links', $this->data)) {
221             return $this->data['links'];
222         }
223
224         $links = $this->getExtension('Atom')->getLinks();
225
226         $this->data['links'] = $links;
227
228         return $this->data['links'];
229     }
230
231     /**
232      * Get a permalink to the entry
233      *
234      * @return string
235      */
236     public function getPermalink()
237     {
238         return $this->getLink(0);
239     }
240
241     /**
242      * Get the entry title
243      *
244      * @return string
245      */
246     public function getTitle()
247     {
248         if (array_key_exists('title', $this->data)) {
249             return $this->data['title'];
250         }
251
252         $title = $this->getExtension('Atom')->getTitle();
253
254         $this->data['title'] = $title;
255
256         return $this->data['title'];
257     }
258
259     /**
260      * Get the number of comments/replies for current entry
261      *
262      * @return int
263      */
264     public function getCommentCount()
265     {
266         if (array_key_exists('commentcount', $this->data)) {
267             return $this->data['commentcount'];
268         }
269
270         $commentcount = $this->getExtension('Thread')->getCommentCount();
271
272         if (! $commentcount) {
273             $commentcount = $this->getExtension('Atom')->getCommentCount();
274         }
275
276         $this->data['commentcount'] = $commentcount;
277
278         return $this->data['commentcount'];
279     }
280
281     /**
282      * Returns a URI pointing to the HTML page where comments can be made on this entry
283      *
284      * @return string
285      */
286     public function getCommentLink()
287     {
288         if (array_key_exists('commentlink', $this->data)) {
289             return $this->data['commentlink'];
290         }
291
292         $commentlink = $this->getExtension('Atom')->getCommentLink();
293
294         $this->data['commentlink'] = $commentlink;
295
296         return $this->data['commentlink'];
297     }
298
299     /**
300      * Returns a URI pointing to a feed of all comments for this entry
301      *
302      * @return string
303      */
304     public function getCommentFeedLink()
305     {
306         if (array_key_exists('commentfeedlink', $this->data)) {
307             return $this->data['commentfeedlink'];
308         }
309
310         $commentfeedlink = $this->getExtension('Atom')->getCommentFeedLink();
311
312         $this->data['commentfeedlink'] = $commentfeedlink;
313
314         return $this->data['commentfeedlink'];
315     }
316
317     /**
318      * Get category data as a Reader\Reader_Collection_Category object
319      *
320      * @return Reader\Collection\Category
321      */
322     public function getCategories()
323     {
324         if (array_key_exists('categories', $this->data)) {
325             return $this->data['categories'];
326         }
327
328         $categoryCollection = $this->getExtension('Atom')->getCategories();
329
330         if (count($categoryCollection) == 0) {
331             $categoryCollection = $this->getExtension('DublinCore')->getCategories();
332         }
333
334         $this->data['categories'] = $categoryCollection;
335
336         return $this->data['categories'];
337     }
338
339     /**
340      * Get source feed metadata from the entry
341      *
342      * @return Reader\Feed\Atom\Source|null
343      */
344     public function getSource()
345     {
346         if (array_key_exists('source', $this->data)) {
347             return $this->data['source'];
348         }
349
350         $source = $this->getExtension('Atom')->getSource();
351
352         $this->data['source'] = $source;
353
354         return $this->data['source'];
355     }
356
357     /**
358      * Set the XPath query (incl. on all Extensions)
359      *
360      * @param DOMXPath $xpath
361      * @return void
362      */
363     public function setXpath(DOMXPath $xpath)
364     {
365         parent::setXpath($xpath);
366         foreach ($this->extensions as $extension) {
367             $extension->setXpath($this->xpath);
368         }
369     }
370 }