File indexing completed on 2024-05-26 06:02:57

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_Reader
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_Reader
0024  */
0025 // require_once 'Zend/Feed/Reader.php';
0026 
0027 /**
0028  * @see Zend_feed_Reader_FeedInterface
0029  */
0030 // require_once 'Zend/Feed/Reader/FeedInterface.php';
0031 
0032 /**
0033  * @category   Zend
0034  * @package    Zend_Feed_Reader
0035  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0036  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0037  */
0038 abstract class Zend_Feed_Reader_FeedAbstract implements Zend_Feed_Reader_FeedInterface
0039 {
0040     /**
0041      * Parsed feed data
0042      *
0043      * @var array
0044      */
0045     protected $_data = array();
0046 
0047     /**
0048      * Parsed feed data in the shape of a DOMDocument
0049      *
0050      * @var DOMDocument
0051      */
0052     protected $_domDocument = null;
0053 
0054     /**
0055      * An array of parsed feed entries
0056      *
0057      * @var array
0058      */
0059     protected $_entries = array();
0060 
0061     /**
0062      * A pointer for the iterator to keep track of the entries array
0063      *
0064      * @var int
0065      */
0066     protected $_entriesKey = 0;
0067 
0068     /**
0069      * The base XPath query used to retrieve feed data
0070      *
0071      * @var DOMXPath
0072      */
0073     protected $_xpath = null;
0074 
0075     /**
0076      * Array of loaded extensions
0077      *
0078      * @var array
0079      */
0080     protected $_extensions = array();
0081 
0082     /**
0083      * Original Source URI (set if imported from a URI)
0084      *
0085      * @var string
0086      */
0087     protected $_originalSourceUri = null;
0088 
0089     /**
0090      * Constructor
0091      *
0092      * @param DomDocument The DOM object for the feed's XML
0093      * @param string $type Feed type
0094      */
0095     public function __construct(DomDocument $domDocument, $type = null)
0096     {
0097         $this->_domDocument = $domDocument;
0098         $this->_xpath = new DOMXPath($this->_domDocument);
0099 
0100         if ($type !== null) {
0101             $this->_data['type'] = $type;
0102         } else {
0103             $this->_data['type'] = Zend_Feed_Reader::detectType($this->_domDocument);
0104         }
0105         $this->_registerNamespaces();
0106         $this->_indexEntries();
0107         $this->_loadExtensions();
0108     }
0109 
0110     /**
0111      * Set an original source URI for the feed being parsed. This value
0112      * is returned from getFeedLink() method if the feed does not carry
0113      * a self-referencing URI.
0114      *
0115      * @param string $uri
0116      */
0117     public function setOriginalSourceUri($uri)
0118     {
0119         $this->_originalSourceUri = $uri;
0120     }
0121 
0122     /**
0123      * Get an original source URI for the feed being parsed. Returns null if
0124      * unset or the feed was not imported from a URI.
0125      *
0126      * @return string|null
0127      */
0128     public function getOriginalSourceUri()
0129     {
0130         return $this->_originalSourceUri;
0131     }
0132 
0133     /**
0134      * Get the number of feed entries.
0135      * Required by the Iterator interface.
0136      *
0137      * @return int
0138      */
0139     public function count()
0140     {
0141         return count($this->_entries);
0142     }
0143 
0144     /**
0145      * Return the current entry
0146      *
0147      * @return Zend_Feed_Reader_EntryInterface
0148      */
0149     public function current()
0150     {
0151         if (substr($this->getType(), 0, 3) == 'rss') {
0152             $reader = new Zend_Feed_Reader_Entry_Rss($this->_entries[$this->key()], $this->key(), $this->getType());
0153         } else {
0154             $reader = new Zend_Feed_Reader_Entry_Atom($this->_entries[$this->key()], $this->key(), $this->getType());
0155         }
0156 
0157         $reader->setXpath($this->_xpath);
0158 
0159         return $reader;
0160     }
0161 
0162     /**
0163      * Get the DOM
0164      *
0165      * @return DOMDocument
0166      */
0167     public function getDomDocument()
0168     {
0169         return $this->_domDocument;
0170     }
0171 
0172     /**
0173      * Get the Feed's encoding
0174      *
0175      * @return string
0176      */
0177     public function getEncoding()
0178     {
0179         $assumed = $this->getDomDocument()->encoding;
0180         if (empty($assumed)) {
0181             $assumed = 'UTF-8';
0182         }
0183         return $assumed;
0184     }
0185 
0186     /**
0187      * Get feed as xml
0188      *
0189      * @return string
0190      */
0191     public function saveXml()
0192     {
0193           return $this->getDomDocument()->saveXml();
0194     }
0195 
0196     /**
0197      * Get the DOMElement representing the items/feed element
0198      *
0199      * @return DOMElement
0200      */
0201     public function getElement()
0202     {
0203           return $this->getDomDocument()->documentElement;
0204     }
0205 
0206     /**
0207      * Get the DOMXPath object for this feed
0208      *
0209      * @return DOMXPath
0210      */
0211     public function getXpath()
0212     {
0213           return $this->_xpath;
0214     }
0215 
0216     /**
0217      * Get the feed type
0218      *
0219      * @return string
0220      */
0221     public function getType()
0222     {
0223         return $this->_data['type'];
0224     }
0225 
0226     /**
0227      * Return the current feed key
0228      *
0229      * @return unknown
0230      */
0231     public function key()
0232     {
0233         return $this->_entriesKey;
0234     }
0235 
0236     /**
0237      * Move the feed pointer forward
0238      *
0239      */
0240     public function next()
0241     {
0242         ++$this->_entriesKey;
0243     }
0244 
0245     /**
0246      * Reset the pointer in the feed object
0247      *
0248      */
0249     public function rewind()
0250     {
0251         $this->_entriesKey = 0;
0252     }
0253 
0254     /**
0255      * Check to see if the iterator is still valid
0256      *
0257      * @return boolean
0258      */
0259     public function valid()
0260     {
0261         return 0 <= $this->_entriesKey && $this->_entriesKey < $this->count();
0262     }
0263 
0264     public function getExtensions()
0265     {
0266         return $this->_extensions;
0267     }
0268 
0269     public function __call($method, $args)
0270     {
0271         foreach ($this->_extensions as $extension) {
0272             if (method_exists($extension, $method)) {
0273                 return call_user_func_array(array($extension, $method), $args);
0274             }
0275         }
0276         // require_once 'Zend/Feed/Exception.php';
0277         throw new Zend_Feed_Exception('Method: ' . $method
0278         . 'does not exist and could not be located on a registered Extension');
0279     }
0280 
0281     /**
0282      * Return an Extension object with the matching name (postfixed with _Feed)
0283      *
0284      * @param string $name
0285      * @return Zend_Feed_Reader_Extension_FeedAbstract
0286      */
0287     public function getExtension($name)
0288     {
0289         if (array_key_exists($name . '_Feed', $this->_extensions)) {
0290             return $this->_extensions[$name . '_Feed'];
0291         }
0292         return null;
0293     }
0294 
0295     protected function _loadExtensions()
0296     {
0297         $all = Zend_Feed_Reader::getExtensions();
0298         $feed = $all['feed'];
0299         foreach ($feed as $extension) {
0300             if (in_array($extension, $all['core'])) {
0301                 continue;
0302             }
0303             $className = Zend_Feed_Reader::getPluginLoader()->getClassName($extension);
0304             $this->_extensions[$extension] = new $className(
0305                 $this->getDomDocument(), $this->_data['type'], $this->_xpath
0306             );
0307         }
0308     }
0309 
0310     /**
0311      * Read all entries to the internal entries array
0312      *
0313      */
0314     abstract protected function _indexEntries();
0315 
0316     /**
0317      * Register the default namespaces for the current feed format
0318      *
0319      */
0320     abstract protected function _registerNamespaces();
0321 }