File indexing completed on 2024-09-22 05:20:56

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  * @category   Zend
0024  * @package    Zend_Feed_Writer
0025  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0026  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0027  */
0028 class Zend_Feed_Writer_Extension_ITunes_Feed
0029 {
0030     /**
0031      * Array of Feed data for rendering by Extension's renderers
0032      *
0033      * @var array
0034      */
0035     protected $_data = array();
0036 
0037     /**
0038      * Encoding of all text values
0039      *
0040      * @var string
0041      */
0042     protected $_encoding = 'UTF-8';
0043 
0044     /**
0045      * Set feed encoding
0046      *
0047      * @param  string $enc
0048      * @return Zend_Feed_Writer_Extension_ITunes_Feed
0049      */
0050     public function setEncoding($enc)
0051     {
0052         $this->_encoding = $enc;
0053         return $this;
0054     }
0055 
0056     /**
0057      * Get feed encoding
0058      *
0059      * @return string
0060      */
0061     public function getEncoding()
0062     {
0063         return $this->_encoding;
0064     }
0065 
0066     /**
0067      * Set a block value of "yes" or "no". You may also set an empty string.
0068      *
0069      * @param  string
0070      * @return Zend_Feed_Writer_Extension_ITunes_Feed
0071      */
0072     public function setItunesBlock($value)
0073     {
0074         if (!ctype_alpha($value) && strlen($value) > 0) {
0075             // require_once 'Zend/Feed/Exception.php';
0076             throw new Zend_Feed_Exception('invalid parameter: "block" may only'
0077             . ' contain alphabetic characters');
0078         }
0079         if (iconv_strlen($value, $this->getEncoding()) > 255) {
0080             // require_once 'Zend/Feed/Exception.php';
0081             throw new Zend_Feed_Exception('invalid parameter: "block" may only'
0082             . ' contain a maximum of 255 characters');
0083         }
0084         $this->_data['block'] = $value;
0085         return $this;
0086     }
0087 
0088     /**
0089      * Add feed authors
0090      *
0091      * @param  array $values
0092      * @return Zend_Feed_Writer_Extension_ITunes_Feed
0093      */
0094     public function addItunesAuthors(array $values)
0095     {
0096         foreach ($values as $value) {
0097             $this->addItunesAuthor($value);
0098         }
0099         return $this;
0100     }
0101 
0102     /**
0103      * Add feed author
0104      *
0105      * @param  string $value
0106      * @return Zend_Feed_Writer_Extension_ITunes_Feed
0107      */
0108     public function addItunesAuthor($value)
0109     {
0110         if (iconv_strlen($value, $this->getEncoding()) > 255) {
0111             // require_once 'Zend/Feed/Exception.php';
0112             throw new Zend_Feed_Exception('invalid parameter: any "author" may only'
0113             . ' contain a maximum of 255 characters each');
0114         }
0115         if (!isset($this->_data['authors'])) {
0116             $this->_data['authors'] = array();
0117         }
0118         $this->_data['authors'][] = $value;
0119         return $this;
0120     }
0121 
0122     /**
0123      * Set feed categories
0124      *
0125      * @param  array $values
0126      * @return Zend_Feed_Writer_Extension_ITunes_Feed
0127      */
0128     public function setItunesCategories(array $values)
0129     {
0130         if (!isset($this->_data['categories'])) {
0131             $this->_data['categories'] = array();
0132         }
0133         foreach ($values as $key=>$value) {
0134             if (!is_array($value)) {
0135                 if (iconv_strlen($value, $this->getEncoding()) > 255) {
0136                     // require_once 'Zend/Feed/Exception.php';
0137                     throw new Zend_Feed_Exception('invalid parameter: any "category" may only'
0138                     . ' contain a maximum of 255 characters each');
0139                 }
0140                 $this->_data['categories'][] = $value;
0141             } else {
0142                 if (iconv_strlen($key, $this->getEncoding()) > 255) {
0143                     // require_once 'Zend/Feed/Exception.php';
0144                     throw new Zend_Feed_Exception('invalid parameter: any "category" may only'
0145                     . ' contain a maximum of 255 characters each');
0146                 }
0147                 $this->_data['categories'][$key] = array();
0148                 foreach ($value as $val) {
0149                     if (iconv_strlen($val, $this->getEncoding()) > 255) {
0150                         // require_once 'Zend/Feed/Exception.php';
0151                         throw new Zend_Feed_Exception('invalid parameter: any "category" may only'
0152                         . ' contain a maximum of 255 characters each');
0153                     }
0154                     $this->_data['categories'][$key][] = $val;
0155                 }
0156             }
0157         }
0158         return $this;
0159     }
0160 
0161     /**
0162      * Set feed image (icon)
0163      *
0164      * @param  string $value
0165      * @return Zend_Feed_Writer_Extension_ITunes_Feed
0166      */
0167     public function setItunesImage($value)
0168     {
0169         if (!Zend_Uri::check($value)) {
0170             // require_once 'Zend/Feed/Exception.php';
0171             throw new Zend_Feed_Exception('invalid parameter: "image" may only'
0172             . ' be a valid URI/IRI');
0173         }
0174         if (!in_array(substr($value, -3), array('jpg','png'))) {
0175             // require_once 'Zend/Feed/Exception.php';
0176             throw new Zend_Feed_Exception('invalid parameter: "image" may only'
0177             . ' use file extension "jpg" or "png" which must be the last three'
0178             . ' characters of the URI (i.e. no query string or fragment)');
0179         }
0180         $this->_data['image'] = $value;
0181         return $this;
0182     }
0183 
0184     /**
0185      * Set feed cumulative duration
0186      *
0187      * @param  string $value
0188      * @return Zend_Feed_Writer_Extension_ITunes_Feed
0189      */
0190     public function setItunesDuration($value)
0191     {
0192         $value = (string) $value;
0193         if (!ctype_digit($value)
0194             && !preg_match("/^\d+:[0-5]{1}[0-9]{1}$/", $value)
0195             && !preg_match("/^\d+:[0-5]{1}[0-9]{1}:[0-5]{1}[0-9]{1}$/", $value)
0196         ) {
0197             // require_once 'Zend/Feed/Exception.php';
0198             throw new Zend_Feed_Exception('invalid parameter: "duration" may only'
0199             . ' be of a specified [[HH:]MM:]SS format');
0200         }
0201         $this->_data['duration'] = $value;
0202         return $this;
0203     }
0204 
0205     /**
0206      * Set "explicit" flag
0207      *
0208      * @param  string $value    Valid values: "yes", "no" or "clean"
0209      * @return Zend_Feed_Writer_Extension_ITunes_Feed
0210      */
0211     public function setItunesExplicit($value)
0212     {
0213         if (!in_array($value, array('yes','no','clean'))) {
0214             // require_once 'Zend/Feed/Exception.php';
0215             throw new Zend_Feed_Exception('invalid parameter: "explicit" may only'
0216             . ' be one of "yes", "no" or "clean"');
0217         }
0218         $this->_data['explicit'] = $value;
0219         return $this;
0220     }
0221 
0222     /**
0223      * Set feed keywords
0224      *
0225      * @param  array $value
0226      * @return Zend_Feed_Writer_Extension_ITunes_Feed
0227      */
0228     public function setItunesKeywords(array $value)
0229     {
0230         if (count($value) > 12) {
0231             // require_once 'Zend/Feed/Exception.php';
0232             throw new Zend_Feed_Exception('invalid parameter: "keywords" may only'
0233             . ' contain a maximum of 12 terms');
0234         }
0235         $concat = implode(',', $value);
0236         if (iconv_strlen($concat, $this->getEncoding()) > 255) {
0237             // require_once 'Zend/Feed/Exception.php';
0238             throw new Zend_Feed_Exception('invalid parameter: "keywords" may only'
0239             . ' have a concatenated length of 255 chars where terms are delimited'
0240             . ' by a comma');
0241         }
0242         $this->_data['keywords'] = $value;
0243         return $this;
0244     }
0245 
0246     /**
0247      * Set new feed URL
0248      *
0249      * @param  string $value
0250      * @return Zend_Feed_Writer_Extension_ITunes_Feed
0251      */
0252     public function setItunesNewFeedUrl($value)
0253     {
0254         if (!Zend_Uri::check($value)) {
0255             // require_once 'Zend/Feed/Exception.php';
0256             throw new Zend_Feed_Exception('invalid parameter: "newFeedUrl" may only'
0257             . ' be a valid URI/IRI');
0258         }
0259         $this->_data['newFeedUrl'] = $value;
0260         return $this;
0261     }
0262 
0263     /**
0264      * Add feed owners
0265      *
0266      * @param  array $values
0267      * @return Zend_Feed_Writer_Extension_ITunes_Feed
0268      */
0269     public function addItunesOwners(array $values)
0270     {
0271         foreach ($values as $value) {
0272             $this->addItunesOwner($value);
0273         }
0274         return $this;
0275     }
0276 
0277     /**
0278      * Add feed owner
0279      *
0280      * @param  string $value
0281      * @return Zend_Feed_Writer_Extension_ITunes_Feed
0282      */
0283     public function addItunesOwner(array $value)
0284     {
0285         if (!isset($value['name']) || !isset($value['email'])) {
0286             // require_once 'Zend/Feed/Exception.php';
0287             throw new Zend_Feed_Exception('invalid parameter: any "owner" must'
0288             . ' be an array containing keys "name" and "email"');
0289         }
0290         if (iconv_strlen($value['name'], $this->getEncoding()) > 255
0291             || iconv_strlen($value['email'], $this->getEncoding()) > 255
0292         ) {
0293             // require_once 'Zend/Feed/Exception.php';
0294             throw new Zend_Feed_Exception('invalid parameter: any "owner" may only'
0295             . ' contain a maximum of 255 characters each for "name" and "email"');
0296         }
0297         if (!isset($this->_data['owners'])) {
0298             $this->_data['owners'] = array();
0299         }
0300         $this->_data['owners'][] = $value;
0301         return $this;
0302     }
0303 
0304     /**
0305      * Set feed subtitle
0306      *
0307      * @param  string $value
0308      * @return Zend_Feed_Writer_Extension_ITunes_Feed
0309      */
0310     public function setItunesSubtitle($value)
0311     {
0312         if (iconv_strlen($value, $this->getEncoding()) > 255) {
0313             // require_once 'Zend/Feed/Exception.php';
0314             throw new Zend_Feed_Exception('invalid parameter: "subtitle" may only'
0315             . ' contain a maximum of 255 characters');
0316         }
0317         $this->_data['subtitle'] = $value;
0318         return $this;
0319     }
0320 
0321     /**
0322      * Set feed summary
0323      *
0324      * @param  string $value
0325      * @return Zend_Feed_Writer_Extension_ITunes_Feed
0326      */
0327     public function setItunesSummary($value)
0328     {
0329         if (iconv_strlen($value, $this->getEncoding()) > 4000) {
0330             // require_once 'Zend/Feed/Exception.php';
0331             throw new Zend_Feed_Exception('invalid parameter: "summary" may only'
0332             . ' contain a maximum of 4000 characters');
0333         }
0334         $this->_data['summary'] = $value;
0335         return $this;
0336     }
0337 
0338     /**
0339      * Overloading: proxy to internal setters
0340      *
0341      * @param  string $method
0342      * @param  array $params
0343      * @return mixed
0344      */
0345     public function __call($method, array $params)
0346     {
0347         $point = Zend_Feed_Writer::lcfirst(substr($method, 9));
0348         if (!method_exists($this, 'setItunes' . ucfirst($point))
0349             && !method_exists($this, 'addItunes' . ucfirst($point))
0350         ) {
0351             // require_once 'Zend/Feed/Writer/Exception/InvalidMethodException.php';
0352             throw new Zend_Feed_Writer_Exception_InvalidMethodException(
0353                 'invalid method: ' . $method
0354             );
0355         }
0356         if (!array_key_exists($point, $this->_data) || empty($this->_data[$point])) {
0357             return null;
0358         }
0359         return $this->_data[$point];
0360     }
0361 }