FeedAbstract.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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-2008 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_Entry_Atom
  27. */
  28. require_once 'Zend/Feed/Reader/Entry/Atom.php';
  29. /**
  30. * @see Zend_Feed_Reader_Entry_Rss
  31. */
  32. require_once 'Zend/Feed/Reader/Entry/Rss.php';
  33. /**
  34. * @see Zend_feed_Reader_FeedInterface
  35. */
  36. require_once 'Zend/Feed/Reader/FeedInterface.php';
  37. /**
  38. * @category Zend
  39. * @package Zend_Feed_Reader
  40. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  41. * @license http://framework.zend.com/license/new-bsd New BSD License
  42. */
  43. abstract class Zend_Feed_Reader_FeedAbstract implements Zend_Feed_Reader_FeedInterface
  44. {
  45. /**
  46. * Parsed feed data
  47. *
  48. * @var array
  49. */
  50. protected $_data = array();
  51. /**
  52. * Parsed feed data in the shape of a DOMDocument
  53. *
  54. * @var DOMDocument
  55. */
  56. protected $_domDocument = null;
  57. /**
  58. * An array of parsed feed entries
  59. *
  60. * @var array
  61. */
  62. protected $_entries = array();
  63. /**
  64. * A pointer for the iterator to keep track of the entries array
  65. *
  66. * @var int
  67. */
  68. protected $_entriesKey = 0;
  69. /**
  70. * The base XPath query used to retrieve feed data
  71. *
  72. * @var DOMXPath
  73. */
  74. protected $_xpath = null;
  75. protected $_extensions = array();
  76. /**
  77. * Constructor
  78. *
  79. * @param DomDocument The DOM object for the feed's XML
  80. * @param string $type Feed type
  81. */
  82. public function __construct(DomDocument $domDocument, $type = null)
  83. {
  84. $this->_domDocument = $domDocument;
  85. $this->_xpath = new DOMXPath($this->_domDocument);
  86. if ($type !== null) {
  87. $this->_data['type'] = $type;
  88. } else {
  89. $this->_data['type'] = Zend_Feed_Reader::detectType($this->_domDocument);
  90. }
  91. $this->_registerNamespaces();
  92. $this->_indexEntries();
  93. $this->_loadExtensions();
  94. }
  95. /**
  96. * Get the number of feed entries.
  97. * Required by the Iterator interface.
  98. *
  99. * @return int
  100. */
  101. public function count()
  102. {
  103. return count($this->_entries);
  104. }
  105. /**
  106. * Return the current entry
  107. *
  108. * @return Zend_Feed_Reader_Entry_Interface
  109. */
  110. public function current()
  111. {
  112. if (substr($this->getType(), 0, 3) == 'rss') {
  113. $reader = new Zend_Feed_Reader_Entry_Rss($this->_entries[$this->key()], $this->key(), $this->getType());
  114. } else {
  115. $reader = new Zend_Feed_Reader_Entry_Atom($this->_entries[$this->key()], $this->key(), $this->getType());
  116. }
  117. $reader->setXpath($this->_xpath);
  118. return $reader;
  119. }
  120. /**
  121. * Get the DOM
  122. *
  123. * @return DOMDocument
  124. */
  125. public function getDomDocument()
  126. {
  127. return $this->_domDocument;
  128. }
  129. /**
  130. * Get the Feed's encoding
  131. *
  132. * @return string
  133. */
  134. public function getEncoding()
  135. {
  136. $assumed = $this->getDomDocument()->encoding;
  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. * Return the feed as an array
  202. *
  203. * @return array
  204. */
  205. public function toArray() // untested
  206. {
  207. return $this->_data;
  208. }
  209. /**
  210. * Check to see if the iterator is still valid
  211. *
  212. * @return boolean
  213. */
  214. public function valid()
  215. {
  216. return 0 <= $this->_entriesKey && $this->_entriesKey < $this->count();
  217. }
  218. public function getExtensions()
  219. {
  220. return $this->_extensions;
  221. }
  222. public function __call($method, $args)
  223. {
  224. foreach ($this->_extensions as $extension) {
  225. if (method_exists($extension, $method)) {
  226. return call_user_func_array(array($extension, $method), $args);
  227. }
  228. }
  229. require_once 'Zend/Feed/Exception.php';
  230. throw new Zend_Feed_Exception('Method: ' . $method
  231. . 'does not exist and could not be located on a registered Extension');
  232. }
  233. /**
  234. * Return an Extension object with the matching name (postfixed with _Feed)
  235. *
  236. * @param string $name
  237. * @return Zend_Feed_Reader_Extension_FeedAbstract
  238. */
  239. public function getExtension($name)
  240. {
  241. if (array_key_exists($name . '_Feed', $this->_extensions)) {
  242. return $this->_extensions[$name . '_Feed'];
  243. }
  244. return null;
  245. }
  246. protected function _loadExtensions()
  247. {
  248. $all = Zend_Feed_Reader::getExtensions();
  249. $feed = $all['feed'];
  250. foreach ($feed as $extension) {
  251. if (in_array($extension, $all['core'])) {
  252. continue;
  253. }
  254. $className = Zend_Feed_Reader::getPluginLoader()->getClassName($extension);
  255. $this->_extensions[$extension] = new $className(
  256. $this->getDomDocument(), $this->_data['type'], $this->_xpath
  257. );
  258. }
  259. }
  260. /**
  261. * Read all entries to the internal entries array
  262. *
  263. */
  264. abstract protected function _indexEntries();
  265. /**
  266. * Register the default namespaces for the current feed format
  267. *
  268. */
  269. abstract protected function _registerNamespaces();
  270. }