4c99936a604a5b5ef32e58e0c492e08043d9b58a
[yaffs-website] / vendor / zendframework / zend-feed / src / Writer / Renderer / Entry / Rss.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\Entry;
11
12 use DateTime;
13 use DOMDocument;
14 use DOMElement;
15 use Zend\Feed\Uri;
16 use Zend\Feed\Writer;
17 use Zend\Feed\Writer\Renderer;
18
19 /**
20 */
21 class Rss extends Renderer\AbstractRenderer implements Renderer\RendererInterface
22 {
23     /**
24      * Constructor
25      *
26      * @param  Writer\Entry $container
27      */
28     public function __construct(Writer\Entry $container)
29     {
30         parent::__construct($container);
31     }
32
33     /**
34      * Render RSS entry
35      *
36      * @return Rss
37      */
38     public function render()
39     {
40         $this->dom = new DOMDocument('1.0', $this->container->getEncoding());
41         $this->dom->formatOutput = true;
42         $this->dom->substituteEntities = false;
43         $entry = $this->dom->createElement('item');
44         $this->dom->appendChild($entry);
45
46         $this->_setTitle($this->dom, $entry);
47         $this->_setDescription($this->dom, $entry);
48         $this->_setDateCreated($this->dom, $entry);
49         $this->_setDateModified($this->dom, $entry);
50         $this->_setLink($this->dom, $entry);
51         $this->_setId($this->dom, $entry);
52         $this->_setAuthors($this->dom, $entry);
53         $this->_setEnclosure($this->dom, $entry);
54         $this->_setCommentLink($this->dom, $entry);
55         $this->_setCategories($this->dom, $entry);
56         foreach ($this->extensions as $ext) {
57             $ext->setType($this->getType());
58             $ext->setRootElement($this->getRootElement());
59             $ext->setDOMDocument($this->getDOMDocument(), $entry);
60             $ext->render();
61         }
62
63         return $this;
64     }
65
66     /**
67      * Set entry title
68      *
69      * @param  DOMDocument $dom
70      * @param  DOMElement $root
71      * @return void
72      * @throws Writer\Exception\InvalidArgumentException
73      */
74     // @codingStandardsIgnoreStart
75     protected function _setTitle(DOMDocument $dom, DOMElement $root)
76     {
77         // @codingStandardsIgnoreEnd
78         if (! $this->getDataContainer()->getDescription()
79         && ! $this->getDataContainer()->getTitle()) {
80             $message = 'RSS 2.0 entry elements SHOULD contain exactly one'
81             . ' title element but a title has not been set. In addition, there'
82             . ' is no description as required in the absence of a title.';
83             $exception = new Writer\Exception\InvalidArgumentException($message);
84             if (! $this->ignoreExceptions) {
85                 throw $exception;
86             } else {
87                 $this->exceptions[] = $exception;
88                 return;
89             }
90         }
91         $title = $dom->createElement('title');
92         $root->appendChild($title);
93         $text = $dom->createTextNode($this->getDataContainer()->getTitle());
94         $title->appendChild($text);
95     }
96
97     /**
98      * Set entry description
99      *
100      * @param  DOMDocument $dom
101      * @param  DOMElement $root
102      * @return void
103      * @throws Writer\Exception\InvalidArgumentException
104      */
105     // @codingStandardsIgnoreStart
106     protected function _setDescription(DOMDocument $dom, DOMElement $root)
107     {
108         // @codingStandardsIgnoreEnd
109         if (! $this->getDataContainer()->getDescription()
110         && ! $this->getDataContainer()->getTitle()) {
111             $message = 'RSS 2.0 entry elements SHOULD contain exactly one'
112             . ' description element but a description has not been set. In'
113             . ' addition, there is no title element as required in the absence'
114             . ' of a description.';
115             $exception = new Writer\Exception\InvalidArgumentException($message);
116             if (! $this->ignoreExceptions) {
117                 throw $exception;
118             } else {
119                 $this->exceptions[] = $exception;
120                 return;
121             }
122         }
123         if (! $this->getDataContainer()->getDescription()) {
124             return;
125         }
126         $subtitle = $dom->createElement('description');
127         $root->appendChild($subtitle);
128         $text = $dom->createCDATASection($this->getDataContainer()->getDescription());
129         $subtitle->appendChild($text);
130     }
131
132     /**
133      * Set date entry was last modified
134      *
135      * @param  DOMDocument $dom
136      * @param  DOMElement $root
137      * @return void
138      */
139     // @codingStandardsIgnoreStart
140     protected function _setDateModified(DOMDocument $dom, DOMElement $root)
141     {
142         // @codingStandardsIgnoreEnd
143         if (! $this->getDataContainer()->getDateModified()) {
144             return;
145         }
146
147         $updated = $dom->createElement('pubDate');
148         $root->appendChild($updated);
149         $text = $dom->createTextNode(
150             $this->getDataContainer()->getDateModified()->format(DateTime::RSS)
151         );
152         $updated->appendChild($text);
153     }
154
155     /**
156      * Set date entry was created
157      *
158      * @param  DOMDocument $dom
159      * @param  DOMElement $root
160      * @return void
161      */
162     // @codingStandardsIgnoreStart
163     protected function _setDateCreated(DOMDocument $dom, DOMElement $root)
164     {
165         // @codingStandardsIgnoreEnd
166         if (! $this->getDataContainer()->getDateCreated()) {
167             return;
168         }
169         if (! $this->getDataContainer()->getDateModified()) {
170             $this->getDataContainer()->setDateModified(
171                 $this->getDataContainer()->getDateCreated()
172             );
173         }
174     }
175
176     /**
177      * Set entry authors
178      *
179      * @param  DOMDocument $dom
180      * @param  DOMElement $root
181      * @return void
182      */
183     // @codingStandardsIgnoreStart
184     protected function _setAuthors(DOMDocument $dom, DOMElement $root)
185     {
186         // @codingStandardsIgnoreEnd
187         $authors = $this->container->getAuthors();
188         if ((! $authors || empty($authors))) {
189             return;
190         }
191         foreach ($authors as $data) {
192             $author = $this->dom->createElement('author');
193             $name = $data['name'];
194             if (array_key_exists('email', $data)) {
195                 $name = $data['email'] . ' (' . $data['name'] . ')';
196             }
197             $text = $dom->createTextNode($name);
198             $author->appendChild($text);
199             $root->appendChild($author);
200         }
201     }
202
203     /**
204      * Set entry enclosure
205      *
206      * @param  DOMDocument $dom
207      * @param  DOMElement $root
208      * @return void
209      * @throws Writer\Exception\InvalidArgumentException
210      */
211     // @codingStandardsIgnoreStart
212     protected function _setEnclosure(DOMDocument $dom, DOMElement $root)
213     {
214         // @codingStandardsIgnoreEnd
215         $data = $this->container->getEnclosure();
216         if ((! $data || empty($data))) {
217             return;
218         }
219         if (! isset($data['type'])) {
220             $exception = new Writer\Exception\InvalidArgumentException('Enclosure "type" is not set');
221             if (! $this->ignoreExceptions) {
222                 throw $exception;
223             } else {
224                 $this->exceptions[] = $exception;
225                 return;
226             }
227         }
228         if (! isset($data['length'])) {
229             $exception = new Writer\Exception\InvalidArgumentException('Enclosure "length" is not set');
230             if (! $this->ignoreExceptions) {
231                 throw $exception;
232             } else {
233                 $this->exceptions[] = $exception;
234                 return;
235             }
236         }
237         if ((int) $data['length'] < 0 || ! ctype_digit((string) $data['length'])) {
238             $exception = new Writer\Exception\InvalidArgumentException('Enclosure "length" must be an integer'
239             . ' indicating the content\'s length in bytes');
240             if (! $this->ignoreExceptions) {
241                 throw $exception;
242             } else {
243                 $this->exceptions[] = $exception;
244                 return;
245             }
246         }
247         $enclosure = $this->dom->createElement('enclosure');
248         $enclosure->setAttribute('type', $data['type']);
249         $enclosure->setAttribute('length', $data['length']);
250         $enclosure->setAttribute('url', $data['uri']);
251         $root->appendChild($enclosure);
252     }
253
254     /**
255      * Set link to entry
256      *
257      * @param  DOMDocument $dom
258      * @param  DOMElement $root
259      * @return void
260      */
261     // @codingStandardsIgnoreStart
262     protected function _setLink(DOMDocument $dom, DOMElement $root)
263     {
264         // @codingStandardsIgnoreEnd
265         if (! $this->getDataContainer()->getLink()) {
266             return;
267         }
268         $link = $dom->createElement('link');
269         $root->appendChild($link);
270         $text = $dom->createTextNode($this->getDataContainer()->getLink());
271         $link->appendChild($text);
272     }
273
274     /**
275      * Set entry identifier
276      *
277      * @param  DOMDocument $dom
278      * @param  DOMElement $root
279      * @return void
280      */
281     // @codingStandardsIgnoreStart
282     protected function _setId(DOMDocument $dom, DOMElement $root)
283     {
284         // @codingStandardsIgnoreEnd
285         if (! $this->getDataContainer()->getId()
286         && ! $this->getDataContainer()->getLink()) {
287             return;
288         }
289
290         $id = $dom->createElement('guid');
291         $root->appendChild($id);
292         if (! $this->getDataContainer()->getId()) {
293             $this->getDataContainer()->setId(
294                 $this->getDataContainer()->getLink()
295             );
296         }
297         $text = $dom->createTextNode($this->getDataContainer()->getId());
298         $id->appendChild($text);
299         if (! Uri::factory($this->getDataContainer()->getId())->isValid()) {
300             $id->setAttribute('isPermaLink', 'false');
301         }
302     }
303
304     /**
305      * Set link to entry comments
306      *
307      * @param  DOMDocument $dom
308      * @param  DOMElement $root
309      * @return void
310      */
311     // @codingStandardsIgnoreStart
312     protected function _setCommentLink(DOMDocument $dom, DOMElement $root)
313     {
314         // @codingStandardsIgnoreEnd
315         $link = $this->getDataContainer()->getCommentLink();
316         if (! $link) {
317             return;
318         }
319         $clink = $this->dom->createElement('comments');
320         $text = $dom->createTextNode($link);
321         $clink->appendChild($text);
322         $root->appendChild($clink);
323     }
324
325     /**
326      * Set entry categories
327      *
328      * @param DOMDocument $dom
329      * @param DOMElement $root
330      * @return void
331      */
332     // @codingStandardsIgnoreStart
333     protected function _setCategories(DOMDocument $dom, DOMElement $root)
334     {
335         // @codingStandardsIgnoreEnd
336         $categories = $this->getDataContainer()->getCategories();
337         if (! $categories) {
338             return;
339         }
340         foreach ($categories as $cat) {
341             $category = $dom->createElement('category');
342             if (isset($cat['scheme'])) {
343                 $category->setAttribute('domain', $cat['scheme']);
344             }
345             $text = $dom->createCDATASection($cat['term']);
346             $category->appendChild($text);
347             $root->appendChild($category);
348         }
349     }
350 }