EntryAbstract.php 5.5 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-2009 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-2009 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 $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($feed);
  82. }
  83. $this->_loadExtensions();
  84. }
  85. /**
  86. * Get the DOM
  87. *
  88. * @return DOMDocument
  89. */
  90. public function getDomDocument()
  91. {
  92. return $this->_domDocument;
  93. }
  94. /**
  95. * Get the entry element
  96. *
  97. * @return DOMElement
  98. */
  99. public function getElement()
  100. {
  101. return $this->_entry;
  102. }
  103. /**
  104. * Get the Entry's encoding
  105. *
  106. * @return string
  107. */
  108. public function getEncoding()
  109. {
  110. $assumed = $this->getDomDocument()->encoding;
  111. return $assumed;
  112. }
  113. /**
  114. * Get entry as xml
  115. *
  116. * @return string
  117. */
  118. public function saveXml()
  119. {
  120. $dom = new DOMDocument('1.0', $this->getEncoding());
  121. $entry = $dom->importNode($this->getElement(), true);
  122. $dom->appendChild($entry);
  123. return $dom->saveXml();
  124. }
  125. /**
  126. * Get the entry type
  127. *
  128. * @return string
  129. */
  130. public function getType()
  131. {
  132. return $this->_data['type'];
  133. }
  134. /**
  135. * Get the XPath query object
  136. *
  137. * @return DOMXPath
  138. */
  139. public function getXpath()
  140. {
  141. return $this->_xpath;
  142. }
  143. /**
  144. * Set the XPath query
  145. *
  146. * @param DOMXPath $xpath
  147. * @return Zend_Feed_Reader_Entry_EntryAbstract
  148. */
  149. public function setXpath(DOMXPath $xpath)
  150. {
  151. $this->_xpath = $xpath;
  152. return $this;
  153. }
  154. /**
  155. * Serialize the entry to an array
  156. *
  157. * @return array
  158. */
  159. public function toArray()
  160. {
  161. return $this->_data;
  162. }
  163. /**
  164. * Get registered extensions
  165. *
  166. * @return array
  167. */
  168. public function getExtensions()
  169. {
  170. return $this->_extensions;
  171. }
  172. /**
  173. * Return an Extension object with the matching name (postfixed with _Entry)
  174. *
  175. * @param string $name
  176. * @return Zend_Feed_Reader_Extension_EntryAbstract
  177. */
  178. public function getExtension($name)
  179. {
  180. if (array_key_exists($name . '_Entry', $this->_extensions)) {
  181. return $this->_extensions[$name . '_Entry'];
  182. }
  183. return null;
  184. }
  185. /**
  186. * Method overloading: call given method on first extension implementing it
  187. *
  188. * @param string $method
  189. * @param array $args
  190. * @return mixed
  191. * @throws Zend_Feed_Exception if no extensions implements the method
  192. */
  193. public function __call($method, $args)
  194. {
  195. foreach ($this->_extensions as $extension) {
  196. if (method_exists($extension, $method)) {
  197. return call_user_func_array(array($extension, $method), $args);
  198. }
  199. }
  200. require_once 'Zend/Feed/Exception.php';
  201. throw new Zend_Feed_Exception('Method: ' . $method
  202. . 'does not exist and could not be located on a registered Extension');
  203. }
  204. /**
  205. * Load extensions from Zend_Feed_Reader
  206. *
  207. * @return void
  208. */
  209. protected function _loadExtensions()
  210. {
  211. $all = Zend_Feed_Reader::getExtensions();
  212. $feed = $all['entry'];
  213. foreach ($feed as $extension) {
  214. if (in_array($extension, $all['core'])) {
  215. continue;
  216. }
  217. $className = Zend_Feed_Reader::getPluginLoader()->getClassName($extension);
  218. $this->_extensions[$extension] = new $className(
  219. $this->getElement(), $this->_entryKey, $this->_data['type']
  220. );
  221. }
  222. }
  223. }