3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
10 namespace Zend\Feed\Reader
;
16 abstract class AbstractFeed
implements Feed\FeedInterface
23 protected $data = array();
26 * Parsed feed data in the shape of a DOMDocument
30 protected $domDocument = null;
33 * An array of parsed feed entries
37 protected $entries = array();
40 * A pointer for the iterator to keep track of the entries array
44 protected $entriesKey = 0;
47 * The base XPath query used to retrieve feed data
51 protected $xpath = null;
54 * Array of loaded extensions
58 protected $extensions = array();
61 * Original Source URI (set if imported from a URI)
65 protected $originalSourceUri = null;
70 * @param DomDocument $domDocument The DOM object for the feed's XML
71 * @param string $type Feed type
73 public function __construct(DOMDocument
$domDocument, $type = null)
75 $this->domDocument
= $domDocument;
76 $this->xpath
= new DOMXPath($this->domDocument
);
79 $this->data
['type'] = $type;
81 $this->data
['type'] = Reader
::detectType($this->domDocument
);
83 $this->registerNamespaces();
84 $this->indexEntries();
85 $this->loadExtensions();
89 * Set an original source URI for the feed being parsed. This value
90 * is returned from getFeedLink() method if the feed does not carry
91 * a self-referencing URI.
95 public function setOriginalSourceUri($uri)
97 $this->originalSourceUri
= $uri;
101 * Get an original source URI for the feed being parsed. Returns null if
102 * unset or the feed was not imported from a URI.
104 * @return string|null
106 public function getOriginalSourceUri()
108 return $this->originalSourceUri
;
112 * Get the number of feed entries.
113 * Required by the Iterator interface.
117 public function count()
119 return count($this->entries
);
123 * Return the current entry
125 * @return \Zend\Feed\Reader\AbstractEntry
127 public function current()
129 if (substr($this->getType(), 0, 3) == 'rss') {
130 $reader = new Entry\
RSS($this->entries
[$this->key()], $this->key(), $this->getType());
132 $reader = new Entry\
Atom($this->entries
[$this->key()], $this->key(), $this->getType());
135 $reader->setXpath($this->xpath
);
143 * @return DOMDocument
145 public function getDomDocument()
147 return $this->domDocument
;
151 * Get the Feed's encoding
155 public function getEncoding()
157 $assumed = $this->getDomDocument()->encoding
;
158 if (empty($assumed)) {
169 public function saveXml()
171 return $this->getDomDocument()->saveXml();
175 * Get the DOMElement representing the items/feed element
179 public function getElement()
181 return $this->getDomDocument()->documentElement
;
185 * Get the DOMXPath object for this feed
189 public function getXpath()
199 public function getType()
201 return $this->data
['type'];
205 * Return the current feed key
209 public function key()
211 return $this->entriesKey
;
215 * Move the feed pointer forward
218 public function next()
224 * Reset the pointer in the feed object
227 public function rewind()
229 $this->entriesKey
= 0;
233 * Check to see if the iterator is still valid
237 public function valid()
239 return 0 <= $this->entriesKey
&& $this->entriesKey
< $this->count();
242 public function getExtensions()
244 return $this->extensions
;
247 public function __call($method, $args)
249 foreach ($this->extensions
as $extension) {
250 if (method_exists($extension, $method)) {
251 return call_user_func_array(array($extension, $method), $args);
254 throw new Exception\
BadMethodCallException('Method: ' . $method
255 . 'does not exist and could not be located on a registered Extension');
259 * Return an Extension object with the matching name (postfixed with _Feed)
261 * @param string $name
262 * @return \Zend\Feed\Reader\Extension\AbstractFeed
264 public function getExtension($name)
266 if (array_key_exists($name . '\Feed', $this->extensions
)) {
267 return $this->extensions
[$name . '\Feed'];
272 protected function loadExtensions()
274 $all = Reader
::getExtensions();
275 $manager = Reader
::getExtensionManager();
276 $feed = $all['feed'];
277 foreach ($feed as $extension) {
278 if (in_array($extension, $all['core'])) {
281 $plugin = $manager->get($extension);
282 $plugin->setDomDocument($this->getDomDocument());
283 $plugin->setType($this->data
['type']);
284 $plugin->setXpath($this->xpath
);
285 $this->extensions
[$extension] = $plugin;
290 * Read all entries to the internal entries array
293 abstract protected function indexEntries();
296 * Register the default namespaces for the current feed format
299 abstract protected function registerNamespaces();