web/lib/Zend/Feed/Writer/Renderer/Entry/Atom.php
changeset 807 877f952ae2bd
parent 207 621fa6caec0c
child 1230 68c69c656a2c
equal deleted inserted replaced
805:5e7a0fedabdf 807:877f952ae2bd
    12  * obtain it through the world-wide-web, please send an email
    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.
    13  * to license@zend.com so we can send you a copy immediately.
    14  *
    14  *
    15  * @category   Zend
    15  * @category   Zend
    16  * @package    Zend_Feed_Writer
    16  * @package    Zend_Feed_Writer
    17  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
    17  * @copyright  Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
    18  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    18  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    19  * @version    $Id: Atom.php 22758 2010-08-01 19:23:27Z padraic $
    19  * @version    $Id: Atom.php 24593 2012-01-05 20:35:02Z matthew $
    20  */
    20  */
    21 
    21 
    22 /**
    22 /**
    23  * @see Zend_Feed_Writer_Renderer_RendererAbstract
    23  * @see Zend_Feed_Writer_Renderer_RendererAbstract
    24  */
    24  */
    27 require_once 'Zend/Feed/Writer/Renderer/Feed/Atom/Source.php';
    27 require_once 'Zend/Feed/Writer/Renderer/Feed/Atom/Source.php';
    28 
    28 
    29 /**
    29 /**
    30  * @category   Zend
    30  * @category   Zend
    31  * @package    Zend_Feed_Writer
    31  * @package    Zend_Feed_Writer
    32  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
    32  * @copyright  Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
    33  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    33  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    34  */
    34  */
    35 class Zend_Feed_Writer_Renderer_Entry_Atom
    35 class Zend_Feed_Writer_Renderer_Entry_Atom
    36     extends Zend_Feed_Writer_Renderer_RendererAbstract
    36     extends Zend_Feed_Writer_Renderer_RendererAbstract
    37     implements Zend_Feed_Writer_Renderer_RendererInterface
    37     implements Zend_Feed_Writer_Renderer_RendererInterface
    38 {
    38 {
    39     /**
    39     /**
    40      * Constructor
    40      * Constructor
    41      * 
    41      *
    42      * @param  Zend_Feed_Writer_Entry $container 
    42      * @param  Zend_Feed_Writer_Entry $container
    43      * @return void
    43      * @return void
    44      */
    44      */
    45     public function __construct (Zend_Feed_Writer_Entry $container)
    45     public function __construct (Zend_Feed_Writer_Entry $container)
    46     {
    46     {
    47         parent::__construct($container);
    47         parent::__construct($container);
    48     }
    48     }
    49 
    49 
    50     /**
    50     /**
    51      * Render atom entry
    51      * Render atom entry
    52      * 
    52      *
    53      * @return Zend_Feed_Writer_Renderer_Entry_Atom
    53      * @return Zend_Feed_Writer_Renderer_Entry_Atom
    54      */
    54      */
    55     public function render()
    55     public function render()
    56     {
    56     {
    57         $this->_dom = new DOMDocument('1.0', $this->_container->getEncoding());
    57         $this->_dom = new DOMDocument('1.0', $this->_container->getEncoding());
    58         $this->_dom->formatOutput = true;
    58         $this->_dom->formatOutput = true;
    59         $entry = $this->_dom->createElementNS(Zend_Feed_Writer::NAMESPACE_ATOM_10, 'entry');
    59         $entry = $this->_dom->createElementNS(Zend_Feed_Writer::NAMESPACE_ATOM_10, 'entry');
    60         $this->_dom->appendChild($entry);
    60         $this->_dom->appendChild($entry);
    61         
    61 
    62         $this->_setSource($this->_dom, $entry);
    62         $this->_setSource($this->_dom, $entry);
    63         $this->_setTitle($this->_dom, $entry);
    63         $this->_setTitle($this->_dom, $entry);
    64         $this->_setDescription($this->_dom, $entry);
    64         $this->_setDescription($this->_dom, $entry);
    65         $this->_setDateCreated($this->_dom, $entry);
    65         $this->_setDateCreated($this->_dom, $entry);
    66         $this->_setDateModified($this->_dom, $entry);
    66         $this->_setDateModified($this->_dom, $entry);
    75             $ext->setType($this->getType());
    75             $ext->setType($this->getType());
    76             $ext->setRootElement($this->getRootElement());
    76             $ext->setRootElement($this->getRootElement());
    77             $ext->setDomDocument($this->getDomDocument(), $entry);
    77             $ext->setDomDocument($this->getDomDocument(), $entry);
    78             $ext->render();
    78             $ext->render();
    79         }
    79         }
    80         
    80 
    81         return $this;
    81         return $this;
    82     }
    82     }
    83     
    83 
    84     /**
    84     /**
    85      * Set entry title
    85      * Set entry title
    86      * 
    86      *
    87      * @param  DOMDocument $dom 
    87      * @param  DOMDocument $dom
    88      * @param  DOMElement $root 
    88      * @param  DOMElement $root
    89      * @return void
    89      * @return void
    90      */
    90      */
    91     protected function _setTitle(DOMDocument $dom, DOMElement $root)
    91     protected function _setTitle(DOMDocument $dom, DOMElement $root)
    92     {
    92     {
    93         if(!$this->getDataContainer()->getTitle()) {
    93         if(!$this->getDataContainer()->getTitle()) {
   106         $root->appendChild($title);
   106         $root->appendChild($title);
   107         $title->setAttribute('type', 'html');
   107         $title->setAttribute('type', 'html');
   108         $cdata = $dom->createCDATASection($this->getDataContainer()->getTitle());
   108         $cdata = $dom->createCDATASection($this->getDataContainer()->getTitle());
   109         $title->appendChild($cdata);
   109         $title->appendChild($cdata);
   110     }
   110     }
   111     
   111 
   112     /**
   112     /**
   113      * Set entry description
   113      * Set entry description
   114      * 
   114      *
   115      * @param  DOMDocument $dom 
   115      * @param  DOMDocument $dom
   116      * @param  DOMElement $root 
   116      * @param  DOMElement $root
   117      * @return void
   117      * @return void
   118      */
   118      */
   119     protected function _setDescription(DOMDocument $dom, DOMElement $root)
   119     protected function _setDescription(DOMDocument $dom, DOMElement $root)
   120     {
   120     {
   121         if(!$this->getDataContainer()->getDescription()) {
   121         if(!$this->getDataContainer()->getDescription()) {
   127         $cdata = $dom->createCDATASection(
   127         $cdata = $dom->createCDATASection(
   128             $this->getDataContainer()->getDescription()
   128             $this->getDataContainer()->getDescription()
   129         );
   129         );
   130         $subtitle->appendChild($cdata);
   130         $subtitle->appendChild($cdata);
   131     }
   131     }
   132     
   132 
   133     /**
   133     /**
   134      * Set date entry was modified
   134      * Set date entry was modified
   135      * 
   135      *
   136      * @param  DOMDocument $dom 
   136      * @param  DOMDocument $dom
   137      * @param  DOMElement $root 
   137      * @param  DOMElement $root
   138      * @return void
   138      * @return void
   139      */
   139      */
   140     protected function _setDateModified(DOMDocument $dom, DOMElement $root)
   140     protected function _setDateModified(DOMDocument $dom, DOMElement $root)
   141     {
   141     {
   142         if(!$this->getDataContainer()->getDateModified()) {
   142         if(!$this->getDataContainer()->getDateModified()) {
   157         $text = $dom->createTextNode(
   157         $text = $dom->createTextNode(
   158             $this->getDataContainer()->getDateModified()->get(Zend_Date::ISO_8601)
   158             $this->getDataContainer()->getDateModified()->get(Zend_Date::ISO_8601)
   159         );
   159         );
   160         $updated->appendChild($text);
   160         $updated->appendChild($text);
   161     }
   161     }
   162     
   162 
   163     /**
   163     /**
   164      * Set date entry was created
   164      * Set date entry was created
   165      * 
   165      *
   166      * @param  DOMDocument $dom 
   166      * @param  DOMDocument $dom
   167      * @param  DOMElement $root 
   167      * @param  DOMElement $root
   168      * @return void
   168      * @return void
   169      */
   169      */
   170     protected function _setDateCreated(DOMDocument $dom, DOMElement $root)
   170     protected function _setDateCreated(DOMDocument $dom, DOMElement $root)
   171     {
   171     {
   172         if (!$this->getDataContainer()->getDateCreated()) {
   172         if (!$this->getDataContainer()->getDateCreated()) {
   177         $text = $dom->createTextNode(
   177         $text = $dom->createTextNode(
   178             $this->getDataContainer()->getDateCreated()->get(Zend_Date::ISO_8601)
   178             $this->getDataContainer()->getDateCreated()->get(Zend_Date::ISO_8601)
   179         );
   179         );
   180         $el->appendChild($text);
   180         $el->appendChild($text);
   181     }
   181     }
   182     
   182 
   183     /**
   183     /**
   184      * Set entry authors 
   184      * Set entry authors
   185      * 
   185      *
   186      * @param  DOMDocument $dom 
   186      * @param  DOMDocument $dom
   187      * @param  DOMElement $root 
   187      * @param  DOMElement $root
   188      * @return void
   188      * @return void
   189      */
   189      */
   190     protected function _setAuthors(DOMDocument $dom, DOMElement $root)
   190     protected function _setAuthors(DOMDocument $dom, DOMElement $root)
   191     {
   191     {
   192         $authors = $this->_container->getAuthors();
   192         $authors = $this->_container->getAuthors();
   216                 $text = $dom->createTextNode($data['uri']);
   216                 $text = $dom->createTextNode($data['uri']);
   217                 $uri->appendChild($text);
   217                 $uri->appendChild($text);
   218             }
   218             }
   219         }
   219         }
   220     }
   220     }
   221     
   221 
   222     /**
   222     /**
   223      * Set entry enclosure
   223      * Set entry enclosure
   224      * 
   224      *
   225      * @param  DOMDocument $dom 
   225      * @param  DOMDocument $dom
   226      * @param  DOMElement $root 
   226      * @param  DOMElement $root
   227      * @return void
   227      * @return void
   228      */
   228      */
   229     protected function _setEnclosure(DOMDocument $dom, DOMElement $root)
   229     protected function _setEnclosure(DOMDocument $dom, DOMElement $root)
   230     {
   230     {
   231         $data = $this->_container->getEnclosure();
   231         $data = $this->_container->getEnclosure();
   241             $enclosure->setAttribute('length', $data['length']);
   241             $enclosure->setAttribute('length', $data['length']);
   242         }
   242         }
   243         $enclosure->setAttribute('href', $data['uri']);
   243         $enclosure->setAttribute('href', $data['uri']);
   244         $root->appendChild($enclosure);
   244         $root->appendChild($enclosure);
   245     }
   245     }
   246     
   246 
   247     protected function _setLink(DOMDocument $dom, DOMElement $root)
   247     protected function _setLink(DOMDocument $dom, DOMElement $root)
   248     {
   248     {
   249         if(!$this->getDataContainer()->getLink()) {
   249         if(!$this->getDataContainer()->getLink()) {
   250             return;
   250             return;
   251         }
   251         }
   253         $root->appendChild($link);
   253         $root->appendChild($link);
   254         $link->setAttribute('rel', 'alternate');
   254         $link->setAttribute('rel', 'alternate');
   255         $link->setAttribute('type', 'text/html');
   255         $link->setAttribute('type', 'text/html');
   256         $link->setAttribute('href', $this->getDataContainer()->getLink());
   256         $link->setAttribute('href', $this->getDataContainer()->getLink());
   257     }
   257     }
   258     
   258 
   259     /**
   259     /**
   260      * Set entry identifier 
   260      * Set entry identifier
   261      * 
   261      *
   262      * @param  DOMDocument $dom 
   262      * @param  DOMDocument $dom
   263      * @param  DOMElement $root 
   263      * @param  DOMElement $root
   264      * @return void
   264      * @return void
   265      */
   265      */
   266     protected function _setId(DOMDocument $dom, DOMElement $root)
   266     protected function _setId(DOMDocument $dom, DOMElement $root)
   267     {
   267     {
   268         if(!$this->getDataContainer()->getId()
   268         if(!$this->getDataContainer()->getId()
   295         $id = $dom->createElement('id');
   295         $id = $dom->createElement('id');
   296         $root->appendChild($id);
   296         $root->appendChild($id);
   297         $text = $dom->createTextNode($this->getDataContainer()->getId());
   297         $text = $dom->createTextNode($this->getDataContainer()->getId());
   298         $id->appendChild($text);
   298         $id->appendChild($text);
   299     }
   299     }
   300     
   300 
   301     /**
   301     /**
   302      * Validate a URI using the tag scheme (RFC 4151)
   302      * Validate a URI using the tag scheme (RFC 4151)
   303      *
   303      *
   304      * @param string $id
   304      * @param string $id
   305      * @return bool
   305      * @return bool
   327             return $dvalid && $nvalid;
   327             return $dvalid && $nvalid;
   328 
   328 
   329         }
   329         }
   330         return false;
   330         return false;
   331     }
   331     }
   332     
   332 
   333     /**
   333     /**
   334      * Set entry content 
   334      * Set entry content
   335      * 
   335      *
   336      * @param  DOMDocument $dom 
   336      * @param  DOMDocument $dom
   337      * @param  DOMElement $root 
   337      * @param  DOMElement $root
   338      * @return void
   338      * @return void
   339      */
   339      */
   340     protected function _setContent(DOMDocument $dom, DOMElement $root)
   340     protected function _setContent(DOMDocument $dom, DOMElement $root)
   341     {
   341     {
   342         $content = $this->getDataContainer()->getContent();
   342         $content = $this->getDataContainer()->getContent();
   362         $xhtmlElement = $this->_loadXhtml($content);
   362         $xhtmlElement = $this->_loadXhtml($content);
   363         $xhtml = $dom->importNode($xhtmlElement, true);
   363         $xhtml = $dom->importNode($xhtmlElement, true);
   364         $element->appendChild($xhtml);
   364         $element->appendChild($xhtml);
   365         $root->appendChild($element);
   365         $root->appendChild($element);
   366     }
   366     }
   367     
   367 
   368     /**
   368     /**
   369      * Load a HTML string and attempt to normalise to XML
   369      * Load a HTML string and attempt to normalise to XML
   370      */
   370      */
   371     protected function _loadXhtml($content)
   371     protected function _loadXhtml($content)
   372     {
   372     {
   384             $xhtml = (string) $tidy;
   384             $xhtml = (string) $tidy;
   385         } else {
   385         } else {
   386             $xhtml = $content;
   386             $xhtml = $content;
   387         }
   387         }
   388         $xhtml = preg_replace(array(
   388         $xhtml = preg_replace(array(
   389             "/(<[\/]?)([a-zA-Z]+)/"   
   389             "/(<[\/]?)([a-zA-Z]+)/"
   390         ), '$1xhtml:$2', $xhtml);
   390         ), '$1xhtml:$2', $xhtml);
   391         $dom = new DOMDocument('1.0', $this->getEncoding());
   391         $dom = new DOMDocument('1.0', $this->getEncoding());
   392         $dom->loadXML('<xhtml:div xmlns:xhtml="http://www.w3.org/1999/xhtml">'
   392         $dom->loadXML('<xhtml:div xmlns:xhtml="http://www.w3.org/1999/xhtml">'
   393             . $xhtml . '</xhtml:div>');
   393             . $xhtml . '</xhtml:div>');
   394         return $dom->documentElement;
   394         return $dom->documentElement;
   395     }
   395     }
   396     
   396 
   397     /**
   397     /**
   398      * Set entry cateories 
   398      * Set entry cateories
   399      * 
   399      *
   400      * @param  DOMDocument $dom 
   400      * @param  DOMDocument $dom
   401      * @param  DOMElement $root 
   401      * @param  DOMElement $root
   402      * @return void
   402      * @return void
   403      */
   403      */
   404     protected function _setCategories(DOMDocument $dom, DOMElement $root)
   404     protected function _setCategories(DOMDocument $dom, DOMElement $root)
   405     {
   405     {
   406         $categories = $this->getDataContainer()->getCategories();
   406         $categories = $this->getDataContainer()->getCategories();
   419                 $category->setAttribute('scheme', $cat['scheme']);
   419                 $category->setAttribute('scheme', $cat['scheme']);
   420             }
   420             }
   421             $root->appendChild($category);
   421             $root->appendChild($category);
   422         }
   422         }
   423     }
   423     }
   424     
   424 
   425     /**
   425     /**
   426      * Append Source element (Atom 1.0 Feed Metadata)
   426      * Append Source element (Atom 1.0 Feed Metadata)
   427      *
   427      *
   428      * @param  DOMDocument $dom 
   428      * @param  DOMDocument $dom
   429      * @param  DOMElement $root 
   429      * @param  DOMElement $root
   430      * @return void
   430      * @return void
   431      */
   431      */
   432     protected function _setSource(DOMDocument $dom, DOMElement $root)
   432     protected function _setSource(DOMDocument $dom, DOMElement $root)
   433     {
   433     {
   434         $source = $this->getDataContainer()->getSource();
   434         $source = $this->getDataContainer()->getSource();
   437         }
   437         }
   438         $renderer = new Zend_Feed_Writer_Renderer_Feed_Atom_Source($source);
   438         $renderer = new Zend_Feed_Writer_Renderer_Feed_Atom_Source($source);
   439         $renderer->setType($this->getType());
   439         $renderer->setType($this->getType());
   440         $element = $renderer->render()->getElement();
   440         $element = $renderer->render()->getElement();
   441         $imported = $dom->importNode($element, true);
   441         $imported = $dom->importNode($element, true);
   442         $root->appendChild($imported); 
   442         $root->appendChild($imported);
   443     }
   443     }
   444 }
   444 }