FeedAbstract.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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_EntryInterface
  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. if (empty($assumed)) {
  138. $assumed = 'UTF-8';
  139. }
  140. return $assumed;
  141. }
  142. /**
  143. * Get feed as xml
  144. *
  145. * @return string
  146. */
  147. public function saveXml()
  148. {
  149. return $this->getDomDocument()->saveXml();
  150. }
  151. /**
  152. * Get the DOMElement representing the items/feed element
  153. *
  154. * @return DOMElement
  155. */
  156. public function getElement()
  157. {
  158. return $this->getDomDocument()->documentElement;
  159. }
  160. /**
  161. * Get the DOMXPath object for this feed
  162. *
  163. * @return DOMXPath
  164. */
  165. public function getXpath()
  166. {
  167. return $this->_xpath;
  168. }
  169. /**
  170. * Get the feed type
  171. *
  172. * @return string
  173. */
  174. public function getType()
  175. {
  176. return $this->_data['type'];
  177. }
  178. /**
  179. * Return the current feed key
  180. *
  181. * @return unknown
  182. */
  183. public function key()
  184. {
  185. return $this->_entriesKey;
  186. }
  187. /**
  188. * Move the feed pointer forward
  189. *
  190. */
  191. public function next()
  192. {
  193. ++$this->_entriesKey;
  194. }
  195. /**
  196. * Reset the pointer in the feed object
  197. *
  198. */
  199. public function rewind()
  200. {
  201. $this->_entriesKey = 0;
  202. }
  203. /**
  204. * Return the feed as an array
  205. *
  206. * @return array
  207. */
  208. public function toArray() // untested
  209. {
  210. return $this->_data;
  211. }
  212. /**
  213. * Check to see if the iterator is still valid
  214. *
  215. * @return boolean
  216. */
  217. public function valid()
  218. {
  219. return 0 <= $this->_entriesKey && $this->_entriesKey < $this->count();
  220. }
  221. public function getExtensions()
  222. {
  223. return $this->_extensions;
  224. }
  225. public function __call($method, $args)
  226. {
  227. foreach ($this->_extensions as $extension) {
  228. if (method_exists($extension, $method)) {
  229. return call_user_func_array(array($extension, $method), $args);
  230. }
  231. }
  232. require_once 'Zend/Feed/Exception.php';
  233. throw new Zend_Feed_Exception('Method: ' . $method
  234. . 'does not exist and could not be located on a registered Extension');
  235. }
  236. /**
  237. * Return an Extension object with the matching name (postfixed with _Feed)
  238. *
  239. * @param string $name
  240. * @return Zend_Feed_Reader_Extension_FeedAbstract
  241. */
  242. public function getExtension($name)
  243. {
  244. if (array_key_exists($name . '_Feed', $this->_extensions)) {
  245. return $this->_extensions[$name . '_Feed'];
  246. }
  247. return null;
  248. }
  249. protected function _loadExtensions()
  250. {
  251. $all = Zend_Feed_Reader::getExtensions();
  252. $feed = $all['feed'];
  253. foreach ($feed as $extension) {
  254. if (in_array($extension, $all['core'])) {
  255. continue;
  256. }
  257. $className = Zend_Feed_Reader::getPluginLoader()->getClassName($extension);
  258. $this->_extensions[$extension] = new $className(
  259. $this->getDomDocument(), $this->_data['type'], $this->_xpath
  260. );
  261. }
  262. }
  263. /**
  264. * Read all entries to the internal entries array
  265. *
  266. */
  267. abstract protected function _indexEntries();
  268. /**
  269. * Register the default namespaces for the current feed format
  270. *
  271. */
  272. abstract protected function _registerNamespaces();
  273. }