File indexing completed on 2024-12-22 05:36:40

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_Date
0024  */
0025 // require_once 'Zend/Date.php';
0026 
0027 /**
0028  * @see Zend_Date
0029  */
0030 // require_once 'Zend/Uri.php';
0031 
0032 // require_once 'Zend/Feed/Writer/Source.php';
0033 
0034 /**
0035  * @category   Zend
0036  * @package    Zend_Feed_Writer
0037  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0038  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0039  */
0040 class Zend_Feed_Writer_Entry
0041 {
0042 
0043     /**
0044      * Internal array containing all data associated with this entry or item.
0045      *
0046      * @var array
0047      */
0048     protected $_data = array();
0049 
0050     /**
0051      * Registered extensions
0052      *
0053      * @var array
0054      */
0055     protected $_extensions = array();
0056 
0057     /**
0058      * Holds the value "atom" or "rss" depending on the feed type set when
0059      * when last exported.
0060      *
0061      * @var string
0062      */
0063     protected $_type = null;
0064 
0065     /**
0066      * Constructor: Primarily triggers the registration of core extensions and
0067      * loads those appropriate to this data container.
0068      *
0069      * @return void
0070      */
0071     public function __construct()
0072     {
0073         Zend_Feed_Writer::registerCoreExtensions();
0074         $this->_loadExtensions();
0075     }
0076 
0077     /**
0078      * Set a single author
0079      *
0080      * @param  int $index
0081      * @return string|null
0082      */
0083     public function addAuthor($name, $email = null, $uri = null)
0084     {
0085         $author = array();
0086         if (is_array($name)) {
0087             if (!array_key_exists('name', $name)
0088                 || empty($name['name'])
0089                 || !is_string($name['name'])
0090             ) {
0091                 // require_once 'Zend/Feed/Exception.php';
0092                 throw new Zend_Feed_Exception('Invalid parameter: author array must include a "name" key with a non-empty string value');
0093             }
0094             $author['name'] = $name['name'];
0095             if (isset($name['email'])) {
0096                 if (empty($name['email']) || !is_string($name['email'])) {
0097                     // require_once 'Zend/Feed/Exception.php';
0098                     throw new Zend_Feed_Exception('Invalid parameter: "email" array value must be a non-empty string');
0099                 }
0100                 $author['email'] = $name['email'];
0101             }
0102             if (isset($name['uri'])) {
0103                 if (empty($name['uri'])
0104                     || !is_string($name['uri'])
0105                     || !Zend_Uri::check($name['uri'])
0106                 ) {
0107                     // require_once 'Zend/Feed/Exception.php';
0108                     throw new Zend_Feed_Exception('Invalid parameter: "uri" array value must be a non-empty string and valid URI/IRI');
0109                 }
0110                 $author['uri'] = $name['uri'];
0111             }
0112         /**
0113          * @deprecated
0114          * Array notation (above) is preferred and will be the sole supported input from ZF 2.0
0115          */
0116         } else {
0117             if (empty($name) || !is_string($name)) {
0118                 // require_once 'Zend/Feed/Exception.php';
0119                 throw new Zend_Feed_Exception('Invalid parameter: "name" must be a non-empty string value');
0120             }
0121             $author['name'] = $name;
0122             if (isset($email)) {
0123                 if (empty($email) || !is_string($email)) {
0124                     // require_once 'Zend/Feed/Exception.php';
0125                     throw new Zend_Feed_Exception('Invalid parameter: "email" value must be a non-empty string');
0126                 }
0127                 $author['email'] = $email;
0128             }
0129             if (isset($uri)) {
0130                 if (empty($uri) || !is_string($uri) || !Zend_Uri::check($uri)) {
0131                     // require_once 'Zend/Feed/Exception.php';
0132                     throw new Zend_Feed_Exception('Invalid parameter: "uri" value must be a non-empty string and valid URI/IRI');
0133                 }
0134                 $author['uri'] = $uri;
0135             }
0136         }
0137         $this->_data['authors'][] = $author;
0138     }
0139 
0140     /**
0141      * Set an array with feed authors
0142      *
0143      * @return array
0144      */
0145     public function addAuthors(array $authors)
0146     {
0147         foreach($authors as $author) {
0148             $this->addAuthor($author);
0149         }
0150     }
0151 
0152     /**
0153      * Set the feed character encoding
0154      *
0155      * @return string|null
0156      */
0157     public function setEncoding($encoding)
0158     {
0159         if (empty($encoding) || !is_string($encoding)) {
0160             // require_once 'Zend/Feed/Exception.php';
0161             throw new Zend_Feed_Exception('Invalid parameter: parameter must be a non-empty string');
0162         }
0163         $this->_data['encoding'] = $encoding;
0164     }
0165 
0166     /**
0167      * Get the feed character encoding
0168      *
0169      * @return string|null
0170      */
0171     public function getEncoding()
0172     {
0173         if (!array_key_exists('encoding', $this->_data)) {
0174             return 'UTF-8';
0175         }
0176         return $this->_data['encoding'];
0177     }
0178 
0179     /**
0180      * Set the copyright entry
0181      *
0182      * @return string|null
0183      */
0184     public function setCopyright($copyright)
0185     {
0186         if (empty($copyright) || !is_string($copyright)) {
0187             // require_once 'Zend/Feed/Exception.php';
0188             throw new Zend_Feed_Exception('Invalid parameter: parameter must be a non-empty string');
0189         }
0190         $this->_data['copyright'] = $copyright;
0191     }
0192 
0193     /**
0194      * Set the entry's content
0195      *
0196      * @return string|null
0197      */
0198     public function setContent($content)
0199     {
0200         if (empty($content) || !is_string($content)) {
0201             // require_once 'Zend/Feed/Exception.php';
0202             throw new Zend_Feed_Exception('Invalid parameter: parameter must be a non-empty string');
0203         }
0204         $this->_data['content'] = $content;
0205     }
0206 
0207     /**
0208      * Set the feed creation date
0209      *
0210      * @return string|null
0211      */
0212     public function setDateCreated($date = null)
0213     {
0214         $zdate = null;
0215         if ($date === null) {
0216             $zdate = new Zend_Date;
0217         } elseif ($date instanceof Zend_Date) {
0218             $zdate = $date;
0219         } elseif (ctype_digit((string)$date)) {
0220             $zdate = new Zend_Date($date, Zend_Date::TIMESTAMP);
0221         } else {
0222             // require_once 'Zend/Feed/Exception.php';
0223             throw new Zend_Feed_Exception('Invalid Zend_Date object or UNIX Timestamp passed as parameter');
0224         }
0225         $this->_data['dateCreated'] = $zdate;
0226     }
0227 
0228     /**
0229      * Set the feed modification date
0230      *
0231      * @return string|null
0232      */
0233     public function setDateModified($date = null)
0234     {
0235         $zdate = null;
0236         if ($date === null) {
0237             $zdate = new Zend_Date;
0238         } elseif ($date instanceof Zend_Date) {
0239             $zdate = $date;
0240         } elseif (ctype_digit((string)$date)) {
0241             $zdate = new Zend_Date($date, Zend_Date::TIMESTAMP);
0242         } else {
0243             // require_once 'Zend/Feed/Exception.php';
0244             throw new Zend_Feed_Exception('Invalid Zend_Date object or UNIX Timestamp passed as parameter');
0245         }
0246         $this->_data['dateModified'] = $zdate;
0247     }
0248 
0249     /**
0250      * Set the feed description
0251      *
0252      * @return string|null
0253      */
0254     public function setDescription($description)
0255     {
0256         if (empty($description) || !is_string($description)) {
0257             // require_once 'Zend/Feed/Exception.php';
0258             throw new Zend_Feed_Exception('Invalid parameter: parameter must be a non-empty string');
0259         }
0260         $this->_data['description'] = $description;
0261     }
0262 
0263     /**
0264      * Set the feed ID
0265      *
0266      * @return string|null
0267      */
0268     public function setId($id)
0269     {
0270         if (empty($id) || !is_string($id)) {
0271             // require_once 'Zend/Feed/Exception.php';
0272             throw new Zend_Feed_Exception('Invalid parameter: parameter must be a non-empty string');
0273         }
0274         $this->_data['id'] = $id;
0275     }
0276 
0277     /**
0278      * Set a link to the HTML source of this entry
0279      *
0280      * @return string|null
0281      */
0282     public function setLink($link)
0283     {
0284         if (empty($link) || !is_string($link) || !Zend_Uri::check($link)) {
0285             // require_once 'Zend/Feed/Exception.php';
0286             throw new Zend_Feed_Exception('Invalid parameter: parameter must be a non-empty string and valid URI/IRI');
0287         }
0288         $this->_data['link'] = $link;
0289     }
0290 
0291     /**
0292      * Set the number of comments associated with this entry
0293      *
0294      * @return string|null
0295      */
0296     public function setCommentCount($count)
0297     {
0298         if ( !is_numeric($count) || (int) $count < 0) {
0299             // require_once 'Zend/Feed/Exception.php';
0300             throw new Zend_Feed_Exception('Invalid parameter: "count" must be a non-empty integer number');
0301         }
0302         $this->_data['commentCount'] = (int) $count;
0303     }
0304 
0305     /**
0306      * Set a link to a HTML page containing comments associated with this entry
0307      *
0308      * @return string|null
0309      */
0310     public function setCommentLink($link)
0311     {
0312         if (empty($link) || !is_string($link) || !Zend_Uri::check($link)) {
0313             // require_once 'Zend/Feed/Exception.php';
0314             throw new Zend_Feed_Exception('Invalid parameter: "link" must be a non-empty string and valid URI/IRI');
0315         }
0316         $this->_data['commentLink'] = $link;
0317     }
0318 
0319     /**
0320      * Set a link to an XML feed for any comments associated with this entry
0321      *
0322      * @return string|null
0323      */
0324     public function setCommentFeedLink(array $link)
0325     {
0326         if (!isset($link['uri']) || !is_string($link['uri']) || !Zend_Uri::check($link['uri'])) {
0327             // require_once 'Zend/Feed/Exception.php';
0328             throw new Zend_Feed_Exception('Invalid parameter: "link" must be a non-empty string and valid URI/IRI');
0329         }
0330         if (!isset($link['type']) || !in_array($link['type'], array('atom', 'rss', 'rdf'))) {
0331             // require_once 'Zend/Feed/Exception.php';
0332             throw new Zend_Feed_Exception('Invalid parameter: "type" must be one'
0333             . ' of "atom", "rss" or "rdf"');
0334         }
0335         if (!isset($this->_data['commentFeedLinks'])) {
0336             $this->_data['commentFeedLinks'] = array();
0337         }
0338         $this->_data['commentFeedLinks'][] = $link;
0339     }
0340 
0341     /**
0342      * Set a links to an XML feed for any comments associated with this entry.
0343      * Each link is an array with keys "uri" and "type", where type is one of:
0344      * "atom", "rss" or "rdf".
0345      *
0346      * @return string|null
0347      */
0348     public function setCommentFeedLinks(array $links)
0349     {
0350         foreach ($links as $link) {
0351             $this->setCommentFeedLink($link);
0352         }
0353     }
0354 
0355     /**
0356      * Set the feed title
0357      *
0358      * @return string|null
0359      */
0360     public function setTitle($title)
0361     {
0362         if (empty($title) || !is_string($title)) {
0363             // require_once 'Zend/Feed/Exception.php';
0364             throw new Zend_Feed_Exception('Invalid parameter: parameter must be a non-empty string');
0365         }
0366         $this->_data['title'] = $title;
0367     }
0368 
0369     /**
0370      * Get an array with feed authors
0371      *
0372      * @return array
0373      */
0374     public function getAuthors()
0375     {
0376         if (!array_key_exists('authors', $this->_data)) {
0377             return null;
0378         }
0379         return $this->_data['authors'];
0380     }
0381 
0382     /**
0383      * Get the entry content
0384      *
0385      * @return string
0386      */
0387     public function getContent()
0388     {
0389         if (!array_key_exists('content', $this->_data)) {
0390             return null;
0391         }
0392         return $this->_data['content'];
0393     }
0394 
0395     /**
0396      * Get the entry copyright information
0397      *
0398      * @return string
0399      */
0400     public function getCopyright()
0401     {
0402         if (!array_key_exists('copyright', $this->_data)) {
0403             return null;
0404         }
0405         return $this->_data['copyright'];
0406     }
0407 
0408     /**
0409      * Get the entry creation date
0410      *
0411      * @return string
0412      */
0413     public function getDateCreated()
0414     {
0415         if (!array_key_exists('dateCreated', $this->_data)) {
0416             return null;
0417         }
0418         return $this->_data['dateCreated'];
0419     }
0420 
0421     /**
0422      * Get the entry modification date
0423      *
0424      * @return string
0425      */
0426     public function getDateModified()
0427     {
0428         if (!array_key_exists('dateModified', $this->_data)) {
0429             return null;
0430         }
0431         return $this->_data['dateModified'];
0432     }
0433 
0434     /**
0435      * Get the entry description
0436      *
0437      * @return string
0438      */
0439     public function getDescription()
0440     {
0441         if (!array_key_exists('description', $this->_data)) {
0442             return null;
0443         }
0444         return $this->_data['description'];
0445     }
0446 
0447     /**
0448      * Get the entry ID
0449      *
0450      * @return string
0451      */
0452     public function getId()
0453     {
0454         if (!array_key_exists('id', $this->_data)) {
0455             return null;
0456         }
0457         return $this->_data['id'];
0458     }
0459 
0460     /**
0461      * Get a link to the HTML source
0462      *
0463      * @return string|null
0464      */
0465     public function getLink()
0466     {
0467         if (!array_key_exists('link', $this->_data)) {
0468             return null;
0469         }
0470         return $this->_data['link'];
0471     }
0472 
0473 
0474     /**
0475      * Get all links
0476      *
0477      * @return array
0478      */
0479     public function getLinks()
0480     {
0481         if (!array_key_exists('links', $this->_data)) {
0482             return null;
0483         }
0484         return $this->_data['links'];
0485     }
0486 
0487     /**
0488      * Get the entry title
0489      *
0490      * @return string
0491      */
0492     public function getTitle()
0493     {
0494         if (!array_key_exists('title', $this->_data)) {
0495             return null;
0496         }
0497         return $this->_data['title'];
0498     }
0499 
0500     /**
0501      * Get the number of comments/replies for current entry
0502      *
0503      * @return integer
0504      */
0505     public function getCommentCount()
0506     {
0507         if (!array_key_exists('commentCount', $this->_data)) {
0508             return null;
0509         }
0510         return $this->_data['commentCount'];
0511     }
0512 
0513     /**
0514      * Returns a URI pointing to the HTML page where comments can be made on this entry
0515      *
0516      * @return string
0517      */
0518     public function getCommentLink()
0519     {
0520         if (!array_key_exists('commentLink', $this->_data)) {
0521             return null;
0522         }
0523         return $this->_data['commentLink'];
0524     }
0525 
0526     /**
0527      * Returns an array of URIs pointing to a feed of all comments for this entry
0528      * where the array keys indicate the feed type (atom, rss or rdf).
0529      *
0530      * @return string
0531      */
0532     public function getCommentFeedLinks()
0533     {
0534         if (!array_key_exists('commentFeedLinks', $this->_data)) {
0535             return null;
0536         }
0537         return $this->_data['commentFeedLinks'];
0538     }
0539 
0540     /**
0541      * Add a entry category
0542      *
0543      * @param string $category
0544      */
0545     public function addCategory(array $category)
0546     {
0547         if (!isset($category['term'])) {
0548             // require_once 'Zend/Feed/Exception.php';
0549             throw new Zend_Feed_Exception('Each category must be an array and '
0550             . 'contain at least a "term" element containing the machine '
0551             . ' readable category name');
0552         }
0553         if (isset($category['scheme'])) {
0554             if (empty($category['scheme'])
0555                 || !is_string($category['scheme'])
0556                 || !Zend_Uri::check($category['scheme'])
0557             ) {
0558                 // require_once 'Zend/Feed/Exception.php';
0559                 throw new Zend_Feed_Exception('The Atom scheme or RSS domain of'
0560                 . ' a category must be a valid URI');
0561             }
0562         }
0563         if (!isset($this->_data['categories'])) {
0564             $this->_data['categories'] = array();
0565         }
0566         $this->_data['categories'][] = $category;
0567     }
0568 
0569     /**
0570      * Set an array of entry categories
0571      *
0572      * @param array $categories
0573      */
0574     public function addCategories(array $categories)
0575     {
0576         foreach ($categories as $category) {
0577             $this->addCategory($category);
0578         }
0579     }
0580 
0581     /**
0582      * Get the entry categories
0583      *
0584      * @return string|null
0585      */
0586     public function getCategories()
0587     {
0588         if (!array_key_exists('categories', $this->_data)) {
0589             return null;
0590         }
0591         return $this->_data['categories'];
0592     }
0593 
0594     /**
0595      * Adds an enclosure to the entry. The array parameter may contain the
0596      * keys 'uri', 'type' and 'length'. Only 'uri' is required for Atom, though the
0597      * others must also be provided or RSS rendering (where they are required)
0598      * will throw an Exception.
0599      *
0600      * @param array $enclosures
0601      */
0602     public function setEnclosure(array $enclosure)
0603     {
0604         if (!isset($enclosure['uri'])) {
0605             // require_once 'Zend/Feed/Exception.php';
0606             throw new Zend_Feed_Exception('Enclosure "uri" is not set');
0607         }
0608         if (!Zend_Uri::check($enclosure['uri'])) {
0609             // require_once 'Zend/Feed/Exception.php';
0610             throw new Zend_Feed_Exception('Enclosure "uri" is not a valid URI/IRI');
0611         }
0612         $this->_data['enclosure'] = $enclosure;
0613     }
0614 
0615     /**
0616      * Retrieve an array of all enclosures to be added to entry.
0617      *
0618      * @return array
0619      */
0620     public function getEnclosure()
0621     {
0622         if (!array_key_exists('enclosure', $this->_data)) {
0623             return null;
0624         }
0625         return $this->_data['enclosure'];
0626     }
0627 
0628     /**
0629      * Unset a specific data point
0630      *
0631      * @param string $name
0632      */
0633     public function remove($name)
0634     {
0635         if (isset($this->_data[$name])) {
0636             unset($this->_data[$name]);
0637         }
0638     }
0639 
0640     /**
0641      * Get registered extensions
0642      *
0643      * @return array
0644      */
0645     public function getExtensions()
0646     {
0647         return $this->_extensions;
0648     }
0649 
0650     /**
0651      * Return an Extension object with the matching name (postfixed with _Entry)
0652      *
0653      * @param string $name
0654      * @return object
0655      */
0656     public function getExtension($name)
0657     {
0658         if (array_key_exists($name . '_Entry', $this->_extensions)) {
0659             return $this->_extensions[$name . '_Entry'];
0660         }
0661         return null;
0662     }
0663 
0664     /**
0665      * Set the current feed type being exported to "rss" or "atom". This allows
0666      * other objects to gracefully choose whether to execute or not, depending
0667      * on their appropriateness for the current type, e.g. renderers.
0668      *
0669      * @param string $type
0670      */
0671     public function setType($type)
0672     {
0673         $this->_type = $type;
0674     }
0675 
0676     /**
0677      * Retrieve the current or last feed type exported.
0678      *
0679      * @return string Value will be "rss" or "atom"
0680      */
0681     public function getType()
0682     {
0683         return $this->_type;
0684     }
0685 
0686     /**
0687      * Method overloading: call given method on first extension implementing it
0688      *
0689      * @param  string $method
0690      * @param  array $args
0691      * @return mixed
0692      * @throws Zend_Feed_Exception if no extensions implements the method
0693      */
0694     public function __call($method, $args)
0695     {
0696         foreach ($this->_extensions as $extension) {
0697             try {
0698                 return call_user_func_array(array($extension, $method), $args);
0699             } catch (Zend_Feed_Writer_Exception_InvalidMethodException $e) {
0700             }
0701         }
0702         // require_once 'Zend/Feed/Exception.php';
0703         throw new Zend_Feed_Exception('Method: ' . $method
0704             . ' does not exist and could not be located on a registered Extension');
0705     }
0706 
0707     /**
0708      * Creates a new Zend_Feed_Writer_Source data container for use. This is NOT
0709      * added to the current feed automatically, but is necessary to create a
0710      * container with some initial values preset based on the current feed data.
0711      *
0712      * @return Zend_Feed_Writer_Source
0713      */
0714     public function createSource()
0715     {
0716         $source = new Zend_Feed_Writer_Source;
0717         if ($this->getEncoding()) {
0718             $source->setEncoding($this->getEncoding());
0719         }
0720         $source->setType($this->getType());
0721         return $source;
0722     }
0723 
0724     /**
0725      * Appends a Zend_Feed_Writer_Entry object representing a new entry/item
0726      * the feed data container's internal group of entries.
0727      *
0728      * @param Zend_Feed_Writer_Source $source
0729      */
0730     public function setSource(Zend_Feed_Writer_Source $source)
0731     {
0732         $this->_data['source'] = $source;
0733     }
0734 
0735     /**
0736      * @return Zend_Feed_Writer_Source
0737      */
0738     public function getSource()
0739     {
0740         if (isset($this->_data['source'])) {
0741             return $this->_data['source'];
0742         }
0743         return null;
0744     }
0745 
0746     /**
0747      * Load extensions from Zend_Feed_Writer
0748      *
0749      * @return void
0750      */
0751     protected function _loadExtensions()
0752     {
0753         $all = Zend_Feed_Writer::getExtensions();
0754         $exts = $all['entry'];
0755         foreach ($exts as $ext) {
0756             $className = Zend_Feed_Writer::getPluginLoader()->getClassName($ext);
0757             $this->_extensions[$ext] = new $className();
0758             $this->_extensions[$ext]->setEncoding($this->getEncoding());
0759         }
0760     }
0761 }