| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321 |
- <?php
- /**
- * Zend Framework
- *
- * LICENSE
- *
- * This source file is subject to the new BSD license that is bundled
- * with this package in the file LICENSE.txt.
- * It is also available through the world-wide-web at this URL:
- * http://framework.zend.com/license/new-bsd
- * If you did not receive a copy of the license and are unable to
- * obtain it through the world-wide-web, please send an email
- * to license@zend.com so we can send you a copy immediately.
- *
- * @category Zend
- * @package Zend_Feed_Reader
- * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
- * @license http://framework.zend.com/license/new-bsd New BSD License
- * @version $Id$
- */
- /**
- * @see Zend_Feed_Reader
- */
- require_once 'Zend/Feed/Reader.php';
- /**
- * @see Zend_feed_Reader_FeedInterface
- */
- require_once 'Zend/Feed/Reader/FeedInterface.php';
- /**
- * @category Zend
- * @package Zend_Feed_Reader
- * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
- * @license http://framework.zend.com/license/new-bsd New BSD License
- */
- abstract class Zend_Feed_Reader_FeedAbstract implements Zend_Feed_Reader_FeedInterface
- {
- /**
- * Parsed feed data
- *
- * @var array
- */
- protected $_data = array();
- /**
- * Parsed feed data in the shape of a DOMDocument
- *
- * @var DOMDocument
- */
- protected $_domDocument = null;
- /**
- * An array of parsed feed entries
- *
- * @var array
- */
- protected $_entries = array();
- /**
- * A pointer for the iterator to keep track of the entries array
- *
- * @var int
- */
- protected $_entriesKey = 0;
- /**
- * The base XPath query used to retrieve feed data
- *
- * @var DOMXPath
- */
- protected $_xpath = null;
- /**
- * Array of loaded extensions
- *
- * @var array
- */
- protected $_extensions = array();
- /**
- * Original Source URI (set if imported from a URI)
- *
- * @var string
- */
- protected $_originalSourceUri = null;
- /**
- * Constructor
- *
- * @param DomDocument The DOM object for the feed's XML
- * @param string $type Feed type
- */
- public function __construct(DomDocument $domDocument, $type = null)
- {
- $this->_domDocument = $domDocument;
- $this->_xpath = new DOMXPath($this->_domDocument);
- if ($type !== null) {
- $this->_data['type'] = $type;
- } else {
- $this->_data['type'] = Zend_Feed_Reader::detectType($this->_domDocument);
- }
- $this->_registerNamespaces();
- $this->_indexEntries();
- $this->_loadExtensions();
- }
- /**
- * Set an original source URI for the feed being parsed. This value
- * is returned from getFeedLink() method if the feed does not carry
- * a self-referencing URI.
- *
- * @param string $uri
- */
- public function setOriginalSourceUri($uri)
- {
- $this->_originalSourceUri = $uri;
- }
- /**
- * Get an original source URI for the feed being parsed. Returns null if
- * unset or the feed was not imported from a URI.
- *
- * @return string|null
- */
- public function getOriginalSourceUri()
- {
- return $this->_originalSourceUri;
- }
- /**
- * Get the number of feed entries.
- * Required by the Iterator interface.
- *
- * @return int
- */
- public function count()
- {
- return count($this->_entries);
- }
- /**
- * Return the current entry
- *
- * @return Zend_Feed_Reader_EntryInterface
- */
- public function current()
- {
- if (substr($this->getType(), 0, 3) == 'rss') {
- $reader = new Zend_Feed_Reader_Entry_Rss($this->_entries[$this->key()], $this->key(), $this->getType());
- } else {
- $reader = new Zend_Feed_Reader_Entry_Atom($this->_entries[$this->key()], $this->key(), $this->getType());
- }
- $reader->setXpath($this->_xpath);
- return $reader;
- }
- /**
- * Get the DOM
- *
- * @return DOMDocument
- */
- public function getDomDocument()
- {
- return $this->_domDocument;
- }
- /**
- * Get the Feed's encoding
- *
- * @return string
- */
- public function getEncoding()
- {
- $assumed = $this->getDomDocument()->encoding;
- if (empty($assumed)) {
- $assumed = 'UTF-8';
- }
- return $assumed;
- }
- /**
- * Get feed as xml
- *
- * @return string
- */
- public function saveXml()
- {
- return $this->getDomDocument()->saveXml();
- }
- /**
- * Get the DOMElement representing the items/feed element
- *
- * @return DOMElement
- */
- public function getElement()
- {
- return $this->getDomDocument()->documentElement;
- }
- /**
- * Get the DOMXPath object for this feed
- *
- * @return DOMXPath
- */
- public function getXpath()
- {
- return $this->_xpath;
- }
- /**
- * Get the feed type
- *
- * @return string
- */
- public function getType()
- {
- return $this->_data['type'];
- }
- /**
- * Return the current feed key
- *
- * @return unknown
- */
- public function key()
- {
- return $this->_entriesKey;
- }
- /**
- * Move the feed pointer forward
- *
- */
- public function next()
- {
- ++$this->_entriesKey;
- }
- /**
- * Reset the pointer in the feed object
- *
- */
- public function rewind()
- {
- $this->_entriesKey = 0;
- }
- /**
- * Check to see if the iterator is still valid
- *
- * @return boolean
- */
- public function valid()
- {
- return 0 <= $this->_entriesKey && $this->_entriesKey < $this->count();
- }
- public function getExtensions()
- {
- return $this->_extensions;
- }
- public function __call($method, $args)
- {
- foreach ($this->_extensions as $extension) {
- if (method_exists($extension, $method)) {
- return call_user_func_array(array($extension, $method), $args);
- }
- }
- require_once 'Zend/Feed/Exception.php';
- throw new Zend_Feed_Exception('Method: ' . $method
- . 'does not exist and could not be located on a registered Extension');
- }
- /**
- * Return an Extension object with the matching name (postfixed with _Feed)
- *
- * @param string $name
- * @return Zend_Feed_Reader_Extension_FeedAbstract
- */
- public function getExtension($name)
- {
- if (array_key_exists($name . '_Feed', $this->_extensions)) {
- return $this->_extensions[$name . '_Feed'];
- }
- return null;
- }
- protected function _loadExtensions()
- {
- $all = Zend_Feed_Reader::getExtensions();
- $feed = $all['feed'];
- foreach ($feed as $extension) {
- if (in_array($extension, $all['core'])) {
- continue;
- }
- $className = Zend_Feed_Reader::getPluginLoader()->getClassName($extension);
- $this->_extensions[$extension] = new $className(
- $this->getDomDocument(), $this->_data['type'], $this->_xpath
- );
- }
- }
- /**
- * Read all entries to the internal entries array
- *
- */
- abstract protected function _indexEntries();
- /**
- * Register the default namespaces for the current feed format
- *
- */
- abstract protected function _registerNamespaces();
- }
|