7bce80d4b204a8cde152a44a6808f7de665995d4
[yaffs-website] / vendor / zendframework / zend-feed / src / Writer / Extension / ITunes / Renderer / Feed.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\Extension\ITunes\Renderer;
11
12 use DOMDocument;
13 use DOMElement;
14 use Zend\Feed\Writer\Extension;
15
16 /**
17 */
18 class Feed extends Extension\AbstractRenderer
19 {
20     /**
21      * Set to TRUE if a rendering method actually renders something. This
22      * is used to prevent premature appending of a XML namespace declaration
23      * until an element which requires it is actually appended.
24      *
25      * @var bool
26      */
27     protected $called = false;
28
29     /**
30      * Render feed
31      *
32      * @return void
33      */
34     public function render()
35     {
36         $this->_setAuthors($this->dom, $this->base);
37         $this->_setBlock($this->dom, $this->base);
38         $this->_setCategories($this->dom, $this->base);
39         $this->_setImage($this->dom, $this->base);
40         $this->_setDuration($this->dom, $this->base);
41         $this->_setExplicit($this->dom, $this->base);
42         $this->_setKeywords($this->dom, $this->base);
43         $this->_setNewFeedUrl($this->dom, $this->base);
44         $this->_setOwners($this->dom, $this->base);
45         $this->_setSubtitle($this->dom, $this->base);
46         $this->_setSummary($this->dom, $this->base);
47         if ($this->called) {
48             $this->_appendNamespaces();
49         }
50     }
51
52     /**
53      * Append feed namespaces
54      *
55      * @return void
56      */
57     // @codingStandardsIgnoreStart
58     protected function _appendNamespaces()
59     {
60         // @codingStandardsIgnoreEnd
61         $this->getRootElement()->setAttribute(
62             'xmlns:itunes',
63             'http://www.itunes.com/dtds/podcast-1.0.dtd'
64         );
65     }
66
67     /**
68      * Set feed authors
69      *
70      * @param  DOMDocument $dom
71      * @param  DOMElement $root
72      * @return void
73      */
74     // @codingStandardsIgnoreStart
75     protected function _setAuthors(DOMDocument $dom, DOMElement $root)
76     {
77         // @codingStandardsIgnoreEnd
78         $authors = $this->getDataContainer()->getItunesAuthors();
79         if (! $authors || empty($authors)) {
80             return;
81         }
82         foreach ($authors as $author) {
83             $el = $dom->createElement('itunes:author');
84             $text = $dom->createTextNode($author);
85             $el->appendChild($text);
86             $root->appendChild($el);
87         }
88         $this->called = true;
89     }
90
91     /**
92      * Set feed itunes block
93      *
94      * @param  DOMDocument $dom
95      * @param  DOMElement $root
96      * @return void
97      */
98     // @codingStandardsIgnoreStart
99     protected function _setBlock(DOMDocument $dom, DOMElement $root)
100     {
101         // @codingStandardsIgnoreEnd
102         $block = $this->getDataContainer()->getItunesBlock();
103         if ($block === null) {
104             return;
105         }
106         $el = $dom->createElement('itunes:block');
107         $text = $dom->createTextNode($block);
108         $el->appendChild($text);
109         $root->appendChild($el);
110         $this->called = true;
111     }
112
113     /**
114      * Set feed categories
115      *
116      * @param  DOMDocument $dom
117      * @param  DOMElement $root
118      * @return void
119      */
120     // @codingStandardsIgnoreStart
121     protected function _setCategories(DOMDocument $dom, DOMElement $root)
122     {
123         // @codingStandardsIgnoreEnd
124         $cats = $this->getDataContainer()->getItunesCategories();
125         if (! $cats || empty($cats)) {
126             return;
127         }
128         foreach ($cats as $key => $cat) {
129             if (! is_array($cat)) {
130                 $el = $dom->createElement('itunes:category');
131                 $el->setAttribute('text', $cat);
132                 $root->appendChild($el);
133             } else {
134                 $el = $dom->createElement('itunes:category');
135                 $el->setAttribute('text', $key);
136                 $root->appendChild($el);
137                 foreach ($cat as $subcat) {
138                     $el2 = $dom->createElement('itunes:category');
139                     $el2->setAttribute('text', $subcat);
140                     $el->appendChild($el2);
141                 }
142             }
143         }
144         $this->called = true;
145     }
146
147     /**
148      * Set feed image (icon)
149      *
150      * @param  DOMDocument $dom
151      * @param  DOMElement $root
152      * @return void
153      */
154     // @codingStandardsIgnoreStart
155     protected function _setImage(DOMDocument $dom, DOMElement $root)
156     {
157         // @codingStandardsIgnoreEnd
158         $image = $this->getDataContainer()->getItunesImage();
159         if (! $image) {
160             return;
161         }
162         $el = $dom->createElement('itunes:image');
163         $el->setAttribute('href', $image);
164         $root->appendChild($el);
165         $this->called = true;
166     }
167
168     /**
169      * Set feed cumulative duration
170      *
171      * @param  DOMDocument $dom
172      * @param  DOMElement $root
173      * @return void
174      */
175     // @codingStandardsIgnoreStart
176     protected function _setDuration(DOMDocument $dom, DOMElement $root)
177     {
178         // @codingStandardsIgnoreEnd
179         $duration = $this->getDataContainer()->getItunesDuration();
180         if (! $duration) {
181             return;
182         }
183         $el = $dom->createElement('itunes:duration');
184         $text = $dom->createTextNode($duration);
185         $el->appendChild($text);
186         $root->appendChild($el);
187         $this->called = true;
188     }
189
190     /**
191      * Set explicit flag
192      *
193      * @param  DOMDocument $dom
194      * @param  DOMElement $root
195      * @return void
196      */
197     // @codingStandardsIgnoreStart
198     protected function _setExplicit(DOMDocument $dom, DOMElement $root)
199     {
200         // @codingStandardsIgnoreEnd
201         $explicit = $this->getDataContainer()->getItunesExplicit();
202         if ($explicit === null) {
203             return;
204         }
205         $el = $dom->createElement('itunes:explicit');
206         $text = $dom->createTextNode($explicit);
207         $el->appendChild($text);
208         $root->appendChild($el);
209         $this->called = true;
210     }
211
212     /**
213      * Set feed keywords
214      *
215      * @param  DOMDocument $dom
216      * @param  DOMElement $root
217      * @return void
218      */
219     // @codingStandardsIgnoreStart
220     protected function _setKeywords(DOMDocument $dom, DOMElement $root)
221     {
222         // @codingStandardsIgnoreEnd
223         $keywords = $this->getDataContainer()->getItunesKeywords();
224         if (! $keywords || empty($keywords)) {
225             return;
226         }
227         $el = $dom->createElement('itunes:keywords');
228         $text = $dom->createTextNode(implode(',', $keywords));
229         $el->appendChild($text);
230         $root->appendChild($el);
231         $this->called = true;
232     }
233
234     /**
235      * Set feed's new URL
236      *
237      * @param  DOMDocument $dom
238      * @param  DOMElement $root
239      * @return void
240      */
241     // @codingStandardsIgnoreStart
242     protected function _setNewFeedUrl(DOMDocument $dom, DOMElement $root)
243     {
244         // @codingStandardsIgnoreEnd
245         $url = $this->getDataContainer()->getItunesNewFeedUrl();
246         if (! $url) {
247             return;
248         }
249         $el = $dom->createElement('itunes:new-feed-url');
250         $text = $dom->createTextNode($url);
251         $el->appendChild($text);
252         $root->appendChild($el);
253         $this->called = true;
254     }
255
256     /**
257      * Set feed owners
258      *
259      * @param  DOMDocument $dom
260      * @param  DOMElement $root
261      * @return void
262      */
263     // @codingStandardsIgnoreStart
264     protected function _setOwners(DOMDocument $dom, DOMElement $root)
265     {
266         // @codingStandardsIgnoreEnd
267         $owners = $this->getDataContainer()->getItunesOwners();
268         if (! $owners || empty($owners)) {
269             return;
270         }
271         foreach ($owners as $owner) {
272             $el = $dom->createElement('itunes:owner');
273             $name = $dom->createElement('itunes:name');
274             $text = $dom->createTextNode($owner['name']);
275             $name->appendChild($text);
276             $email = $dom->createElement('itunes:email');
277             $text = $dom->createTextNode($owner['email']);
278             $email->appendChild($text);
279             $root->appendChild($el);
280             $el->appendChild($name);
281             $el->appendChild($email);
282         }
283         $this->called = true;
284     }
285
286     /**
287      * Set feed subtitle
288      *
289      * @param  DOMDocument $dom
290      * @param  DOMElement $root
291      * @return void
292      */
293     // @codingStandardsIgnoreStart
294     protected function _setSubtitle(DOMDocument $dom, DOMElement $root)
295     {
296         // @codingStandardsIgnoreEnd
297         $subtitle = $this->getDataContainer()->getItunesSubtitle();
298         if (! $subtitle) {
299             return;
300         }
301         $el = $dom->createElement('itunes:subtitle');
302         $text = $dom->createTextNode($subtitle);
303         $el->appendChild($text);
304         $root->appendChild($el);
305         $this->called = true;
306     }
307
308     /**
309      * Set feed summary
310      *
311      * @param  DOMDocument $dom
312      * @param  DOMElement $root
313      * @return void
314      */
315     // @codingStandardsIgnoreStart
316     protected function _setSummary(DOMDocument $dom, DOMElement $root)
317     {
318         // @codingStandardsIgnoreEnd
319         $summary = $this->getDataContainer()->getItunesSummary();
320         if (! $summary) {
321             return;
322         }
323         $el = $dom->createElement('itunes:summary');
324         $text = $dom->createTextNode($summary);
325         $el->appendChild($text);
326         $root->appendChild($el);
327         $this->called = true;
328     }
329 }