EntryAbstract.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Feed_Reader
  17. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id$
  20. */
  21. /**
  22. * @category Zend
  23. * @package Zend_Feed_Reader
  24. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  25. * @license http://framework.zend.com/license/new-bsd New BSD License
  26. */
  27. abstract class Zend_Feed_Reader_EntryAbstract
  28. {
  29. /**
  30. * Feed entry data
  31. *
  32. * @var array
  33. */
  34. protected $_data = array();
  35. /**
  36. * DOM document object
  37. *
  38. * @var DOMDocument
  39. */
  40. protected $_domDocument = null;
  41. /**
  42. * Entry instance
  43. *
  44. * @var Zend_Feed_Entry_Interface
  45. */
  46. protected $_entry = null;
  47. /**
  48. * Pointer to the current entry
  49. *
  50. * @var int
  51. */
  52. protected $_entryKey = 0;
  53. /**
  54. * XPath object
  55. *
  56. * @var DOMXPath
  57. */
  58. protected $_xpath = null;
  59. /**
  60. * Registered extensions
  61. *
  62. * @var array
  63. */
  64. protected $_extensions = array();
  65. /**
  66. * Constructor
  67. *
  68. * @param DOMElement $entry
  69. * @param int $entryKey
  70. * @param string|null $type
  71. * @return void
  72. */
  73. public function __construct(DOMElement $entry, $entryKey, $type = null)
  74. {
  75. $this->_entry = $entry;
  76. $this->_entryKey = $entryKey;
  77. $this->_domDocument = $entry->ownerDocument;
  78. if ($type !== null) {
  79. $this->_data['type'] = $type;
  80. } else {
  81. $this->_data['type'] = Zend_Feed_Reader::detectType(
  82. $this->_domDocument
  83. );
  84. }
  85. $this->_loadExtensions();
  86. }
  87. /**
  88. * Get the DOM
  89. *
  90. * @return DOMDocument
  91. */
  92. public function getDomDocument()
  93. {
  94. return $this->_domDocument;
  95. }
  96. /**
  97. * Get the entry element
  98. *
  99. * @return DOMElement
  100. */
  101. public function getElement()
  102. {
  103. return $this->_entry;
  104. }
  105. /**
  106. * Get the Entry's encoding
  107. *
  108. * @return string
  109. */
  110. public function getEncoding()
  111. {
  112. $assumed = $this->getDomDocument()->encoding;
  113. if (empty($assumed)) {
  114. $assumed = 'UTF-8';
  115. }
  116. return $assumed;
  117. }
  118. /**
  119. * Get entry as xml
  120. *
  121. * @return string
  122. */
  123. public function saveXml()
  124. {
  125. $dom = new DOMDocument('1.0', $this->getEncoding());
  126. $entry = $dom->importNode($this->getElement(), true);
  127. $dom->appendChild($entry);
  128. return $dom->saveXml();
  129. }
  130. /**
  131. * Get the entry type
  132. *
  133. * @return string
  134. */
  135. public function getType()
  136. {
  137. return $this->_data['type'];
  138. }
  139. /**
  140. * Get the XPath query object
  141. *
  142. * @return DOMXPath
  143. */
  144. public function getXpath()
  145. {
  146. if (!$this->_xpath) {
  147. $this->setXpath(new DOMXPath($this->getDomDocument()));
  148. }
  149. return $this->_xpath;
  150. }
  151. /**
  152. * Set the XPath query
  153. *
  154. * @param DOMXPath $xpath
  155. * @return Zend_Feed_Reader_Entry_EntryAbstract
  156. */
  157. public function setXpath(DOMXPath $xpath)
  158. {
  159. $this->_xpath = $xpath;
  160. return $this;
  161. }
  162. /**
  163. * Get registered extensions
  164. *
  165. * @return array
  166. */
  167. public function getExtensions()
  168. {
  169. return $this->_extensions;
  170. }
  171. /**
  172. * Return an Extension object with the matching name (postfixed with _Entry)
  173. *
  174. * @param string $name
  175. * @return Zend_Feed_Reader_Extension_EntryAbstract
  176. */
  177. public function getExtension($name)
  178. {
  179. if (array_key_exists($name . '_Entry', $this->_extensions)) {
  180. return $this->_extensions[$name . '_Entry'];
  181. }
  182. return null;
  183. }
  184. /**
  185. * Method overloading: call given method on first extension implementing it
  186. *
  187. * @param string $method
  188. * @param array $args
  189. * @return mixed
  190. * @throws Zend_Feed_Exception if no extensions implements the method
  191. */
  192. public function __call($method, $args)
  193. {
  194. foreach ($this->_extensions as $extension) {
  195. if (method_exists($extension, $method)) {
  196. return call_user_func_array(array($extension, $method), $args);
  197. }
  198. }
  199. require_once 'Zend/Feed/Exception.php';
  200. throw new Zend_Feed_Exception(
  201. 'Method: ' . $method
  202. . 'does not exist and could not be located on a registered Extension'
  203. );
  204. }
  205. /**
  206. * Load extensions from Zend_Feed_Reader
  207. *
  208. * @return void
  209. */
  210. protected function _loadExtensions()
  211. {
  212. $all = Zend_Feed_Reader::getExtensions();
  213. $feed = $all['entry'];
  214. foreach ($feed as $extension) {
  215. if (in_array($extension, $all['core'])) {
  216. continue;
  217. }
  218. $className = Zend_Feed_Reader::getPluginLoader()->getClassName($extension);
  219. $this->_extensions[$extension] = new $className(
  220. $this->getElement(), $this->_entryKey, $this->_data['type']
  221. );
  222. }
  223. }
  224. }