File indexing completed on 2025-03-09 05:21:31

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_Extension_RendererAbstract
0024  */
0025 // require_once 'Zend/Feed/Writer/Extension/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_Extension_Atom_Renderer_Feed
0034     extends Zend_Feed_Writer_Extension_RendererAbstract
0035 {
0036 
0037     /**
0038      * Set to TRUE if a rendering method actually renders something. This
0039      * is used to prevent premature appending of a XML namespace declaration
0040      * until an element which requires it is actually appended.
0041      *
0042      * @var bool
0043      */
0044     protected $_called = false;
0045 
0046     /**
0047      * Render feed
0048      *
0049      * @return void
0050      */
0051     public function render()
0052     {
0053         /**
0054          * RSS 2.0 only. Used mainly to include Atom links and
0055          * Pubsubhubbub Hub endpoint URIs under the Atom namespace
0056          */
0057         if (strtolower($this->getType()) == 'atom') {
0058             return;
0059         }
0060         $this->_setFeedLinks($this->_dom, $this->_base);
0061         $this->_setHubs($this->_dom, $this->_base);
0062         if ($this->_called) {
0063             $this->_appendNamespaces();
0064         }
0065     }
0066 
0067     /**
0068      * Append namespaces to root element of feed
0069      *
0070      * @return void
0071      */
0072     protected function _appendNamespaces()
0073     {
0074         $this->getRootElement()->setAttribute('xmlns:atom',
0075             'http://www.w3.org/2005/Atom');
0076     }
0077 
0078     /**
0079      * Set feed link elements
0080      *
0081      * @param  DOMDocument $dom
0082      * @param  DOMElement $root
0083      * @return void
0084      */
0085     protected function _setFeedLinks(DOMDocument $dom, DOMElement $root)
0086     {
0087         $flinks = $this->getDataContainer()->getFeedLinks();
0088         if(!$flinks || empty($flinks)) {
0089             return;
0090         }
0091         foreach ($flinks as $type => $href) {
0092             $mime  = 'application/' . strtolower($type) . '+xml';
0093             $flink = $dom->createElement('atom:link');
0094             $root->appendChild($flink);
0095             $flink->setAttribute('rel', 'self');
0096             $flink->setAttribute('type', $mime);
0097             $flink->setAttribute('href', $href);
0098         }
0099         $this->_called = true;
0100     }
0101 
0102     /**
0103      * Set PuSH hubs
0104      *
0105      * @param  DOMDocument $dom
0106      * @param  DOMElement $root
0107      * @return void
0108      */
0109     protected function _setHubs(DOMDocument $dom, DOMElement $root)
0110     {
0111         $hubs = $this->getDataContainer()->getHubs();
0112         if (!$hubs || empty($hubs)) {
0113             return;
0114         }
0115         foreach ($hubs as $hubUrl) {
0116             $hub = $dom->createElement('atom:link');
0117             $hub->setAttribute('rel', 'hub');
0118             $hub->setAttribute('href', $hubUrl);
0119             $root->appendChild($hub);
0120         }
0121         $this->_called = true;
0122     }
0123 }