3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2015 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\Feed;
16 use Zend\Feed\Reader\Exception;
20 abstract class AbstractFeed implements FeedInterface
30 * Parsed feed data in the shape of a DOMDocument
34 protected $domDocument = null;
37 * An array of parsed feed entries
41 protected $entries = [];
44 * A pointer for the iterator to keep track of the entries array
48 protected $entriesKey = 0;
51 * The base XPath query used to retrieve feed data
55 protected $xpath = null;
58 * Array of loaded extensions
62 protected $extensions = [];
65 * Original Source URI (set if imported from a URI)
69 protected $originalSourceUri = null;
74 * @param DOMDocument $domDocument The DOM object for the feed's XML
75 * @param string $type Feed type
77 public function __construct(DOMDocument $domDocument, $type = null)
79 $this->domDocument = $domDocument;
80 $this->xpath = new DOMXPath($this->domDocument);
83 $this->data['type'] = $type;
85 $this->data['type'] = Reader\Reader::detectType($this->domDocument);
87 $this->registerNamespaces();
88 $this->indexEntries();
89 $this->loadExtensions();
93 * Set an original source URI for the feed being parsed. This value
94 * is returned from getFeedLink() method if the feed does not carry
95 * a self-referencing URI.
99 public function setOriginalSourceUri($uri)
101 $this->originalSourceUri = $uri;
105 * Get an original source URI for the feed being parsed. Returns null if
106 * unset or the feed was not imported from a URI.
108 * @return string|null
110 public function getOriginalSourceUri()
112 return $this->originalSourceUri;
116 * Get the number of feed entries.
117 * Required by the Iterator interface.
121 public function count()
123 return count($this->entries);
127 * Return the current entry
129 * @return \Zend\Feed\Reader\Entry\EntryInterface
131 public function current()
133 if (substr($this->getType(), 0, 3) == 'rss') {
134 $reader = new Reader\Entry\Rss($this->entries[$this->key()], $this->key(), $this->getType());
136 $reader = new Reader\Entry\Atom($this->entries[$this->key()], $this->key(), $this->getType());
139 $reader->setXpath($this->xpath);
147 * @return DOMDocument
149 public function getDomDocument()
151 return $this->domDocument;
155 * Get the Feed's encoding
159 public function getEncoding()
161 $assumed = $this->getDomDocument()->encoding;
162 if (empty($assumed)) {
173 public function saveXml()
175 return $this->getDomDocument()->saveXML();
179 * Get the DOMElement representing the items/feed element
183 public function getElement()
185 return $this->getDomDocument()->documentElement;
189 * Get the DOMXPath object for this feed
193 public function getXpath()
203 public function getType()
205 return $this->data['type'];
209 * Return the current feed key
213 public function key()
215 return $this->entriesKey;
219 * Move the feed pointer forward
222 public function next()
228 * Reset the pointer in the feed object
231 public function rewind()
233 $this->entriesKey = 0;
237 * Check to see if the iterator is still valid
241 public function valid()
243 return 0 <= $this->entriesKey && $this->entriesKey < $this->count();
246 public function getExtensions()
248 return $this->extensions;
251 public function __call($method, $args)
253 foreach ($this->extensions as $extension) {
254 if (method_exists($extension, $method)) {
255 return call_user_func_array([$extension, $method], $args);
258 throw new Exception\BadMethodCallException('Method: ' . $method
259 . 'does not exist and could not be located on a registered Extension');
263 * Return an Extension object with the matching name (postfixed with _Feed)
265 * @param string $name
266 * @return \Zend\Feed\Reader\Extension\AbstractFeed
268 public function getExtension($name)
270 if (array_key_exists($name . '\\Feed', $this->extensions)) {
271 return $this->extensions[$name . '\\Feed'];
276 protected function loadExtensions()
278 $all = Reader\Reader::getExtensions();
279 $manager = Reader\Reader::getExtensionManager();
280 $feed = $all['feed'];
281 foreach ($feed as $extension) {
282 if (in_array($extension, $all['core'])) {
285 if (! $manager->has($extension)) {
286 throw new Exception\RuntimeException(
287 sprintf('Unable to load extension "%s"; cannot find class', $extension)
290 $plugin = $manager->get($extension);
291 $plugin->setDomDocument($this->getDomDocument());
292 $plugin->setType($this->data['type']);
293 $plugin->setXpath($this->xpath);
294 $this->extensions[$extension] = $plugin;
299 * Read all entries to the internal entries array
302 abstract protected function indexEntries();
305 * Register the default namespaces for the current feed format
308 abstract protected function registerNamespaces();