FeedAbstract.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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-2010 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. * @see Zend_Feed_Reader
  23. */
  24. require_once 'Zend/Feed/Reader.php';
  25. /**
  26. * @see Zend_feed_Reader_FeedInterface
  27. */
  28. require_once 'Zend/Feed/Reader/FeedInterface.php';
  29. /**
  30. * @category Zend
  31. * @package Zend_Feed_Reader
  32. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  33. * @license http://framework.zend.com/license/new-bsd New BSD License
  34. */
  35. abstract class Zend_Feed_Reader_FeedAbstract implements Zend_Feed_Reader_FeedInterface
  36. {
  37. /**
  38. * Parsed feed data
  39. *
  40. * @var array
  41. */
  42. protected $_data = array();
  43. /**
  44. * Parsed feed data in the shape of a DOMDocument
  45. *
  46. * @var DOMDocument
  47. */
  48. protected $_domDocument = null;
  49. /**
  50. * An array of parsed feed entries
  51. *
  52. * @var array
  53. */
  54. protected $_entries = array();
  55. /**
  56. * A pointer for the iterator to keep track of the entries array
  57. *
  58. * @var int
  59. */
  60. protected $_entriesKey = 0;
  61. /**
  62. * The base XPath query used to retrieve feed data
  63. *
  64. * @var DOMXPath
  65. */
  66. protected $_xpath = null;
  67. /**
  68. * Array of loaded extensions
  69. *
  70. * @var array
  71. */
  72. protected $_extensions = array();
  73. /**
  74. * Constructor
  75. *
  76. * @param DomDocument The DOM object for the feed's XML
  77. * @param string $type Feed type
  78. */
  79. public function __construct(DomDocument $domDocument, $type = null)
  80. {
  81. $this->_domDocument = $domDocument;
  82. $this->_xpath = new DOMXPath($this->_domDocument);
  83. if ($type !== null) {
  84. $this->_data['type'] = $type;
  85. } else {
  86. $this->_data['type'] = Zend_Feed_Reader::detectType($this->_domDocument);
  87. }
  88. $this->_registerNamespaces();
  89. $this->_indexEntries();
  90. $this->_loadExtensions();
  91. }
  92. /**
  93. * Get the number of feed entries.
  94. * Required by the Iterator interface.
  95. *
  96. * @return int
  97. */
  98. public function count()
  99. {
  100. return count($this->_entries);
  101. }
  102. /**
  103. * Return the current entry
  104. *
  105. * @return Zend_Feed_Reader_EntryInterface
  106. */
  107. public function current()
  108. {
  109. if (substr($this->getType(), 0, 3) == 'rss') {
  110. $reader = new Zend_Feed_Reader_Entry_Rss($this->_entries[$this->key()], $this->key(), $this->getType());
  111. } else {
  112. $reader = new Zend_Feed_Reader_Entry_Atom($this->_entries[$this->key()], $this->key(), $this->getType());
  113. }
  114. $reader->setXpath($this->_xpath);
  115. return $reader;
  116. }
  117. /**
  118. * Get the DOM
  119. *
  120. * @return DOMDocument
  121. */
  122. public function getDomDocument()
  123. {
  124. return $this->_domDocument;
  125. }
  126. /**
  127. * Get the Feed's encoding
  128. *
  129. * @return string
  130. */
  131. public function getEncoding()
  132. {
  133. $assumed = $this->getDomDocument()->encoding;
  134. if (empty($assumed)) {
  135. $assumed = 'UTF-8';
  136. }
  137. return $assumed;
  138. }
  139. /**
  140. * Get feed as xml
  141. *
  142. * @return string
  143. */
  144. public function saveXml()
  145. {
  146. return $this->getDomDocument()->saveXml();
  147. }
  148. /**
  149. * Get the DOMElement representing the items/feed element
  150. *
  151. * @return DOMElement
  152. */
  153. public function getElement()
  154. {
  155. return $this->getDomDocument()->documentElement;
  156. }
  157. /**
  158. * Get the DOMXPath object for this feed
  159. *
  160. * @return DOMXPath
  161. */
  162. public function getXpath()
  163. {
  164. return $this->_xpath;
  165. }
  166. /**
  167. * Get the feed type
  168. *
  169. * @return string
  170. */
  171. public function getType()
  172. {
  173. return $this->_data['type'];
  174. }
  175. /**
  176. * Return the current feed key
  177. *
  178. * @return unknown
  179. */
  180. public function key()
  181. {
  182. return $this->_entriesKey;
  183. }
  184. /**
  185. * Move the feed pointer forward
  186. *
  187. */
  188. public function next()
  189. {
  190. ++$this->_entriesKey;
  191. }
  192. /**
  193. * Reset the pointer in the feed object
  194. *
  195. */
  196. public function rewind()
  197. {
  198. $this->_entriesKey = 0;
  199. }
  200. /**
  201. * Check to see if the iterator is still valid
  202. *
  203. * @return boolean
  204. */
  205. public function valid()
  206. {
  207. return 0 <= $this->_entriesKey && $this->_entriesKey < $this->count();
  208. }
  209. public function getExtensions()
  210. {
  211. return $this->_extensions;
  212. }
  213. public function __call($method, $args)
  214. {
  215. foreach ($this->_extensions as $extension) {
  216. if (method_exists($extension, $method)) {
  217. return call_user_func_array(array($extension, $method), $args);
  218. }
  219. }
  220. require_once 'Zend/Feed/Exception.php';
  221. throw new Zend_Feed_Exception('Method: ' . $method
  222. . 'does not exist and could not be located on a registered Extension');
  223. }
  224. /**
  225. * Return an Extension object with the matching name (postfixed with _Feed)
  226. *
  227. * @param string $name
  228. * @return Zend_Feed_Reader_Extension_FeedAbstract
  229. */
  230. public function getExtension($name)
  231. {
  232. if (array_key_exists($name . '_Feed', $this->_extensions)) {
  233. return $this->_extensions[$name . '_Feed'];
  234. }
  235. return null;
  236. }
  237. protected function _loadExtensions()
  238. {
  239. $all = Zend_Feed_Reader::getExtensions();
  240. $feed = $all['feed'];
  241. foreach ($feed as $extension) {
  242. if (in_array($extension, $all['core'])) {
  243. continue;
  244. }
  245. $className = Zend_Feed_Reader::getPluginLoader()->getClassName($extension);
  246. $this->_extensions[$extension] = new $className(
  247. $this->getDomDocument(), $this->_data['type'], $this->_xpath
  248. );
  249. }
  250. }
  251. /**
  252. * Read all entries to the internal entries array
  253. *
  254. */
  255. abstract protected function _indexEntries();
  256. /**
  257. * Register the default namespaces for the current feed format
  258. *
  259. */
  260. abstract protected function _registerNamespaces();
  261. }