3 * Zend Framework (http://framework.zend.com/)
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
10 namespace Zend\Feed\Writer\Renderer\Feed;
17 use Zend\Feed\Writer\Renderer;
18 use Zend\Feed\Writer\Version;
22 class Rss extends Renderer\AbstractRenderer implements Renderer\RendererInterface
27 * @param Writer\Feed $container
29 public function __construct(Writer\Feed $container)
31 parent::__construct($container);
39 public function render()
41 $this->dom = new DOMDocument('1.0', $this->container->getEncoding());
42 $this->dom->formatOutput = true;
43 $this->dom->substituteEntities = false;
44 $rss = $this->dom->createElement('rss');
45 $this->setRootElement($rss);
46 $rss->setAttribute('version', '2.0');
48 $channel = $this->dom->createElement('channel');
49 $rss->appendChild($channel);
50 $this->dom->appendChild($rss);
51 $this->_setLanguage($this->dom, $channel);
52 $this->_setBaseUrl($this->dom, $channel);
53 $this->_setTitle($this->dom, $channel);
54 $this->_setDescription($this->dom, $channel);
55 $this->_setImage($this->dom, $channel);
56 $this->_setDateCreated($this->dom, $channel);
57 $this->_setDateModified($this->dom, $channel);
58 $this->_setLastBuildDate($this->dom, $channel);
59 $this->_setGenerator($this->dom, $channel);
60 $this->_setLink($this->dom, $channel);
61 $this->_setAuthors($this->dom, $channel);
62 $this->_setCopyright($this->dom, $channel);
63 $this->_setCategories($this->dom, $channel);
65 foreach ($this->extensions as $ext) {
66 $ext->setType($this->getType());
67 $ext->setRootElement($this->getRootElement());
68 $ext->setDOMDocument($this->getDOMDocument(), $channel);
72 foreach ($this->container as $entry) {
73 if ($this->getDataContainer()->getEncoding()) {
74 $entry->setEncoding($this->getDataContainer()->getEncoding());
76 if ($entry instanceof Writer\Entry) {
77 $renderer = new Renderer\Entry\Rss($entry);
81 if ($this->ignoreExceptions === true) {
82 $renderer->ignoreExceptions();
84 $renderer->setType($this->getType());
85 $renderer->setRootElement($this->dom->documentElement);
87 $element = $renderer->getElement();
88 $deep = version_compare(PHP_VERSION, '7', 'ge') ? 1 : true;
89 $imported = $this->dom->importNode($element, $deep);
90 $channel->appendChild($imported);
98 * @param DOMDocument $dom
99 * @param DOMElement $root
102 // @codingStandardsIgnoreStart
103 protected function _setLanguage(DOMDocument $dom, DOMElement $root)
105 // @codingStandardsIgnoreEnd
106 $lang = $this->getDataContainer()->getLanguage();
110 $language = $dom->createElement('language');
111 $root->appendChild($language);
112 $language->nodeValue = $lang;
118 * @param DOMDocument $dom
119 * @param DOMElement $root
121 * @throws Writer\Exception\InvalidArgumentException
123 // @codingStandardsIgnoreStart
124 protected function _setTitle(DOMDocument $dom, DOMElement $root)
126 // @codingStandardsIgnoreEnd
127 if (! $this->getDataContainer()->getTitle()) {
128 $message = 'RSS 2.0 feed elements MUST contain exactly one'
129 . ' title element but a title has not been set';
130 $exception = new Writer\Exception\InvalidArgumentException($message);
131 if (! $this->ignoreExceptions) {
134 $this->exceptions[] = $exception;
139 $title = $dom->createElement('title');
140 $root->appendChild($title);
141 $text = $dom->createTextNode($this->getDataContainer()->getTitle());
142 $title->appendChild($text);
146 * Set feed description
148 * @param DOMDocument $dom
149 * @param DOMElement $root
151 * @throws Writer\Exception\InvalidArgumentException
153 // @codingStandardsIgnoreStart
154 protected function _setDescription(DOMDocument $dom, DOMElement $root)
156 // @codingStandardsIgnoreEnd
157 if (! $this->getDataContainer()->getDescription()) {
158 $message = 'RSS 2.0 feed elements MUST contain exactly one'
159 . ' description element but one has not been set';
160 $exception = new Writer\Exception\InvalidArgumentException($message);
161 if (! $this->ignoreExceptions) {
164 $this->exceptions[] = $exception;
168 $subtitle = $dom->createElement('description');
169 $root->appendChild($subtitle);
170 $text = $dom->createTextNode($this->getDataContainer()->getDescription());
171 $subtitle->appendChild($text);
175 * Set date feed was last modified
177 * @param DOMDocument $dom
178 * @param DOMElement $root
181 // @codingStandardsIgnoreStart
182 protected function _setDateModified(DOMDocument $dom, DOMElement $root)
184 // @codingStandardsIgnoreEnd
185 if (! $this->getDataContainer()->getDateModified()) {
189 $updated = $dom->createElement('pubDate');
190 $root->appendChild($updated);
191 $text = $dom->createTextNode(
192 $this->getDataContainer()->getDateModified()->format(DateTime::RSS)
194 $updated->appendChild($text);
198 * Set feed generator string
200 * @param DOMDocument $dom
201 * @param DOMElement $root
204 // @codingStandardsIgnoreStart
205 protected function _setGenerator(DOMDocument $dom, DOMElement $root)
207 // @codingStandardsIgnoreEnd
208 if (! $this->getDataContainer()->getGenerator()) {
209 $this->getDataContainer()->setGenerator(
212 'http://framework.zend.com'
216 $gdata = $this->getDataContainer()->getGenerator();
217 $generator = $dom->createElement('generator');
218 $root->appendChild($generator);
219 $name = $gdata['name'];
220 if (array_key_exists('version', $gdata)) {
221 $name .= ' ' . $gdata['version'];
223 if (array_key_exists('uri', $gdata)) {
224 $name .= ' (' . $gdata['uri'] . ')';
226 $text = $dom->createTextNode($name);
227 $generator->appendChild($text);
233 * @param DOMDocument $dom
234 * @param DOMElement $root
236 * @throws Writer\Exception\InvalidArgumentException
238 // @codingStandardsIgnoreStart
239 protected function _setLink(DOMDocument $dom, DOMElement $root)
241 // @codingStandardsIgnoreEnd
242 $value = $this->getDataContainer()->getLink();
244 $message = 'RSS 2.0 feed elements MUST contain exactly one'
245 . ' link element but one has not been set';
246 $exception = new Writer\Exception\InvalidArgumentException($message);
247 if (! $this->ignoreExceptions) {
250 $this->exceptions[] = $exception;
254 $link = $dom->createElement('link');
255 $root->appendChild($link);
256 $text = $dom->createTextNode($value);
257 $link->appendChild($text);
258 if (! Uri::factory($value)->isValid()) {
259 $link->setAttribute('isPermaLink', 'false');
266 * @param DOMDocument $dom
267 * @param DOMElement $root
270 // @codingStandardsIgnoreStart
271 protected function _setAuthors(DOMDocument $dom, DOMElement $root)
273 // @codingStandardsIgnoreEnd
274 $authors = $this->getDataContainer()->getAuthors();
275 if (! $authors || empty($authors)) {
278 foreach ($authors as $data) {
279 $author = $this->dom->createElement('author');
280 $name = $data['name'];
281 if (array_key_exists('email', $data)) {
282 $name = $data['email'] . ' (' . $data['name'] . ')';
284 $text = $dom->createTextNode($name);
285 $author->appendChild($text);
286 $root->appendChild($author);
293 * @param DOMDocument $dom
294 * @param DOMElement $root
297 // @codingStandardsIgnoreStart
298 protected function _setCopyright(DOMDocument $dom, DOMElement $root)
300 // @codingStandardsIgnoreEnd
301 $copyright = $this->getDataContainer()->getCopyright();
305 $copy = $dom->createElement('copyright');
306 $root->appendChild($copy);
307 $text = $dom->createTextNode($copyright);
308 $copy->appendChild($text);
312 * Set feed channel image
314 * @param DOMDocument $dom
315 * @param DOMElement $root
317 * @throws Writer\Exception\InvalidArgumentException
319 // @codingStandardsIgnoreStart
320 protected function _setImage(DOMDocument $dom, DOMElement $root)
322 // @codingStandardsIgnoreEnd
323 $image = $this->getDataContainer()->getImage();
328 if (! isset($image['title']) || empty($image['title'])
329 || ! is_string($image['title'])
331 $message = 'RSS 2.0 feed images must include a title';
332 $exception = new Writer\Exception\InvalidArgumentException($message);
333 if (! $this->ignoreExceptions) {
336 $this->exceptions[] = $exception;
341 if (empty($image['link']) || ! is_string($image['link'])
342 || ! Uri::factory($image['link'])->isValid()
344 $message = 'Invalid parameter: parameter \'link\''
345 . ' must be a non-empty string and valid URI/IRI';
346 $exception = new Writer\Exception\InvalidArgumentException($message);
347 if (! $this->ignoreExceptions) {
350 $this->exceptions[] = $exception;
355 $img = $dom->createElement('image');
356 $root->appendChild($img);
358 $url = $dom->createElement('url');
359 $text = $dom->createTextNode($image['uri']);
360 $url->appendChild($text);
362 $title = $dom->createElement('title');
363 $text = $dom->createTextNode($image['title']);
364 $title->appendChild($text);
366 $link = $dom->createElement('link');
367 $text = $dom->createTextNode($image['link']);
368 $link->appendChild($text);
370 $img->appendChild($url);
371 $img->appendChild($title);
372 $img->appendChild($link);
374 if (isset($image['height'])) {
375 if (! ctype_digit((string) $image['height']) || $image['height'] > 400) {
376 $message = 'Invalid parameter: parameter \'height\''
377 . ' must be an integer not exceeding 400';
378 $exception = new Writer\Exception\InvalidArgumentException($message);
379 if (! $this->ignoreExceptions) {
382 $this->exceptions[] = $exception;
386 $height = $dom->createElement('height');
387 $text = $dom->createTextNode($image['height']);
388 $height->appendChild($text);
389 $img->appendChild($height);
391 if (isset($image['width'])) {
392 if (! ctype_digit((string) $image['width']) || $image['width'] > 144) {
393 $message = 'Invalid parameter: parameter \'width\''
394 . ' must be an integer not exceeding 144';
395 $exception = new Writer\Exception\InvalidArgumentException($message);
396 if (! $this->ignoreExceptions) {
399 $this->exceptions[] = $exception;
403 $width = $dom->createElement('width');
404 $text = $dom->createTextNode($image['width']);
405 $width->appendChild($text);
406 $img->appendChild($width);
408 if (isset($image['description'])) {
409 if (empty($image['description']) || ! is_string($image['description'])) {
410 $message = 'Invalid parameter: parameter \'description\''
411 . ' must be a non-empty string';
412 $exception = new Writer\Exception\InvalidArgumentException($message);
413 if (! $this->ignoreExceptions) {
416 $this->exceptions[] = $exception;
420 $desc = $dom->createElement('description');
421 $text = $dom->createTextNode($image['description']);
422 $desc->appendChild($text);
423 $img->appendChild($desc);
428 * Set date feed was created
430 * @param DOMDocument $dom
431 * @param DOMElement $root
434 // @codingStandardsIgnoreStart
435 protected function _setDateCreated(DOMDocument $dom, DOMElement $root)
437 // @codingStandardsIgnoreEnd
438 if (! $this->getDataContainer()->getDateCreated()) {
441 if (! $this->getDataContainer()->getDateModified()) {
442 $this->getDataContainer()->setDateModified(
443 $this->getDataContainer()->getDateCreated()
449 * Set date feed last build date
451 * @param DOMDocument $dom
452 * @param DOMElement $root
455 // @codingStandardsIgnoreStart
456 protected function _setLastBuildDate(DOMDocument $dom, DOMElement $root)
458 // @codingStandardsIgnoreEnd
459 if (! $this->getDataContainer()->getLastBuildDate()) {
463 $lastBuildDate = $dom->createElement('lastBuildDate');
464 $root->appendChild($lastBuildDate);
465 $text = $dom->createTextNode(
466 $this->getDataContainer()->getLastBuildDate()->format(DateTime::RSS)
468 $lastBuildDate->appendChild($text);
472 * Set base URL to feed links
474 * @param DOMDocument $dom
475 * @param DOMElement $root
478 // @codingStandardsIgnoreStart
479 protected function _setBaseUrl(DOMDocument $dom, DOMElement $root)
481 // @codingStandardsIgnoreEnd
482 $baseUrl = $this->getDataContainer()->getBaseUrl();
486 $root->setAttribute('xml:base', $baseUrl);
490 * Set feed categories
492 * @param DOMDocument $dom
493 * @param DOMElement $root
496 // @codingStandardsIgnoreStart
497 protected function _setCategories(DOMDocument $dom, DOMElement $root)
499 // @codingStandardsIgnoreEnd
500 $categories = $this->getDataContainer()->getCategories();
504 foreach ($categories as $cat) {
505 $category = $dom->createElement('category');
506 if (isset($cat['scheme'])) {
507 $category->setAttribute('domain', $cat['scheme']);
509 $text = $dom->createTextNode($cat['term']);
510 $category->appendChild($text);
511 $root->appendChild($category);