349d65cd652d6f039c5000cb836471dfacd1437e
[yaffs-website] / vendor / zendframework / zend-feed / src / Writer / Renderer / Feed / AbstractAtom.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\Writer\Renderer\Feed;
11
12 use DateTime;
13 use DOMDocument;
14 use DOMElement;
15 use Zend\Feed\Writer;
16 use Zend\Feed\Writer\Renderer;
17 use Zend\Feed\Writer\Version;
18
19 /**
20 */
21 class AbstractAtom extends Renderer\AbstractRenderer
22 {
23     /**
24      * Constructor
25      *
26      * @param  Writer\AbstractFeed $container
27      */
28     public function __construct($container)
29     {
30         parent::__construct($container);
31     }
32
33     /**
34      * Set feed language
35      *
36      * @param  DOMDocument $dom
37      * @param  DOMElement $root
38      * @return void
39      */
40     // @codingStandardsIgnoreStart
41     protected function _setLanguage(DOMDocument $dom, DOMElement $root)
42     {
43         // @codingStandardsIgnoreEnd
44         if ($this->getDataContainer()->getLanguage()) {
45             $root->setAttribute('xml:lang', $this->getDataContainer()
46                 ->getLanguage());
47         }
48     }
49
50     /**
51      * Set feed title
52      *
53      * @param  DOMDocument $dom
54      * @param  DOMElement $root
55      * @return void
56      * @throws Writer\Exception\InvalidArgumentException
57      */
58     // @codingStandardsIgnoreStart
59     protected function _setTitle(DOMDocument $dom, DOMElement $root)
60     {
61         // @codingStandardsIgnoreEnd
62         if (! $this->getDataContainer()->getTitle()) {
63             $message = 'Atom 1.0 feed elements MUST contain exactly one'
64             . ' atom:title element but a title has not been set';
65             $exception = new Writer\Exception\InvalidArgumentException($message);
66             if (! $this->ignoreExceptions) {
67                 throw $exception;
68             } else {
69                 $this->exceptions[] = $exception;
70                 return;
71             }
72         }
73
74         $title = $dom->createElement('title');
75         $root->appendChild($title);
76         $title->setAttribute('type', 'text');
77         $text = $dom->createTextNode($this->getDataContainer()->getTitle());
78         $title->appendChild($text);
79     }
80
81     /**
82      * Set feed description
83      *
84      * @param  DOMDocument $dom
85      * @param  DOMElement $root
86      * @return void
87      */
88     // @codingStandardsIgnoreStart
89     protected function _setDescription(DOMDocument $dom, DOMElement $root)
90     {
91         // @codingStandardsIgnoreEnd
92         if (! $this->getDataContainer()->getDescription()) {
93             return;
94         }
95         $subtitle = $dom->createElement('subtitle');
96         $root->appendChild($subtitle);
97         $subtitle->setAttribute('type', 'text');
98         $text = $dom->createTextNode($this->getDataContainer()->getDescription());
99         $subtitle->appendChild($text);
100     }
101
102     /**
103      * Set date feed was last modified
104      *
105      * @param  DOMDocument $dom
106      * @param  DOMElement $root
107      * @return void
108      * @throws Writer\Exception\InvalidArgumentException
109      */
110     // @codingStandardsIgnoreStart
111     protected function _setDateModified(DOMDocument $dom, DOMElement $root)
112     {
113         // @codingStandardsIgnoreEnd
114         if (! $this->getDataContainer()->getDateModified()) {
115             $message = 'Atom 1.0 feed elements MUST contain exactly one'
116             . ' atom:updated element but a modification date has not been set';
117             $exception = new Writer\Exception\InvalidArgumentException($message);
118             if (! $this->ignoreExceptions) {
119                 throw $exception;
120             } else {
121                 $this->exceptions[] = $exception;
122                 return;
123             }
124         }
125
126         $updated = $dom->createElement('updated');
127         $root->appendChild($updated);
128         $text = $dom->createTextNode(
129             $this->getDataContainer()->getDateModified()->format(DateTime::ATOM)
130         );
131         $updated->appendChild($text);
132     }
133
134     /**
135      * Set feed generator string
136      *
137      * @param  DOMDocument $dom
138      * @param  DOMElement $root
139      * @return void
140      */
141     // @codingStandardsIgnoreStart
142     protected function _setGenerator(DOMDocument $dom, DOMElement $root)
143     {
144         // @codingStandardsIgnoreEnd
145         if (! $this->getDataContainer()->getGenerator()) {
146             $this->getDataContainer()->setGenerator(
147                 'Zend_Feed_Writer',
148                 Version::VERSION,
149                 'http://framework.zend.com'
150             );
151         }
152
153         $gdata = $this->getDataContainer()->getGenerator();
154         $generator = $dom->createElement('generator');
155         $root->appendChild($generator);
156         $text = $dom->createTextNode($gdata['name']);
157         $generator->appendChild($text);
158         if (array_key_exists('uri', $gdata)) {
159             $generator->setAttribute('uri', $gdata['uri']);
160         }
161         if (array_key_exists('version', $gdata)) {
162             $generator->setAttribute('version', $gdata['version']);
163         }
164     }
165
166     /**
167      * Set link to feed
168      *
169      * @param  DOMDocument $dom
170      * @param  DOMElement $root
171      * @return void
172      */
173     // @codingStandardsIgnoreStart
174     protected function _setLink(DOMDocument $dom, DOMElement $root)
175     {
176         // @codingStandardsIgnoreEnd
177         if (! $this->getDataContainer()->getLink()) {
178             return;
179         }
180         $link = $dom->createElement('link');
181         $root->appendChild($link);
182         $link->setAttribute('rel', 'alternate');
183         $link->setAttribute('type', 'text/html');
184         $link->setAttribute('href', $this->getDataContainer()->getLink());
185     }
186
187     /**
188      * Set feed links
189      *
190      * @param  DOMDocument $dom
191      * @param  DOMElement $root
192      * @return void
193      * @throws Writer\Exception\InvalidArgumentException
194      */
195     // @codingStandardsIgnoreStart
196     protected function _setFeedLinks(DOMDocument $dom, DOMElement $root)
197     {
198         // @codingStandardsIgnoreEnd
199         $flinks = $this->getDataContainer()->getFeedLinks();
200         if (! $flinks || ! array_key_exists('atom', $flinks)) {
201             $message = 'Atom 1.0 feed elements SHOULD contain one atom:link '
202             . 'element with a rel attribute value of "self".  This is the '
203             . 'preferred URI for retrieving Atom Feed Documents representing '
204             . 'this Atom feed but a feed link has not been set';
205             $exception = new Writer\Exception\InvalidArgumentException($message);
206             if (! $this->ignoreExceptions) {
207                 throw $exception;
208             } else {
209                 $this->exceptions[] = $exception;
210                 return;
211             }
212         }
213
214         foreach ($flinks as $type => $href) {
215             $mime = 'application/' . strtolower($type) . '+xml';
216             $flink = $dom->createElement('link');
217             $root->appendChild($flink);
218             $flink->setAttribute('rel', 'self');
219             $flink->setAttribute('type', $mime);
220             $flink->setAttribute('href', $href);
221         }
222     }
223
224     /**
225      * Set feed authors
226      *
227      * @param  DOMDocument $dom
228      * @param  DOMElement $root
229      * @return void
230      */
231     // @codingStandardsIgnoreStart
232     protected function _setAuthors(DOMDocument $dom, DOMElement $root)
233     {
234         // @codingStandardsIgnoreEnd
235         $authors = $this->container->getAuthors();
236         if (! $authors || empty($authors)) {
237             /**
238              * Technically we should defer an exception until we can check
239              * that all entries contain an author. If any entry is missing
240              * an author, then a missing feed author element is invalid
241              */
242             return;
243         }
244         foreach ($authors as $data) {
245             $author = $this->dom->createElement('author');
246             $name = $this->dom->createElement('name');
247             $author->appendChild($name);
248             $root->appendChild($author);
249             $text = $dom->createTextNode($data['name']);
250             $name->appendChild($text);
251             if (array_key_exists('email', $data)) {
252                 $email = $this->dom->createElement('email');
253                 $author->appendChild($email);
254                 $text = $dom->createTextNode($data['email']);
255                 $email->appendChild($text);
256             }
257             if (array_key_exists('uri', $data)) {
258                 $uri = $this->dom->createElement('uri');
259                 $author->appendChild($uri);
260                 $text = $dom->createTextNode($data['uri']);
261                 $uri->appendChild($text);
262             }
263         }
264     }
265
266     /**
267      * Set feed identifier
268      *
269      * @param  DOMDocument $dom
270      * @param  DOMElement $root
271      * @return void
272      * @throws Writer\Exception\InvalidArgumentException
273      */
274     // @codingStandardsIgnoreStart
275     protected function _setId(DOMDocument $dom, DOMElement $root)
276     {
277         // @codingStandardsIgnoreEnd
278         if (! $this->getDataContainer()->getId()
279         && ! $this->getDataContainer()->getLink()) {
280             $message = 'Atom 1.0 feed elements MUST contain exactly one '
281             . 'atom:id element, or as an alternative, we can use the same '
282             . 'value as atom:link however neither a suitable link nor an '
283             . 'id have been set';
284             $exception = new Writer\Exception\InvalidArgumentException($message);
285             if (! $this->ignoreExceptions) {
286                 throw $exception;
287             } else {
288                 $this->exceptions[] = $exception;
289                 return;
290             }
291         }
292
293         if (! $this->getDataContainer()->getId()) {
294             $this->getDataContainer()->setId(
295                 $this->getDataContainer()->getLink()
296             );
297         }
298         $id = $dom->createElement('id');
299         $root->appendChild($id);
300         $text = $dom->createTextNode($this->getDataContainer()->getId());
301         $id->appendChild($text);
302     }
303
304     /**
305      * Set feed copyright
306      *
307      * @param  DOMDocument $dom
308      * @param  DOMElement $root
309      * @return void
310      */
311     // @codingStandardsIgnoreStart
312     protected function _setCopyright(DOMDocument $dom, DOMElement $root)
313     {
314         // @codingStandardsIgnoreEnd
315         $copyright = $this->getDataContainer()->getCopyright();
316         if (! $copyright) {
317             return;
318         }
319         $copy = $dom->createElement('rights');
320         $root->appendChild($copy);
321         $text = $dom->createTextNode($copyright);
322         $copy->appendChild($text);
323     }
324
325     /**
326      * Set feed level logo (image)
327      *
328      * @param DOMDocument $dom
329      * @param DOMElement $root
330      * @return void
331      */
332     // @codingStandardsIgnoreStart
333     protected function _setImage(DOMDocument $dom, DOMElement $root)
334     {
335         // @codingStandardsIgnoreEnd
336         $image = $this->getDataContainer()->getImage();
337         if (! $image) {
338             return;
339         }
340         $img = $dom->createElement('logo');
341         $root->appendChild($img);
342         $text = $dom->createTextNode($image['uri']);
343         $img->appendChild($text);
344     }
345
346     /**
347      * Set date feed was created
348      *
349      * @param  DOMDocument $dom
350      * @param  DOMElement $root
351      * @return void
352      */
353     // @codingStandardsIgnoreStart
354     protected function _setDateCreated(DOMDocument $dom, DOMElement $root)
355     {
356         // @codingStandardsIgnoreEnd
357         if (! $this->getDataContainer()->getDateCreated()) {
358             return;
359         }
360         if (! $this->getDataContainer()->getDateModified()) {
361             $this->getDataContainer()->setDateModified(
362                 $this->getDataContainer()->getDateCreated()
363             );
364         }
365     }
366
367     /**
368      * Set base URL to feed links
369      *
370      * @param  DOMDocument $dom
371      * @param  DOMElement $root
372      * @return void
373      */
374     // @codingStandardsIgnoreStart
375     protected function _setBaseUrl(DOMDocument $dom, DOMElement $root)
376     {
377         // @codingStandardsIgnoreEnd
378         $baseUrl = $this->getDataContainer()->getBaseUrl();
379         if (! $baseUrl) {
380             return;
381         }
382         $root->setAttribute('xml:base', $baseUrl);
383     }
384
385     /**
386      * Set hubs to which this feed pushes
387      *
388      * @param  DOMDocument $dom
389      * @param  DOMElement $root
390      * @return void
391      */
392     // @codingStandardsIgnoreStart
393     protected function _setHubs(DOMDocument $dom, DOMElement $root)
394     {
395         // @codingStandardsIgnoreEnd
396         $hubs = $this->getDataContainer()->getHubs();
397         if (! $hubs) {
398             return;
399         }
400         foreach ($hubs as $hubUrl) {
401             $hub = $dom->createElement('link');
402             $hub->setAttribute('rel', 'hub');
403             $hub->setAttribute('href', $hubUrl);
404             $root->appendChild($hub);
405         }
406     }
407
408     /**
409      * Set feed categories
410      *
411      * @param  DOMDocument $dom
412      * @param  DOMElement $root
413      * @return void
414      */
415     // @codingStandardsIgnoreStart
416     protected function _setCategories(DOMDocument $dom, DOMElement $root)
417     {
418         // @codingStandardsIgnoreEnd
419         $categories = $this->getDataContainer()->getCategories();
420         if (! $categories) {
421             return;
422         }
423         foreach ($categories as $cat) {
424             $category = $dom->createElement('category');
425             $category->setAttribute('term', $cat['term']);
426             if (isset($cat['label'])) {
427                 $category->setAttribute('label', $cat['label']);
428             } else {
429                 $category->setAttribute('label', $cat['term']);
430             }
431             if (isset($cat['scheme'])) {
432                 $category->setAttribute('scheme', $cat['scheme']);
433             }
434             $root->appendChild($category);
435         }
436     }
437 }