EntryAbstract.php 5.4 KB

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