web/enmi/Zend/Feed/Writer/Extension/ITunes/Feed.php
changeset 19 1c2f13fd785c
parent 0 4eba9c11703f
equal deleted inserted replaced
18:bd595ad770fc 19:1c2f13fd785c
       
     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 20096 2010-01-06 02:05:09Z bkarwin $
       
    20  */
       
    21  
       
    22 /**
       
    23  * @category   Zend
       
    24  * @package    Zend_Feed_Writer
       
    25  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    26  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    27  */
       
    28 class Zend_Feed_Writer_Extension_ITunes_Feed
       
    29 {
       
    30     /**
       
    31      * Array of Feed data for rendering by Extension's renderers
       
    32      *
       
    33      * @var array
       
    34      */
       
    35     protected $_data = array();
       
    36     
       
    37     /**
       
    38      * Encoding of all text values
       
    39      *
       
    40      * @var string
       
    41      */
       
    42     protected $_encoding = 'UTF-8';
       
    43     
       
    44     /**
       
    45      * Set feed encoding
       
    46      * 
       
    47      * @param  string $enc 
       
    48      * @return Zend_Feed_Writer_Extension_ITunes_Feed
       
    49      */
       
    50     public function setEncoding($enc)
       
    51     {
       
    52         $this->_encoding = $enc;
       
    53         return $this;
       
    54     }
       
    55     
       
    56     /**
       
    57      * Get feed encoding
       
    58      * 
       
    59      * @return string
       
    60      */
       
    61     public function getEncoding()
       
    62     {
       
    63         return $this->_encoding;
       
    64     }
       
    65     
       
    66     /**
       
    67      * Set a block value of "yes" or "no". You may also set an empty string.
       
    68      *
       
    69      * @param  string
       
    70      * @return Zend_Feed_Writer_Extension_ITunes_Feed
       
    71      */
       
    72     public function setItunesBlock($value)
       
    73     {
       
    74         if (!ctype_alpha($value) && strlen($value) > 0) {
       
    75             require_once 'Zend/Feed/Exception.php';
       
    76             throw new Zend_Feed_Exception('invalid parameter: "block" may only'
       
    77             . ' contain alphabetic characters');
       
    78         }
       
    79         if (iconv_strlen($value, $this->getEncoding()) > 255) {
       
    80             require_once 'Zend/Feed/Exception.php';
       
    81             throw new Zend_Feed_Exception('invalid parameter: "block" may only'
       
    82             . ' contain a maximum of 255 characters');
       
    83         }
       
    84         $this->_data['block'] = $value;
       
    85         return $this;
       
    86     }
       
    87     
       
    88     /**
       
    89      * Add feed authors
       
    90      * 
       
    91      * @param  array $values 
       
    92      * @return Zend_Feed_Writer_Extension_ITunes_Feed
       
    93      */
       
    94     public function addItunesAuthors(array $values)
       
    95     {
       
    96         foreach ($values as $value) {
       
    97             $this->addItunesAuthor($value);
       
    98         }
       
    99         return $this;
       
   100     }
       
   101     
       
   102     /**
       
   103      * Add feed author
       
   104      * 
       
   105      * @param  string $value 
       
   106      * @return Zend_Feed_Writer_Extension_ITunes_Feed
       
   107      */
       
   108     public function addItunesAuthor($value)
       
   109     {
       
   110         if (iconv_strlen($value, $this->getEncoding()) > 255) {
       
   111             require_once 'Zend/Feed/Exception.php';
       
   112             throw new Zend_Feed_Exception('invalid parameter: any "author" may only'
       
   113             . ' contain a maximum of 255 characters each');
       
   114         }
       
   115         if (!isset($this->_data['authors'])) {
       
   116             $this->_data['authors'] = array();
       
   117         }
       
   118         $this->_data['authors'][] = $value;   
       
   119         return $this;
       
   120     }
       
   121     
       
   122     /**
       
   123      * Set feed categories
       
   124      * 
       
   125      * @param  array $values 
       
   126      * @return Zend_Feed_Writer_Extension_ITunes_Feed
       
   127      */
       
   128     public function setItunesCategories(array $values)
       
   129     {
       
   130         if (!isset($this->_data['categories'])) {
       
   131             $this->_data['categories'] = array();
       
   132         }
       
   133         foreach ($values as $key=>$value) {
       
   134             if (!is_array($value)) {
       
   135                 if (iconv_strlen($value, $this->getEncoding()) > 255) {
       
   136                     require_once 'Zend/Feed/Exception.php';
       
   137                     throw new Zend_Feed_Exception('invalid parameter: any "category" may only'
       
   138                     . ' contain a maximum of 255 characters each');
       
   139                 }
       
   140                 $this->_data['categories'][] = $value;
       
   141             } else {
       
   142                 if (iconv_strlen($key, $this->getEncoding()) > 255) {
       
   143                     require_once 'Zend/Feed/Exception.php';
       
   144                     throw new Zend_Feed_Exception('invalid parameter: any "category" may only'
       
   145                     . ' contain a maximum of 255 characters each');
       
   146                 }
       
   147                 $this->_data['categories'][$key] = array();
       
   148                 foreach ($value as $val) {
       
   149                     if (iconv_strlen($val, $this->getEncoding()) > 255) {
       
   150                         require_once 'Zend/Feed/Exception.php';
       
   151                         throw new Zend_Feed_Exception('invalid parameter: any "category" may only'
       
   152                         . ' contain a maximum of 255 characters each');
       
   153                     }
       
   154                     $this->_data['categories'][$key][] = $val;
       
   155                 } 
       
   156             }
       
   157         }
       
   158         return $this;
       
   159     }
       
   160     
       
   161     /**
       
   162      * Set feed image (icon)
       
   163      * 
       
   164      * @param  string $value 
       
   165      * @return Zend_Feed_Writer_Extension_ITunes_Feed
       
   166      */
       
   167     public function setItunesImage($value)
       
   168     {
       
   169         if (!Zend_Uri::check($value)) {
       
   170             require_once 'Zend/Feed/Exception.php';
       
   171             throw new Zend_Feed_Exception('invalid parameter: "image" may only'
       
   172             . ' be a valid URI/IRI');
       
   173         }
       
   174         if (!in_array(substr($value, -3), array('jpg','png'))) {
       
   175             require_once 'Zend/Feed/Exception.php';
       
   176             throw new Zend_Feed_Exception('invalid parameter: "image" may only'
       
   177             . ' use file extension "jpg" or "png" which must be the last three'
       
   178             . ' characters of the URI (i.e. no query string or fragment)');
       
   179         }
       
   180         $this->_data['image'] = $value;
       
   181         return $this;
       
   182     }
       
   183     
       
   184     /**
       
   185      * Set feed cumulative duration
       
   186      * 
       
   187      * @param  string $value 
       
   188      * @return Zend_Feed_Writer_Extension_ITunes_Feed
       
   189      */
       
   190     public function setItunesDuration($value)
       
   191     {
       
   192         $value = (string) $value;
       
   193         if (!ctype_digit($value)
       
   194             && !preg_match("/^\d+:[0-5]{1}[0-9]{1}$/", $value)
       
   195             && !preg_match("/^\d+:[0-5]{1}[0-9]{1}:[0-5]{1}[0-9]{1}$/", $value)
       
   196         ) {
       
   197             require_once 'Zend/Feed/Exception.php';
       
   198             throw new Zend_Feed_Exception('invalid parameter: "duration" may only'
       
   199             . ' be of a specified [[HH:]MM:]SS format');
       
   200         }
       
   201         $this->_data['duration'] = $value;
       
   202         return $this;
       
   203     }
       
   204     
       
   205     /**
       
   206      * Set "explicit" flag
       
   207      * 
       
   208      * @param  bool $value 
       
   209      * @return Zend_Feed_Writer_Extension_ITunes_Feed
       
   210      */
       
   211     public function setItunesExplicit($value)
       
   212     {
       
   213         if (!in_array($value, array('yes','no','clean'))) {
       
   214             require_once 'Zend/Feed/Exception.php';
       
   215             throw new Zend_Feed_Exception('invalid parameter: "explicit" may only'
       
   216             . ' be one of "yes", "no" or "clean"');
       
   217         }
       
   218         $this->_data['explicit'] = $value;
       
   219         return $this;
       
   220     }
       
   221     
       
   222     /**
       
   223      * Set feed keywords
       
   224      * 
       
   225      * @param  array $value 
       
   226      * @return Zend_Feed_Writer_Extension_ITunes_Feed
       
   227      */
       
   228     public function setItunesKeywords(array $value)
       
   229     {
       
   230         if (count($value) > 12) {
       
   231             require_once 'Zend/Feed/Exception.php';
       
   232             throw new Zend_Feed_Exception('invalid parameter: "keywords" may only'
       
   233             . ' contain a maximum of 12 terms');
       
   234         }
       
   235         $concat = implode(',', $value);
       
   236         if (iconv_strlen($concat, $this->getEncoding()) > 255) {
       
   237             require_once 'Zend/Feed/Exception.php';
       
   238             throw new Zend_Feed_Exception('invalid parameter: "keywords" may only'
       
   239             . ' have a concatenated length of 255 chars where terms are delimited'
       
   240             . ' by a comma');
       
   241         }
       
   242         $this->_data['keywords'] = $value;
       
   243         return $this;
       
   244     }
       
   245     
       
   246     /**
       
   247      * Set new feed URL
       
   248      * 
       
   249      * @param  string $value 
       
   250      * @return Zend_Feed_Writer_Extension_ITunes_Feed
       
   251      */
       
   252     public function setItunesNewFeedUrl($value)
       
   253     {
       
   254         if (!Zend_Uri::check($value)) {
       
   255             require_once 'Zend/Feed/Exception.php';
       
   256             throw new Zend_Feed_Exception('invalid parameter: "newFeedUrl" may only'
       
   257             . ' be a valid URI/IRI');
       
   258         }
       
   259         $this->_data['newFeedUrl'] = $value;
       
   260         return $this;
       
   261     }
       
   262     
       
   263     /**
       
   264      * Add feed owners
       
   265      * 
       
   266      * @param  array $values 
       
   267      * @return Zend_Feed_Writer_Extension_ITunes_Feed
       
   268      */
       
   269     public function addItunesOwners(array $values)
       
   270     {
       
   271         foreach ($values as $value) {
       
   272             $this->addItunesOwner($value); 
       
   273         }
       
   274         return $this;
       
   275     }
       
   276     
       
   277     /**
       
   278      * Add feed owner
       
   279      * 
       
   280      * @param  string $value 
       
   281      * @return Zend_Feed_Writer_Extension_ITunes_Feed
       
   282      */
       
   283     public function addItunesOwner(array $value)
       
   284     {
       
   285         if (!isset($value['name']) || !isset($value['email'])) {
       
   286             require_once 'Zend/Feed/Exception.php';
       
   287             throw new Zend_Feed_Exception('invalid parameter: any "owner" must'
       
   288             . ' be an array containing keys "name" and "email"');
       
   289         }
       
   290         if (iconv_strlen($value['name'], $this->getEncoding()) > 255
       
   291             || iconv_strlen($value['email'], $this->getEncoding()) > 255
       
   292         ) {
       
   293             require_once 'Zend/Feed/Exception.php';
       
   294             throw new Zend_Feed_Exception('invalid parameter: any "owner" may only'
       
   295             . ' contain a maximum of 255 characters each for "name" and "email"');
       
   296         }
       
   297         if (!isset($this->_data['owners'])) {
       
   298             $this->_data['owners'] = array();
       
   299         }
       
   300         $this->_data['owners'][] = $value;
       
   301         return $this;
       
   302     }
       
   303     
       
   304     /**
       
   305      * Set feed subtitle
       
   306      * 
       
   307      * @param  string $value 
       
   308      * @return Zend_Feed_Writer_Extension_ITunes_Feed
       
   309      */
       
   310     public function setItunesSubtitle($value)
       
   311     {
       
   312         if (iconv_strlen($value, $this->getEncoding()) > 255) {
       
   313             require_once 'Zend/Feed/Exception.php';
       
   314             throw new Zend_Feed_Exception('invalid parameter: "subtitle" may only'
       
   315             . ' contain a maximum of 255 characters');
       
   316         }
       
   317         $this->_data['subtitle'] = $value;
       
   318         return $this;
       
   319     }
       
   320     
       
   321     /**
       
   322      * Set feed summary
       
   323      * 
       
   324      * @param  string $value 
       
   325      * @return Zend_Feed_Writer_Extension_ITunes_Feed
       
   326      */
       
   327     public function setItunesSummary($value)
       
   328     {
       
   329         if (iconv_strlen($value, $this->getEncoding()) > 4000) {
       
   330             require_once 'Zend/Feed/Exception.php';
       
   331             throw new Zend_Feed_Exception('invalid parameter: "summary" may only'
       
   332             . ' contain a maximum of 4000 characters');
       
   333         }
       
   334         $this->_data['summary'] = $value;
       
   335         return $this;
       
   336     }
       
   337     
       
   338     /**
       
   339      * Overloading: proxy to internal setters
       
   340      * 
       
   341      * @param  string $method 
       
   342      * @param  array $params 
       
   343      * @return mixed
       
   344      */
       
   345     public function __call($method, array $params)
       
   346     {
       
   347         $point = Zend_Feed_Writer::lcfirst(substr($method, 9));
       
   348         if (!method_exists($this, 'setItunes' . ucfirst($point))
       
   349             && !method_exists($this, 'addItunes' . ucfirst($point))
       
   350         ) {
       
   351             require_once 'Zend/Feed/Writer/Exception/InvalidMethodException.php';
       
   352             throw new Zend_Feed_Writer_Exception_InvalidMethodException(
       
   353                 'invalid method: ' . $method
       
   354             );
       
   355         }
       
   356         if (!array_key_exists($point, $this->_data) || empty($this->_data[$point])) {
       
   357             return null;
       
   358         }
       
   359         return $this->_data[$point];
       
   360     }
       
   361 }