|
1 <?php |
|
2 /** |
|
3 * Zend Framework |
|
4 * |
|
5 * LICENSE |
|
6 * |
|
7 * This source file is subject to the new BSD license that is bundled |
|
8 * with this package in the file LICENSE.txt. |
|
9 * It is also available through the world-wide-web at this URL: |
|
10 * http://framework.zend.com/license/new-bsd |
|
11 * If you did not receive a copy of the license and are unable to |
|
12 * obtain it through the world-wide-web, please send an email |
|
13 * to license@zend.com so we can send you a copy immediately. |
|
14 * |
|
15 * @category Zend |
|
16 * @package Zend_Feed_Writer |
|
17 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) |
|
18 * @license http://framework.zend.com/license/new-bsd New BSD License |
|
19 * @version $Id: Rss.php 22064 2010-04-30 14:02:38Z padraic $ |
|
20 */ |
|
21 |
|
22 /** |
|
23 * @see Zend_Feed_Writer_Renderer_RendererAbstract |
|
24 */ |
|
25 require_once 'Zend/Feed/Writer/Renderer/RendererAbstract.php'; |
|
26 |
|
27 /** |
|
28 * @category Zend |
|
29 * @package Zend_Feed_Writer |
|
30 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) |
|
31 * @license http://framework.zend.com/license/new-bsd New BSD License |
|
32 */ |
|
33 class Zend_Feed_Writer_Renderer_Entry_Rss |
|
34 extends Zend_Feed_Writer_Renderer_RendererAbstract |
|
35 implements Zend_Feed_Writer_Renderer_RendererInterface |
|
36 { |
|
37 /** |
|
38 * Constructor |
|
39 * |
|
40 * @param Zend_Feed_Writer_Entry $container |
|
41 * @return void |
|
42 */ |
|
43 public function __construct (Zend_Feed_Writer_Entry $container) |
|
44 { |
|
45 parent::__construct($container); |
|
46 } |
|
47 |
|
48 /** |
|
49 * Render RSS entry |
|
50 * |
|
51 * @return Zend_Feed_Writer_Renderer_Entry_Rss |
|
52 */ |
|
53 public function render() |
|
54 { |
|
55 $this->_dom = new DOMDocument('1.0', $this->_container->getEncoding()); |
|
56 $this->_dom->formatOutput = true; |
|
57 $this->_dom->substituteEntities = false; |
|
58 $entry = $this->_dom->createElement('item'); |
|
59 $this->_dom->appendChild($entry); |
|
60 |
|
61 $this->_setTitle($this->_dom, $entry); |
|
62 $this->_setDescription($this->_dom, $entry); |
|
63 $this->_setDateCreated($this->_dom, $entry); |
|
64 $this->_setDateModified($this->_dom, $entry); |
|
65 $this->_setLink($this->_dom, $entry); |
|
66 $this->_setId($this->_dom, $entry); |
|
67 $this->_setAuthors($this->_dom, $entry); |
|
68 $this->_setEnclosure($this->_dom, $entry); |
|
69 $this->_setCommentLink($this->_dom, $entry); |
|
70 $this->_setCategories($this->_dom, $entry); |
|
71 foreach ($this->_extensions as $ext) { |
|
72 $ext->setType($this->getType()); |
|
73 $ext->setRootElement($this->getRootElement()); |
|
74 $ext->setDomDocument($this->getDomDocument(), $entry); |
|
75 $ext->render(); |
|
76 } |
|
77 |
|
78 return $this; |
|
79 } |
|
80 |
|
81 /** |
|
82 * Set entry title |
|
83 * |
|
84 * @param DOMDocument $dom |
|
85 * @param DOMElement $root |
|
86 * @return void |
|
87 */ |
|
88 protected function _setTitle(DOMDocument $dom, DOMElement $root) |
|
89 { |
|
90 if(!$this->getDataContainer()->getDescription() |
|
91 && !$this->getDataContainer()->getTitle()) { |
|
92 require_once 'Zend/Feed/Exception.php'; |
|
93 $message = 'RSS 2.0 entry elements SHOULD contain exactly one' |
|
94 . ' title element but a title has not been set. In addition, there' |
|
95 . ' is no description as required in the absence of a title.'; |
|
96 $exception = new Zend_Feed_Exception($message); |
|
97 if (!$this->_ignoreExceptions) { |
|
98 throw $exception; |
|
99 } else { |
|
100 $this->_exceptions[] = $exception; |
|
101 return; |
|
102 } |
|
103 } |
|
104 $title = $dom->createElement('title'); |
|
105 $root->appendChild($title); |
|
106 $text = $dom->createTextNode($this->getDataContainer()->getTitle()); |
|
107 $title->appendChild($text); |
|
108 } |
|
109 |
|
110 /** |
|
111 * Set entry description |
|
112 * |
|
113 * @param DOMDocument $dom |
|
114 * @param DOMElement $root |
|
115 * @return void |
|
116 */ |
|
117 protected function _setDescription(DOMDocument $dom, DOMElement $root) |
|
118 { |
|
119 if(!$this->getDataContainer()->getDescription() |
|
120 && !$this->getDataContainer()->getTitle()) { |
|
121 require_once 'Zend/Feed/Exception.php'; |
|
122 $message = 'RSS 2.0 entry elements SHOULD contain exactly one' |
|
123 . ' description element but a description has not been set. In' |
|
124 . ' addition, there is no title element as required in the absence' |
|
125 . ' of a description.'; |
|
126 $exception = new Zend_Feed_Exception($message); |
|
127 if (!$this->_ignoreExceptions) { |
|
128 throw $exception; |
|
129 } else { |
|
130 $this->_exceptions[] = $exception; |
|
131 return; |
|
132 } |
|
133 } |
|
134 if (!$this->getDataContainer()->getDescription()) { |
|
135 return; |
|
136 } |
|
137 $subtitle = $dom->createElement('description'); |
|
138 $root->appendChild($subtitle); |
|
139 $text = $dom->createCDATASection($this->getDataContainer()->getDescription()); |
|
140 $subtitle->appendChild($text); |
|
141 } |
|
142 |
|
143 /** |
|
144 * Set date entry was last modified |
|
145 * |
|
146 * @param DOMDocument $dom |
|
147 * @param DOMElement $root |
|
148 * @return void |
|
149 */ |
|
150 protected function _setDateModified(DOMDocument $dom, DOMElement $root) |
|
151 { |
|
152 if(!$this->getDataContainer()->getDateModified()) { |
|
153 return; |
|
154 } |
|
155 |
|
156 $updated = $dom->createElement('pubDate'); |
|
157 $root->appendChild($updated); |
|
158 $text = $dom->createTextNode( |
|
159 $this->getDataContainer()->getDateModified()->get(Zend_Date::RSS) |
|
160 ); |
|
161 $updated->appendChild($text); |
|
162 } |
|
163 |
|
164 /** |
|
165 * Set date entry was created |
|
166 * |
|
167 * @param DOMDocument $dom |
|
168 * @param DOMElement $root |
|
169 * @return void |
|
170 */ |
|
171 protected function _setDateCreated(DOMDocument $dom, DOMElement $root) |
|
172 { |
|
173 if (!$this->getDataContainer()->getDateCreated()) { |
|
174 return; |
|
175 } |
|
176 if (!$this->getDataContainer()->getDateModified()) { |
|
177 $this->getDataContainer()->setDateModified( |
|
178 $this->getDataContainer()->getDateCreated() |
|
179 ); |
|
180 } |
|
181 } |
|
182 |
|
183 /** |
|
184 * Set entry authors |
|
185 * |
|
186 * @param DOMDocument $dom |
|
187 * @param DOMElement $root |
|
188 * @return void |
|
189 */ |
|
190 protected function _setAuthors(DOMDocument $dom, DOMElement $root) |
|
191 { |
|
192 $authors = $this->_container->getAuthors(); |
|
193 if ((!$authors || empty($authors))) { |
|
194 return; |
|
195 } |
|
196 foreach ($authors as $data) { |
|
197 $author = $this->_dom->createElement('author'); |
|
198 $name = $data['name']; |
|
199 if (array_key_exists('email', $data)) { |
|
200 $name = $data['email'] . ' (' . $data['name'] . ')'; |
|
201 } |
|
202 $text = $dom->createTextNode($name); |
|
203 $author->appendChild($text); |
|
204 $root->appendChild($author); |
|
205 } |
|
206 } |
|
207 |
|
208 /** |
|
209 * Set entry enclosure |
|
210 * |
|
211 * @param DOMDocument $dom |
|
212 * @param DOMElement $root |
|
213 * @return void |
|
214 */ |
|
215 protected function _setEnclosure(DOMDocument $dom, DOMElement $root) |
|
216 { |
|
217 $data = $this->_container->getEnclosure(); |
|
218 if ((!$data || empty($data))) { |
|
219 return; |
|
220 } |
|
221 if (!isset($data['type'])) { |
|
222 require_once 'Zend/Feed/Exception.php'; |
|
223 $exception = new Zend_Feed_Exception('Enclosure "type" is not set'); |
|
224 if (!$this->_ignoreExceptions) { |
|
225 throw $exception; |
|
226 } else { |
|
227 $this->_exceptions[] = $exception; |
|
228 return; |
|
229 } |
|
230 } |
|
231 if (!isset($data['length'])) { |
|
232 require_once 'Zend/Feed/Exception.php'; |
|
233 $exception = new Zend_Feed_Exception('Enclosure "length" is not set'); |
|
234 if (!$this->_ignoreExceptions) { |
|
235 throw $exception; |
|
236 } else { |
|
237 $this->_exceptions[] = $exception; |
|
238 return; |
|
239 } |
|
240 } |
|
241 if (isset($data['length']) && (int) $data['length'] <= 0) { |
|
242 require_once 'Zend/Feed/Exception.php'; |
|
243 $exception = new Zend_Feed_Exception('Enclosure "length" must be an integer' |
|
244 . ' indicating the content\'s length in bytes'); |
|
245 if (!$this->_ignoreExceptions) { |
|
246 throw $exception; |
|
247 } else { |
|
248 $this->_exceptions[] = $exception; |
|
249 return; |
|
250 } |
|
251 } |
|
252 $enclosure = $this->_dom->createElement('enclosure'); |
|
253 $enclosure->setAttribute('type', $data['type']); |
|
254 $enclosure->setAttribute('length', $data['length']); |
|
255 $enclosure->setAttribute('url', $data['uri']); |
|
256 $root->appendChild($enclosure); |
|
257 } |
|
258 |
|
259 /** |
|
260 * Set link to entry |
|
261 * |
|
262 * @param DOMDocument $dom |
|
263 * @param DOMElement $root |
|
264 * @return void |
|
265 */ |
|
266 protected function _setLink(DOMDocument $dom, DOMElement $root) |
|
267 { |
|
268 if(!$this->getDataContainer()->getLink()) { |
|
269 return; |
|
270 } |
|
271 $link = $dom->createElement('link'); |
|
272 $root->appendChild($link); |
|
273 $text = $dom->createTextNode($this->getDataContainer()->getLink()); |
|
274 $link->appendChild($text); |
|
275 } |
|
276 |
|
277 /** |
|
278 * Set entry identifier |
|
279 * |
|
280 * @param DOMDocument $dom |
|
281 * @param DOMElement $root |
|
282 * @return void |
|
283 */ |
|
284 protected function _setId(DOMDocument $dom, DOMElement $root) |
|
285 { |
|
286 if(!$this->getDataContainer()->getId() |
|
287 && !$this->getDataContainer()->getLink()) { |
|
288 return; |
|
289 } |
|
290 |
|
291 $id = $dom->createElement('guid'); |
|
292 $root->appendChild($id); |
|
293 if (!$this->getDataContainer()->getId()) { |
|
294 $this->getDataContainer()->setId( |
|
295 $this->getDataContainer()->getLink()); |
|
296 } |
|
297 $text = $dom->createTextNode($this->getDataContainer()->getId()); |
|
298 $id->appendChild($text); |
|
299 if (!Zend_Uri::check($this->getDataContainer()->getId())) { |
|
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 protected function _setCommentLink(DOMDocument $dom, DOMElement $root) |
|
312 { |
|
313 $link = $this->getDataContainer()->getCommentLink(); |
|
314 if (!$link) { |
|
315 return; |
|
316 } |
|
317 $clink = $this->_dom->createElement('comments'); |
|
318 $text = $dom->createTextNode($link); |
|
319 $clink->appendChild($text); |
|
320 $root->appendChild($clink); |
|
321 } |
|
322 |
|
323 /** |
|
324 * Set entry categories |
|
325 * |
|
326 * @param DOMDocument $dom |
|
327 * @param DOMElement $root |
|
328 * @return void |
|
329 */ |
|
330 protected function _setCategories(DOMDocument $dom, DOMElement $root) |
|
331 { |
|
332 $categories = $this->getDataContainer()->getCategories(); |
|
333 if (!$categories) { |
|
334 return; |
|
335 } |
|
336 foreach ($categories as $cat) { |
|
337 $category = $dom->createElement('category'); |
|
338 if (isset($cat['scheme'])) { |
|
339 $category->setAttribute('domain', $cat['scheme']); |
|
340 } |
|
341 $text = $dom->createCDATASection($cat['term']); |
|
342 $category->appendChild($text); |
|
343 $root->appendChild($category); |
|
344 } |
|
345 } |
|
346 } |