EntryAbstract.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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 $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. if (!$this->_xpath) {
  145. $this->setXpath(new DOMXPath($this->getDomDocument()));
  146. }
  147. return $this->_xpath;
  148. }
  149. /**
  150. * Set the XPath query
  151. *
  152. * @param DOMXPath $xpath
  153. * @return Zend_Feed_Reader_Entry_EntryAbstract
  154. */
  155. public function setXpath(DOMXPath $xpath)
  156. {
  157. $this->_xpath = $xpath;
  158. return $this;
  159. }
  160. /**
  161. * Get registered extensions
  162. *
  163. * @return array
  164. */
  165. public function getExtensions()
  166. {
  167. return $this->_extensions;
  168. }
  169. /**
  170. * Return an Extension object with the matching name (postfixed with _Entry)
  171. *
  172. * @param string $name
  173. * @return Zend_Feed_Reader_Extension_EntryAbstract
  174. */
  175. public function getExtension($name)
  176. {
  177. if (array_key_exists($name . '_Entry', $this->_extensions)) {
  178. return $this->_extensions[$name . '_Entry'];
  179. }
  180. return null;
  181. }
  182. /**
  183. * Method overloading: call given method on first extension implementing it
  184. *
  185. * @param string $method
  186. * @param array $args
  187. * @return mixed
  188. * @throws Zend_Feed_Exception if no extensions implements the method
  189. */
  190. public function __call($method, $args)
  191. {
  192. foreach ($this->_extensions as $extension) {
  193. if (method_exists($extension, $method)) {
  194. return call_user_func_array(array($extension, $method), $args);
  195. }
  196. }
  197. require_once 'Zend/Feed/Exception.php';
  198. throw new Zend_Feed_Exception('Method: ' . $method
  199. . 'does not exist and could not be located on a registered Extension');
  200. }
  201. /**
  202. * Load extensions from Zend_Feed_Reader
  203. *
  204. * @return void
  205. */
  206. protected function _loadExtensions()
  207. {
  208. $all = Zend_Feed_Reader::getExtensions();
  209. $feed = $all['entry'];
  210. foreach ($feed as $extension) {
  211. if (in_array($extension, $all['core'])) {
  212. continue;
  213. }
  214. $className = Zend_Feed_Reader::getPluginLoader()->getClassName($extension);
  215. $this->_extensions[$extension] = new $className(
  216. $this->getElement(), $this->_entryKey, $this->_data['type']
  217. );
  218. }
  219. }
  220. }