Chris@0: domDocument = $domDocument; Chris@0: $this->xpath = new DOMXPath($this->domDocument); Chris@0: Chris@0: if ($type !== null) { Chris@0: $this->data['type'] = $type; Chris@0: } else { Chris@0: $this->data['type'] = Reader\Reader::detectType($this->domDocument); Chris@0: } Chris@0: $this->registerNamespaces(); Chris@0: $this->indexEntries(); Chris@0: $this->loadExtensions(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Set an original source URI for the feed being parsed. This value Chris@0: * is returned from getFeedLink() method if the feed does not carry Chris@0: * a self-referencing URI. Chris@0: * Chris@0: * @param string $uri Chris@0: */ Chris@0: public function setOriginalSourceUri($uri) Chris@0: { Chris@0: $this->originalSourceUri = $uri; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Get an original source URI for the feed being parsed. Returns null if Chris@0: * unset or the feed was not imported from a URI. Chris@0: * Chris@0: * @return string|null Chris@0: */ Chris@0: public function getOriginalSourceUri() Chris@0: { Chris@0: return $this->originalSourceUri; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Get the number of feed entries. Chris@0: * Required by the Iterator interface. Chris@0: * Chris@0: * @return int Chris@0: */ Chris@0: public function count() Chris@0: { Chris@0: return count($this->entries); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Return the current entry Chris@0: * Chris@0: * @return \Zend\Feed\Reader\Entry\EntryInterface Chris@0: */ Chris@0: public function current() Chris@0: { Chris@17: if (0 === strpos($this->getType(), 'rss')) { Chris@0: $reader = new Reader\Entry\Rss($this->entries[$this->key()], $this->key(), $this->getType()); Chris@0: } else { Chris@0: $reader = new Reader\Entry\Atom($this->entries[$this->key()], $this->key(), $this->getType()); Chris@0: } Chris@0: Chris@0: $reader->setXpath($this->xpath); Chris@0: Chris@0: return $reader; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Get the DOM Chris@0: * Chris@0: * @return DOMDocument Chris@0: */ Chris@0: public function getDomDocument() Chris@0: { Chris@0: return $this->domDocument; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Get the Feed's encoding Chris@0: * Chris@0: * @return string Chris@0: */ Chris@0: public function getEncoding() Chris@0: { Chris@0: $assumed = $this->getDomDocument()->encoding; Chris@0: if (empty($assumed)) { Chris@0: $assumed = 'UTF-8'; Chris@0: } Chris@0: return $assumed; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Get feed as xml Chris@0: * Chris@0: * @return string Chris@0: */ Chris@0: public function saveXml() Chris@0: { Chris@12: return $this->getDomDocument()->saveXML(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Get the DOMElement representing the items/feed element Chris@0: * Chris@0: * @return DOMElement Chris@0: */ Chris@0: public function getElement() Chris@0: { Chris@0: return $this->getDomDocument()->documentElement; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Get the DOMXPath object for this feed Chris@0: * Chris@0: * @return DOMXPath Chris@0: */ Chris@0: public function getXpath() Chris@0: { Chris@0: return $this->xpath; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Get the feed type Chris@0: * Chris@0: * @return string Chris@0: */ Chris@0: public function getType() Chris@0: { Chris@0: return $this->data['type']; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Return the current feed key Chris@0: * Chris@0: * @return int Chris@0: */ Chris@0: public function key() Chris@0: { Chris@0: return $this->entriesKey; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Move the feed pointer forward Chris@0: * Chris@0: */ Chris@0: public function next() Chris@0: { Chris@0: ++$this->entriesKey; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Reset the pointer in the feed object Chris@0: * Chris@0: */ Chris@0: public function rewind() Chris@0: { Chris@0: $this->entriesKey = 0; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Check to see if the iterator is still valid Chris@0: * Chris@0: * @return bool Chris@0: */ Chris@0: public function valid() Chris@0: { Chris@0: return 0 <= $this->entriesKey && $this->entriesKey < $this->count(); Chris@0: } Chris@0: Chris@0: public function getExtensions() Chris@0: { Chris@0: return $this->extensions; Chris@0: } Chris@0: Chris@0: public function __call($method, $args) Chris@0: { Chris@0: foreach ($this->extensions as $extension) { Chris@0: if (method_exists($extension, $method)) { Chris@0: return call_user_func_array([$extension, $method], $args); Chris@0: } Chris@0: } Chris@0: throw new Exception\BadMethodCallException('Method: ' . $method Chris@0: . 'does not exist and could not be located on a registered Extension'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Return an Extension object with the matching name (postfixed with _Feed) Chris@0: * Chris@0: * @param string $name Chris@17: * @return \Zend\Feed\Reader\Extension\AbstractFeed|null Chris@0: */ Chris@0: public function getExtension($name) Chris@0: { Chris@0: if (array_key_exists($name . '\\Feed', $this->extensions)) { Chris@0: return $this->extensions[$name . '\\Feed']; Chris@0: } Chris@0: return; Chris@0: } Chris@0: Chris@0: protected function loadExtensions() Chris@0: { Chris@0: $all = Reader\Reader::getExtensions(); Chris@0: $manager = Reader\Reader::getExtensionManager(); Chris@0: $feed = $all['feed']; Chris@0: foreach ($feed as $extension) { Chris@0: if (in_array($extension, $all['core'])) { Chris@0: continue; Chris@0: } Chris@12: if (! $manager->has($extension)) { Chris@12: throw new Exception\RuntimeException( Chris@12: sprintf('Unable to load extension "%s"; cannot find class', $extension) Chris@12: ); Chris@0: } Chris@0: $plugin = $manager->get($extension); Chris@0: $plugin->setDomDocument($this->getDomDocument()); Chris@0: $plugin->setType($this->data['type']); Chris@0: $plugin->setXpath($this->xpath); Chris@0: $this->extensions[$extension] = $plugin; Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Read all entries to the internal entries array Chris@0: * Chris@0: */ Chris@0: abstract protected function indexEntries(); Chris@0: Chris@0: /** Chris@0: * Register the default namespaces for the current feed format Chris@0: * Chris@0: */ Chris@0: abstract protected function registerNamespaces(); Chris@0: }