File indexing completed on 2024-06-30 05:58:09

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_ITunes_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         $this->_setAuthors($this->_dom, $this->_base);
0054         $this->_setBlock($this->_dom, $this->_base);
0055         $this->_setCategories($this->_dom, $this->_base);
0056         $this->_setImage($this->_dom, $this->_base);
0057         $this->_setDuration($this->_dom, $this->_base);
0058         $this->_setExplicit($this->_dom, $this->_base);
0059         $this->_setKeywords($this->_dom, $this->_base);
0060         $this->_setNewFeedUrl($this->_dom, $this->_base);
0061         $this->_setOwners($this->_dom, $this->_base);
0062         $this->_setSubtitle($this->_dom, $this->_base);
0063         $this->_setSummary($this->_dom, $this->_base);
0064         if ($this->_called) {
0065             $this->_appendNamespaces();
0066         }
0067     }
0068 
0069     /**
0070      * Append feed namespaces
0071      *
0072      * @return void
0073      */
0074     protected function _appendNamespaces()
0075     {
0076         $this->getRootElement()->setAttribute('xmlns:itunes',
0077             'http://www.itunes.com/dtds/podcast-1.0.dtd');
0078     }
0079 
0080     /**
0081      * Set feed authors
0082      *
0083      * @param  DOMDocument $dom
0084      * @param  DOMElement $root
0085      * @return void
0086      */
0087     protected function _setAuthors(DOMDocument $dom, DOMElement $root)
0088     {
0089         $authors = $this->getDataContainer()->getItunesAuthors();
0090         if (!$authors || empty($authors)) {
0091             return;
0092         }
0093         foreach ($authors as $author) {
0094             $el = $dom->createElement('itunes:author');
0095             $text = $dom->createTextNode($author);
0096             $el->appendChild($text);
0097             $root->appendChild($el);
0098         }
0099         $this->_called = true;
0100     }
0101 
0102     /**
0103      * Set feed itunes block
0104      *
0105      * @param  DOMDocument $dom
0106      * @param  DOMElement $root
0107      * @return void
0108      */
0109     protected function _setBlock(DOMDocument $dom, DOMElement $root)
0110     {
0111         $block = $this->getDataContainer()->getItunesBlock();
0112         if ($block === null) {
0113             return;
0114         }
0115         $el = $dom->createElement('itunes:block');
0116         $text = $dom->createTextNode($block);
0117         $el->appendChild($text);
0118         $root->appendChild($el);
0119         $this->_called = true;
0120     }
0121 
0122     /**
0123      * Set feed categories
0124      *
0125      * @param  DOMDocument $dom
0126      * @param  DOMElement $root
0127      * @return void
0128      */
0129     protected function _setCategories(DOMDocument $dom, DOMElement $root)
0130     {
0131         $cats = $this->getDataContainer()->getItunesCategories();
0132         if (!$cats || empty($cats)) {
0133             return;
0134         }
0135         foreach ($cats as $key=>$cat) {
0136             if (!is_array($cat)) {
0137                 $el = $dom->createElement('itunes:category');
0138                 $el->setAttribute('text', $cat);
0139                 $root->appendChild($el);
0140             } else {
0141                 $el = $dom->createElement('itunes:category');
0142                 $el->setAttribute('text', $key);
0143                 $root->appendChild($el);
0144                 foreach ($cat as $subcat) {
0145                     $el2 = $dom->createElement('itunes:category');
0146                     $el2->setAttribute('text', $subcat);
0147                     $el->appendChild($el2);
0148                 }
0149             }
0150         }
0151         $this->_called = true;
0152     }
0153 
0154     /**
0155      * Set feed image (icon)
0156      *
0157      * @param  DOMDocument $dom
0158      * @param  DOMElement $root
0159      * @return void
0160      */
0161     protected function _setImage(DOMDocument $dom, DOMElement $root)
0162     {
0163         $image = $this->getDataContainer()->getItunesImage();
0164         if (!$image) {
0165             return;
0166         }
0167         $el = $dom->createElement('itunes:image');
0168         $el->setAttribute('href', $image);
0169         $root->appendChild($el);
0170         $this->_called = true;
0171     }
0172 
0173     /**
0174      * Set feed cumulative duration
0175      *
0176      * @param  DOMDocument $dom
0177      * @param  DOMElement $root
0178      * @return void
0179      */
0180     protected function _setDuration(DOMDocument $dom, DOMElement $root)
0181     {
0182         $duration = $this->getDataContainer()->getItunesDuration();
0183         if (!$duration) {
0184             return;
0185         }
0186         $el = $dom->createElement('itunes:duration');
0187         $text = $dom->createTextNode($duration);
0188         $el->appendChild($text);
0189         $root->appendChild($el);
0190         $this->_called = true;
0191     }
0192 
0193     /**
0194      * Set explicit flag
0195      *
0196      * @param  DOMDocument $dom
0197      * @param  DOMElement $root
0198      * @return void
0199      */
0200     protected function _setExplicit(DOMDocument $dom, DOMElement $root)
0201     {
0202         $explicit = $this->getDataContainer()->getItunesExplicit();
0203         if ($explicit === null) {
0204             return;
0205         }
0206         $el = $dom->createElement('itunes:explicit');
0207         $text = $dom->createTextNode($explicit);
0208         $el->appendChild($text);
0209         $root->appendChild($el);
0210         $this->_called = true;
0211     }
0212 
0213     /**
0214      * Set feed keywords
0215      *
0216      * @param  DOMDocument $dom
0217      * @param  DOMElement $root
0218      * @return void
0219      */
0220     protected function _setKeywords(DOMDocument $dom, DOMElement $root)
0221     {
0222         $keywords = $this->getDataContainer()->getItunesKeywords();
0223         if (!$keywords || empty($keywords)) {
0224             return;
0225         }
0226         $el = $dom->createElement('itunes:keywords');
0227         $text = $dom->createTextNode(implode(',', $keywords));
0228         $el->appendChild($text);
0229         $root->appendChild($el);
0230         $this->_called = true;
0231     }
0232 
0233     /**
0234      * Set feed's new URL
0235      *
0236      * @param  DOMDocument $dom
0237      * @param  DOMElement $root
0238      * @return void
0239      */
0240     protected function _setNewFeedUrl(DOMDocument $dom, DOMElement $root)
0241     {
0242         $url = $this->getDataContainer()->getItunesNewFeedUrl();
0243         if (!$url) {
0244             return;
0245         }
0246         $el = $dom->createElement('itunes:new-feed-url');
0247         $text = $dom->createTextNode($url);
0248         $el->appendChild($text);
0249         $root->appendChild($el);
0250         $this->_called = true;
0251     }
0252 
0253     /**
0254      * Set feed owners
0255      *
0256      * @param  DOMDocument $dom
0257      * @param  DOMElement $root
0258      * @return void
0259      */
0260     protected function _setOwners(DOMDocument $dom, DOMElement $root)
0261     {
0262         $owners = $this->getDataContainer()->getItunesOwners();
0263         if (!$owners || empty($owners)) {
0264             return;
0265         }
0266         foreach ($owners as $owner) {
0267             $el = $dom->createElement('itunes:owner');
0268             $name = $dom->createElement('itunes:name');
0269             $text = $dom->createTextNode($owner['name']);
0270             $name->appendChild($text);
0271             $email = $dom->createElement('itunes:email');
0272             $text = $dom->createTextNode($owner['email']);
0273             $email->appendChild($text);
0274             $root->appendChild($el);
0275             $el->appendChild($name);
0276             $el->appendChild($email);
0277         }
0278         $this->_called = true;
0279     }
0280 
0281     /**
0282      * Set feed subtitle
0283      *
0284      * @param  DOMDocument $dom
0285      * @param  DOMElement $root
0286      * @return void
0287      */
0288     protected function _setSubtitle(DOMDocument $dom, DOMElement $root)
0289     {
0290         $subtitle = $this->getDataContainer()->getItunesSubtitle();
0291         if (!$subtitle) {
0292             return;
0293         }
0294         $el = $dom->createElement('itunes:subtitle');
0295         $text = $dom->createTextNode($subtitle);
0296         $el->appendChild($text);
0297         $root->appendChild($el);
0298         $this->_called = true;
0299     }
0300 
0301     /**
0302      * Set feed summary
0303      *
0304      * @param  DOMDocument $dom
0305      * @param  DOMElement $root
0306      * @return void
0307      */
0308     protected function _setSummary(DOMDocument $dom, DOMElement $root)
0309     {
0310         $summary = $this->getDataContainer()->getItunesSummary();
0311         if (!$summary) {
0312             return;
0313         }
0314         $el = $dom->createElement('itunes:summary');
0315         $text = $dom->createTextNode($summary);
0316         $el->appendChild($text);
0317         $root->appendChild($el);
0318         $this->_called = true;
0319     }
0320 }