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\Entry;
16 use Zend\Feed\Reader\Exception;
18 abstract class AbstractEntry
32 protected $domDocument = null;
39 protected $entry = null;
42 * Pointer to the current entry
46 protected $entryKey = 0;
53 protected $xpath = null;
56 * Registered extensions
60 protected $extensions = [];
65 * @param DOMElement $entry
66 * @param int $entryKey
69 public function __construct(DOMElement $entry, $entryKey, $type = null)
71 $this->entry = $entry;
72 $this->entryKey = $entryKey;
73 $this->domDocument = $entry->ownerDocument;
75 $this->data['type'] = $type;
76 } elseif ($this->domDocument !== null) {
77 $this->data['type'] = Reader\Reader::detectType($this->domDocument);
79 $this->data['type'] = Reader\Reader::TYPE_ANY;
81 $this->loadExtensions();
89 public function getDomDocument()
91 return $this->domDocument;
95 * Get the entry element
99 public function getElement()
105 * Get the Entry's encoding
109 public function getEncoding()
111 $assumed = $this->getDomDocument()->encoding;
112 if (empty($assumed)) {
123 public function saveXml()
125 $dom = new DOMDocument('1.0', $this->getEncoding());
126 $deep = version_compare(PHP_VERSION, '7', 'ge') ? 1 : true;
127 $entry = $dom->importNode($this->getElement(), $deep);
128 $dom->appendChild($entry);
129 return $dom->saveXml();
137 public function getType()
139 return $this->data['type'];
143 * Get the XPath query object
147 public function getXpath()
149 if (! $this->xpath) {
150 $this->setXpath(new DOMXPath($this->getDomDocument()));
156 * Set the XPath query
158 * @param DOMXPath $xpath
159 * @return AbstractEntry
161 public function setXpath(DOMXPath $xpath)
163 $this->xpath = $xpath;
168 * Get registered extensions
172 public function getExtensions()
174 return $this->extensions;
178 * Return an Extension object with the matching name (postfixed with _Entry)
180 * @param string $name
181 * @return Reader\Extension\AbstractEntry
183 public function getExtension($name)
185 if (array_key_exists($name . '\\Entry', $this->extensions)) {
186 return $this->extensions[$name . '\\Entry'];
192 * Method overloading: call given method on first extension implementing it
194 * @param string $method
197 * @throws Exception\RuntimeException if no extensions implements the method
199 public function __call($method, $args)
201 foreach ($this->extensions as $extension) {
202 if (method_exists($extension, $method)) {
203 return call_user_func_array([$extension, $method], $args);
206 throw new Exception\RuntimeException(sprintf(
207 'Method: %s does not exist and could not be located on a registered Extension',
213 * Load extensions from Zend\Feed\Reader\Reader
217 protected function loadExtensions()
219 $all = Reader\Reader::getExtensions();
220 $manager = Reader\Reader::getExtensionManager();
221 $feed = $all['entry'];
222 foreach ($feed as $extension) {
223 if (in_array($extension, $all['core'])) {
226 $plugin = $manager->get($extension);
227 $plugin->setEntryElement($this->getElement());
228 $plugin->setEntryKey($this->entryKey);
229 $plugin->setType($this->data['type']);
230 $this->extensions[$extension] = $plugin;