File indexing completed on 2025-01-12 05:21:59

0001 <?php
0002 /**
0003  * Zend Framework
0004  *
0005  * LICENSE
0006  *
0007  * This source file is subject to the new BSD license that is bundled
0008  * with this package in the file LICENSE.txt.
0009  * It is also available through the world-wide-web at this URL:
0010  * http://framework.zend.com/license/new-bsd
0011  * If you did not receive a copy of the license and are unable to
0012  * obtain it through the world-wide-web, please send an email
0013  * to license@zend.com so we can send you a copy immediately.
0014  *
0015  * @category   Zend
0016  * @package    Zend_Feed_Writer
0017  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0018  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0019  * @version    $Id$
0020  */
0021 
0022 /**
0023  * @see Zend_Feed_Writer_Renderer_RendererAbstract
0024  */
0025 // require_once 'Zend/Feed/Writer/Renderer/RendererAbstract.php';
0026 
0027 /**
0028  * @category   Zend
0029  * @package    Zend_Feed_Writer
0030  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0031  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0032  */
0033 class Zend_Feed_Writer_Renderer_Entry_Rss
0034     extends Zend_Feed_Writer_Renderer_RendererAbstract
0035     implements Zend_Feed_Writer_Renderer_RendererInterface
0036 {
0037     /**
0038      * Constructor
0039      *
0040      * @param  Zend_Feed_Writer_Entry $container
0041      * @return void
0042      */
0043     public function __construct (Zend_Feed_Writer_Entry $container)
0044     {
0045         parent::__construct($container);
0046     }
0047 
0048     /**
0049      * Render RSS entry
0050      *
0051      * @return Zend_Feed_Writer_Renderer_Entry_Rss
0052      */
0053     public function render()
0054     {
0055         $this->_dom = new DOMDocument('1.0', $this->_container->getEncoding());
0056         $this->_dom->formatOutput = true;
0057         $this->_dom->substituteEntities = false;
0058         $entry = $this->_dom->createElement('item');
0059         $this->_dom->appendChild($entry);
0060 
0061         $this->_setTitle($this->_dom, $entry);
0062         $this->_setDescription($this->_dom, $entry);
0063         $this->_setDateCreated($this->_dom, $entry);
0064         $this->_setDateModified($this->_dom, $entry);
0065         $this->_setLink($this->_dom, $entry);
0066         $this->_setId($this->_dom, $entry);
0067         $this->_setAuthors($this->_dom, $entry);
0068         $this->_setEnclosure($this->_dom, $entry);
0069         $this->_setCommentLink($this->_dom, $entry);
0070         $this->_setCategories($this->_dom, $entry);
0071         foreach ($this->_extensions as $ext) {
0072             $ext->setType($this->getType());
0073             $ext->setRootElement($this->getRootElement());
0074             $ext->setDomDocument($this->getDomDocument(), $entry);
0075             $ext->render();
0076         }
0077 
0078         return $this;
0079     }
0080 
0081     /**
0082      * Set entry title
0083      *
0084      * @param  DOMDocument $dom
0085      * @param  DOMElement $root
0086      * @return void
0087      */
0088     protected function _setTitle(DOMDocument $dom, DOMElement $root)
0089     {
0090         if(!$this->getDataContainer()->getDescription()
0091         && !$this->getDataContainer()->getTitle()) {
0092             // require_once 'Zend/Feed/Exception.php';
0093             $message = 'RSS 2.0 entry elements SHOULD contain exactly one'
0094             . ' title element but a title has not been set. In addition, there'
0095             . ' is no description as required in the absence of a title.';
0096             $exception = new Zend_Feed_Exception($message);
0097             if (!$this->_ignoreExceptions) {
0098                 throw $exception;
0099             } else {
0100                 $this->_exceptions[] = $exception;
0101                 return;
0102             }
0103         }
0104         $title = $dom->createElement('title');
0105         $root->appendChild($title);
0106         $text = $dom->createTextNode($this->getDataContainer()->getTitle());
0107         $title->appendChild($text);
0108     }
0109 
0110     /**
0111      * Set entry description
0112      *
0113      * @param  DOMDocument $dom
0114      * @param  DOMElement $root
0115      * @return void
0116      */
0117     protected function _setDescription(DOMDocument $dom, DOMElement $root)
0118     {
0119         if(!$this->getDataContainer()->getDescription()
0120         && !$this->getDataContainer()->getTitle()) {
0121             // require_once 'Zend/Feed/Exception.php';
0122             $message = 'RSS 2.0 entry elements SHOULD contain exactly one'
0123             . ' description element but a description has not been set. In'
0124             . ' addition, there is no title element as required in the absence'
0125             . ' of a description.';
0126             $exception = new Zend_Feed_Exception($message);
0127             if (!$this->_ignoreExceptions) {
0128                 throw $exception;
0129             } else {
0130                 $this->_exceptions[] = $exception;
0131                 return;
0132             }
0133         }
0134         if (!$this->getDataContainer()->getDescription()) {
0135             return;
0136         }
0137         $subtitle = $dom->createElement('description');
0138         $root->appendChild($subtitle);
0139         $text = $dom->createCDATASection($this->getDataContainer()->getDescription());
0140         $subtitle->appendChild($text);
0141     }
0142 
0143     /**
0144      * Set date entry was last modified
0145      *
0146      * @param  DOMDocument $dom
0147      * @param  DOMElement $root
0148      * @return void
0149      */
0150     protected function _setDateModified(DOMDocument $dom, DOMElement $root)
0151     {
0152         if(!$this->getDataContainer()->getDateModified()) {
0153             return;
0154         }
0155 
0156         $updated = $dom->createElement('pubDate');
0157         $root->appendChild($updated);
0158         $text = $dom->createTextNode(
0159             $this->getDataContainer()->getDateModified()->get(Zend_Date::RSS)
0160         );
0161         $updated->appendChild($text);
0162     }
0163 
0164     /**
0165      * Set date entry was created
0166      *
0167      * @param  DOMDocument $dom
0168      * @param  DOMElement $root
0169      * @return void
0170      */
0171     protected function _setDateCreated(DOMDocument $dom, DOMElement $root)
0172     {
0173         if (!$this->getDataContainer()->getDateCreated()) {
0174             return;
0175         }
0176         if (!$this->getDataContainer()->getDateModified()) {
0177             $this->getDataContainer()->setDateModified(
0178                 $this->getDataContainer()->getDateCreated()
0179             );
0180         }
0181     }
0182 
0183     /**
0184      * Set entry authors
0185      *
0186      * @param  DOMDocument $dom
0187      * @param  DOMElement $root
0188      * @return void
0189      */
0190     protected function _setAuthors(DOMDocument $dom, DOMElement $root)
0191     {
0192         $authors = $this->_container->getAuthors();
0193         if ((!$authors || empty($authors))) {
0194             return;
0195         }
0196         foreach ($authors as $data) {
0197             $author = $this->_dom->createElement('author');
0198             $name = $data['name'];
0199             if (array_key_exists('email', $data)) {
0200                 $name = $data['email'] . ' (' . $data['name'] . ')';
0201             }
0202             $text = $dom->createTextNode($name);
0203             $author->appendChild($text);
0204             $root->appendChild($author);
0205         }
0206     }
0207 
0208     /**
0209      * Set entry enclosure
0210      *
0211      * @param  DOMDocument $dom
0212      * @param  DOMElement $root
0213      * @return void
0214      */
0215     protected function _setEnclosure(DOMDocument $dom, DOMElement $root)
0216     {
0217         $data = $this->_container->getEnclosure();
0218         if ((!$data || empty($data))) {
0219             return;
0220         }
0221         if (!isset($data['type'])) {
0222             // require_once 'Zend/Feed/Exception.php';
0223             $exception = new Zend_Feed_Exception('Enclosure "type" is not set');
0224             if (!$this->_ignoreExceptions) {
0225                 throw $exception;
0226             } else {
0227                 $this->_exceptions[] = $exception;
0228                 return;
0229             }
0230         }
0231         if (!isset($data['length'])) {
0232             // require_once 'Zend/Feed/Exception.php';
0233             $exception = new Zend_Feed_Exception('Enclosure "length" is not set');
0234             if (!$this->_ignoreExceptions) {
0235                 throw $exception;
0236             } else {
0237                 $this->_exceptions[] = $exception;
0238                 return;
0239             }
0240         }
0241         if (isset($data['length']) && (int) $data['length'] <= 0) {
0242             // require_once 'Zend/Feed/Exception.php';
0243             $exception = new Zend_Feed_Exception('Enclosure "length" must be an integer'
0244             . ' indicating the content\'s length in bytes');
0245             if (!$this->_ignoreExceptions) {
0246                 throw $exception;
0247             } else {
0248                 $this->_exceptions[] = $exception;
0249                 return;
0250             }
0251         }
0252         $enclosure = $this->_dom->createElement('enclosure');
0253         $enclosure->setAttribute('type', $data['type']);
0254         $enclosure->setAttribute('length', $data['length']);
0255         $enclosure->setAttribute('url', $data['uri']);
0256         $root->appendChild($enclosure);
0257     }
0258 
0259     /**
0260      * Set link to entry
0261      *
0262      * @param  DOMDocument $dom
0263      * @param  DOMElement $root
0264      * @return void
0265      */
0266     protected function _setLink(DOMDocument $dom, DOMElement $root)
0267     {
0268         if(!$this->getDataContainer()->getLink()) {
0269             return;
0270         }
0271         $link = $dom->createElement('link');
0272         $root->appendChild($link);
0273         $text = $dom->createTextNode($this->getDataContainer()->getLink());
0274         $link->appendChild($text);
0275     }
0276 
0277     /**
0278      * Set entry identifier
0279      *
0280      * @param  DOMDocument $dom
0281      * @param  DOMElement $root
0282      * @return void
0283      */
0284     protected function _setId(DOMDocument $dom, DOMElement $root)
0285     {
0286         if(!$this->getDataContainer()->getId()
0287         && !$this->getDataContainer()->getLink()) {
0288             return;
0289         }
0290 
0291         $id = $dom->createElement('guid');
0292         $root->appendChild($id);
0293         if (!$this->getDataContainer()->getId()) {
0294             $this->getDataContainer()->setId(
0295                 $this->getDataContainer()->getLink());
0296         }
0297         $text = $dom->createTextNode($this->getDataContainer()->getId());
0298         $id->appendChild($text);
0299         if (!Zend_Uri::check($this->getDataContainer()->getId())) {
0300             $id->setAttribute('isPermaLink', 'false');
0301         }
0302     }
0303 
0304     /**
0305      * Set link to entry comments
0306      *
0307      * @param  DOMDocument $dom
0308      * @param  DOMElement $root
0309      * @return void
0310      */
0311     protected function _setCommentLink(DOMDocument $dom, DOMElement $root)
0312     {
0313         $link = $this->getDataContainer()->getCommentLink();
0314         if (!$link) {
0315             return;
0316         }
0317         $clink = $this->_dom->createElement('comments');
0318         $text = $dom->createTextNode($link);
0319         $clink->appendChild($text);
0320         $root->appendChild($clink);
0321     }
0322 
0323     /**
0324      * Set entry categories
0325      *
0326      * @param DOMDocument $dom
0327      * @param DOMElement $root
0328      * @return void
0329      */
0330     protected function _setCategories(DOMDocument $dom, DOMElement $root)
0331     {
0332         $categories = $this->getDataContainer()->getCategories();
0333         if (!$categories) {
0334             return;
0335         }
0336         foreach ($categories as $cat) {
0337             $category = $dom->createElement('category');
0338             if (isset($cat['scheme'])) {
0339                 $category->setAttribute('domain', $cat['scheme']);
0340             }
0341             $text = $dom->createCDATASection($cat['term']);
0342             $category->appendChild($text);
0343             $root->appendChild($category);
0344         }
0345     }
0346 }