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 /**
0033  * @see Zend_Feed_Writer
0034  */
0035 // require_once 'Zend/Feed/Writer.php';
0036 
0037 /**
0038  * @see Zend_Feed_Writer_Entry
0039  */
0040 // require_once 'Zend/Feed/Writer/Entry.php';
0041 
0042 /**
0043  * @see Zend_Feed_Writer_Deleted
0044  */
0045 // require_once 'Zend/Feed/Writer/Deleted.php';
0046 
0047 /**
0048  * @see Zend_Feed_Writer_Renderer_Feed_Atom
0049  */
0050 // require_once 'Zend/Feed/Writer/Renderer/Feed/Atom.php';
0051 
0052 /**
0053  * @see Zend_Feed_Writer_Renderer_Feed_Rss
0054  */
0055 // require_once 'Zend/Feed/Writer/Renderer/Feed/Rss.php';
0056 
0057 // require_once 'Zend/Feed/Writer/Feed/FeedAbstract.php';
0058 
0059 /**
0060  * @category   Zend
0061  * @package    Zend_Feed_Writer
0062  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0063  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0064  */
0065 class Zend_Feed_Writer_Feed extends Zend_Feed_Writer_Feed_FeedAbstract
0066 implements Iterator, Countable
0067 {
0068 
0069     /**
0070      * Contains all entry objects
0071      *
0072      * @var array
0073      */
0074     protected $_entries = array();
0075 
0076     /**
0077      * A pointer for the iterator to keep track of the entries array
0078      *
0079      * @var int
0080      */
0081     protected $_entriesKey = 0;
0082 
0083     /**
0084      * Creates a new Zend_Feed_Writer_Entry data container for use. This is NOT
0085      * added to the current feed automatically, but is necessary to create a
0086      * container with some initial values preset based on the current feed data.
0087      *
0088      * @return Zend_Feed_Writer_Entry
0089      */
0090     public function createEntry()
0091     {
0092         $entry = new Zend_Feed_Writer_Entry;
0093         if ($this->getEncoding()) {
0094             $entry->setEncoding($this->getEncoding());
0095         }
0096         $entry->setType($this->getType());
0097         return $entry;
0098     }
0099 
0100     /**
0101      * Appends a Zend_Feed_Writer_Deleted object representing a new entry tombstone
0102      * to the feed data container's internal group of entries.
0103      *
0104      * @param Zend_Feed_Writer_Deleted $entry
0105      */
0106     public function addTombstone(Zend_Feed_Writer_Deleted $deleted)
0107     {
0108         $this->_entries[] = $deleted;
0109     }
0110 
0111     /**
0112      * Creates a new Zend_Feed_Writer_Deleted data container for use. This is NOT
0113      * added to the current feed automatically, but is necessary to create a
0114      * container with some initial values preset based on the current feed data.
0115      *
0116      * @return Zend_Feed_Writer_Deleted
0117      */
0118     public function createTombstone()
0119     {
0120         $deleted = new Zend_Feed_Writer_Deleted;
0121         if ($this->getEncoding()) {
0122             $deleted->setEncoding($this->getEncoding());
0123         }
0124         $deleted->setType($this->getType());
0125         return $deleted;
0126     }
0127 
0128     /**
0129      * Appends a Zend_Feed_Writer_Entry object representing a new entry/item
0130      * the feed data container's internal group of entries.
0131      *
0132      * @param Zend_Feed_Writer_Entry $entry
0133      */
0134     public function addEntry(Zend_Feed_Writer_Entry $entry)
0135     {
0136         $this->_entries[] = $entry;
0137     }
0138 
0139     /**
0140      * Removes a specific indexed entry from the internal queue. Entries must be
0141      * added to a feed container in order to be indexed.
0142      *
0143      * @param int $index
0144      */
0145     public function removeEntry($index)
0146     {
0147         if (isset($this->_entries[$index])) {
0148             unset($this->_entries[$index]);
0149         }
0150         // require_once 'Zend/Feed/Exception.php';
0151         throw new Zend_Feed_Exception('Undefined index: ' . $index . '. Entry does not exist.');
0152     }
0153 
0154     /**
0155      * Retrieve a specific indexed entry from the internal queue. Entries must be
0156      * added to a feed container in order to be indexed.
0157      *
0158      * @param int $index
0159      */
0160     public function getEntry($index = 0)
0161     {
0162         if (isset($this->_entries[$index])) {
0163             return $this->_entries[$index];
0164         }
0165         // require_once 'Zend/Feed/Exception.php';
0166         throw new Zend_Feed_Exception('Undefined index: ' . $index . '. Entry does not exist.');
0167     }
0168 
0169     /**
0170      * Orders all indexed entries by date, thus offering date ordered readable
0171      * content where a parser (or Homo Sapien) ignores the generic rule that
0172      * XML element order is irrelevant and has no intrinsic meaning.
0173      *
0174      * Using this method will alter the original indexation.
0175      *
0176      * @return void
0177      */
0178     public function orderByDate()
0179     {
0180         /**
0181          * Could do with some improvement for performance perhaps
0182          */
0183         $timestamp = time();
0184         $entries = array();
0185         foreach ($this->_entries as $entry) {
0186             if ($entry->getDateModified()) {
0187                 $timestamp = (int) $entry->getDateModified()->get(Zend_Date::TIMESTAMP);
0188             } elseif ($entry->getDateCreated()) {
0189                 $timestamp = (int) $entry->getDateCreated()->get(Zend_Date::TIMESTAMP);
0190             }
0191             $entries[$timestamp] = $entry;
0192         }
0193         krsort($entries, SORT_NUMERIC);
0194         $this->_entries = array_values($entries);
0195     }
0196 
0197     /**
0198      * Get the number of feed entries.
0199      * Required by the Iterator interface.
0200      *
0201      * @return int
0202      */
0203     public function count()
0204     {
0205         return count($this->_entries);
0206     }
0207 
0208     /**
0209      * Return the current entry
0210      *
0211      * @return Zend_Feed_Reader_Entry_Interface
0212      */
0213     public function current()
0214     {
0215         return $this->_entries[$this->key()];
0216     }
0217 
0218     /**
0219      * Return the current feed key
0220      *
0221      * @return unknown
0222      */
0223     public function key()
0224     {
0225         return $this->_entriesKey;
0226     }
0227 
0228     /**
0229      * Move the feed pointer forward
0230      *
0231      * @return void
0232      */
0233     public function next()
0234     {
0235         ++$this->_entriesKey;
0236     }
0237 
0238     /**
0239      * Reset the pointer in the feed object
0240      *
0241      * @return void
0242      */
0243     public function rewind()
0244     {
0245         $this->_entriesKey = 0;
0246     }
0247 
0248     /**
0249      * Check to see if the iterator is still valid
0250      *
0251      * @return boolean
0252      */
0253     public function valid()
0254     {
0255         return 0 <= $this->_entriesKey && $this->_entriesKey < $this->count();
0256     }
0257 
0258     /**
0259      * Attempt to build and return the feed resulting from the data set
0260      *
0261      * @param string $type             The feed type "rss" or "atom" to export as
0262      * @param bool   $ignoreExceptions
0263      * @return string
0264      */
0265     public function export($type, $ignoreExceptions = false)
0266     {
0267         $this->setType(strtolower($type));
0268         $type = ucfirst($this->getType());
0269         if ($type !== 'Rss' && $type !== 'Atom') {
0270             // require_once 'Zend/Feed/Exception.php';
0271             throw new Zend_Feed_Exception('Invalid feed type specified: ' . $type . '.'
0272             . ' Should be one of "rss" or "atom".');
0273         }
0274         $renderClass = 'Zend_Feed_Writer_Renderer_Feed_' . $type;
0275         $renderer = new $renderClass($this);
0276         if ($ignoreExceptions) {
0277             $renderer->ignoreExceptions();
0278         }
0279         return $renderer->render()->saveXml();
0280     }
0281 
0282 }