web/Zend/Feed/Writer/Extension/Atom/Renderer/Feed.php
changeset 0 4eba9c11703f
equal deleted inserted replaced
-1:000000000000 0:4eba9c11703f
       
     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: Feed.php 20325 2010-01-16 00:17:59Z padraic $
       
    20  */
       
    21  
       
    22 /**
       
    23  * @see Zend_Feed_Writer_Extension_RendererAbstract
       
    24  */
       
    25 require_once 'Zend/Feed/Writer/Extension/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_Extension_Atom_Renderer_Feed
       
    34     extends Zend_Feed_Writer_Extension_RendererAbstract
       
    35 {
       
    36 
       
    37     /**
       
    38      * Set to TRUE if a rendering method actually renders something. This
       
    39      * is used to prevent premature appending of a XML namespace declaration
       
    40      * until an element which requires it is actually appended.
       
    41      *
       
    42      * @var bool
       
    43      */
       
    44     protected $_called = false;
       
    45     
       
    46     /**
       
    47      * Render feed
       
    48      * 
       
    49      * @return void
       
    50      */
       
    51     public function render()
       
    52     {
       
    53         /**
       
    54          * RSS 2.0 only. Used mainly to include Atom links and
       
    55          * Pubsubhubbub Hub endpoint URIs under the Atom namespace
       
    56          */
       
    57         if (strtolower($this->getType()) == 'atom') {
       
    58             return;
       
    59         }
       
    60         $this->_setFeedLinks($this->_dom, $this->_base);
       
    61         $this->_setHubs($this->_dom, $this->_base);
       
    62         if ($this->_called) {
       
    63             $this->_appendNamespaces();
       
    64         }
       
    65     }
       
    66     
       
    67     /**
       
    68      * Append namespaces to root element of feed
       
    69      * 
       
    70      * @return void
       
    71      */
       
    72     protected function _appendNamespaces()
       
    73     {
       
    74         $this->getRootElement()->setAttribute('xmlns:atom',
       
    75             'http://www.w3.org/2005/Atom');  
       
    76     }
       
    77 
       
    78     /**
       
    79      * Set feed link elements
       
    80      * 
       
    81      * @param  DOMDocument $dom 
       
    82      * @param  DOMElement $root 
       
    83      * @return void
       
    84      */
       
    85     protected function _setFeedLinks(DOMDocument $dom, DOMElement $root)
       
    86     {
       
    87         $flinks = $this->getDataContainer()->getFeedLinks();
       
    88         if(!$flinks || empty($flinks)) {
       
    89             return;
       
    90         }
       
    91         foreach ($flinks as $type => $href) {
       
    92             $mime  = 'application/' . strtolower($type) . '+xml';
       
    93             $flink = $dom->createElement('atom:link');
       
    94             $root->appendChild($flink);
       
    95             $flink->setAttribute('rel', 'self');
       
    96             $flink->setAttribute('type', $mime);
       
    97             $flink->setAttribute('href', $href);
       
    98         }
       
    99         $this->_called = true;
       
   100     }
       
   101     
       
   102     /**
       
   103      * Set PuSH hubs
       
   104      * 
       
   105      * @param  DOMDocument $dom 
       
   106      * @param  DOMElement $root 
       
   107      * @return void
       
   108      */
       
   109     protected function _setHubs(DOMDocument $dom, DOMElement $root)
       
   110     {
       
   111         $hubs = $this->getDataContainer()->getHubs();
       
   112         if (!$hubs || empty($hubs)) {
       
   113             return;
       
   114         }
       
   115         foreach ($hubs as $hubUrl) {
       
   116             $hub = $dom->createElement('atom:link');
       
   117             $hub->setAttribute('rel', 'hub');
       
   118             $hub->setAttribute('href', $hubUrl);
       
   119             $root->appendChild($hub);
       
   120         }
       
   121         $this->_called = true;
       
   122     }
       
   123 }