PageRenderTime 52ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 1ms

/library/Zend/Feed/Reader/Feed/AbstractFeed.php

https://github.com/Exercise/zf2
PHP | 319 lines | 129 code | 32 blank | 158 comment | 10 complexity | ce974baa1be30fb5129e886e7e379b0c MD5 | raw file
  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: FeedAbstract.php 22092 2010-05-04 12:50:51Z padraic $
  20. */
  21. /**
  22. * @namespace
  23. */
  24. namespace Zend\Feed\Reader\Feed;
  25. use Zend\Feed\Reader;
  26. /**
  27. * @uses \Zend\Feed\Exception
  28. * @uses \Zend\Feed\Reader\Reader
  29. * @uses \Zend\Feed\Reader\Feed
  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 AbstractFeed implements Reader\Feed
  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. * Original Source URI (set if imported from a URI)
  75. *
  76. * @var string
  77. */
  78. protected $_originalSourceUri = null;
  79. /**
  80. * Constructor
  81. *
  82. * @param DomDocument The DOM object for the feed's XML
  83. * @param string $type Feed type
  84. */
  85. public function __construct(\DomDocument $domDocument, $type = null)
  86. {
  87. $this->_domDocument = $domDocument;
  88. $this->_xpath = new \DOMXPath($this->_domDocument);
  89. if ($type !== null) {
  90. $this->_data['type'] = $type;
  91. } else {
  92. $this->_data['type'] = Reader\Reader::detectType($this->_domDocument);
  93. }
  94. $this->_registerNamespaces();
  95. $this->_indexEntries();
  96. $this->_loadExtensions();
  97. }
  98. /**
  99. * Set an original source URI for the feed being parsed. This value
  100. * is returned from getFeedLink() method if the feed does not carry
  101. * a self-referencing URI.
  102. *
  103. * @param string $uri
  104. */
  105. public function setOriginalSourceUri($uri)
  106. {
  107. $this->_originalSourceUri = $uri;
  108. }
  109. /**
  110. * Get an original source URI for the feed being parsed. Returns null if
  111. * unset or the feed was not imported from a URI.
  112. *
  113. * @return string|null
  114. */
  115. public function getOriginalSourceUri()
  116. {
  117. return $this->_originalSourceUri;
  118. }
  119. /**
  120. * Get the number of feed entries.
  121. * Required by the Iterator interface.
  122. *
  123. * @return int
  124. */
  125. public function count()
  126. {
  127. return count($this->_entries);
  128. }
  129. /**
  130. * Return the current entry
  131. *
  132. * @return Zend_Feed_Reader_EntryInterface
  133. */
  134. public function current()
  135. {
  136. if (substr($this->getType(), 0, 3) == 'rss') {
  137. $reader = new Reader\Entry\Rss($this->_entries[$this->key()], $this->key(), $this->getType());
  138. } else {
  139. $reader = new Reader\Entry\Atom($this->_entries[$this->key()], $this->key(), $this->getType());
  140. }
  141. $reader->setXpath($this->_xpath);
  142. return $reader;
  143. }
  144. /**
  145. * Get the DOM
  146. *
  147. * @return DOMDocument
  148. */
  149. public function getDomDocument()
  150. {
  151. return $this->_domDocument;
  152. }
  153. /**
  154. * Get the Feed's encoding
  155. *
  156. * @return string
  157. */
  158. public function getEncoding()
  159. {
  160. $assumed = $this->getDomDocument()->encoding;
  161. if (empty($assumed)) {
  162. $assumed = 'UTF-8';
  163. }
  164. return $assumed;
  165. }
  166. /**
  167. * Get feed as xml
  168. *
  169. * @return string
  170. */
  171. public function saveXml()
  172. {
  173. return $this->getDomDocument()->saveXml();
  174. }
  175. /**
  176. * Get the DOMElement representing the items/feed element
  177. *
  178. * @return DOMElement
  179. */
  180. public function getElement()
  181. {
  182. return $this->getDomDocument()->documentElement;
  183. }
  184. /**
  185. * Get the DOMXPath object for this feed
  186. *
  187. * @return DOMXPath
  188. */
  189. public function getXpath()
  190. {
  191. return $this->_xpath;
  192. }
  193. /**
  194. * Get the feed type
  195. *
  196. * @return string
  197. */
  198. public function getType()
  199. {
  200. return $this->_data['type'];
  201. }
  202. /**
  203. * Return the current feed key
  204. *
  205. * @return unknown
  206. */
  207. public function key()
  208. {
  209. return $this->_entriesKey;
  210. }
  211. /**
  212. * Move the feed pointer forward
  213. *
  214. */
  215. public function next()
  216. {
  217. ++$this->_entriesKey;
  218. }
  219. /**
  220. * Reset the pointer in the feed object
  221. *
  222. */
  223. public function rewind()
  224. {
  225. $this->_entriesKey = 0;
  226. }
  227. /**
  228. * Check to see if the iterator is still valid
  229. *
  230. * @return boolean
  231. */
  232. public function valid()
  233. {
  234. return 0 <= $this->_entriesKey && $this->_entriesKey < $this->count();
  235. }
  236. public function getExtensions()
  237. {
  238. return $this->_extensions;
  239. }
  240. public function __call($method, $args)
  241. {
  242. foreach ($this->_extensions as $extension) {
  243. if (method_exists($extension, $method)) {
  244. return call_user_func_array(array($extension, $method), $args);
  245. }
  246. }
  247. throw new Exception('Method: ' . $method
  248. . 'does not exist and could not be located on a registered Extension');
  249. }
  250. /**
  251. * Return an Extension object with the matching name (postfixed with _Feed)
  252. *
  253. * @param string $name
  254. * @return Zend_Feed_Reader_Extension_FeedAbstract
  255. */
  256. public function getExtension($name)
  257. {
  258. if (array_key_exists($name . '\\Feed', $this->_extensions)) {
  259. return $this->_extensions[$name . '\\Feed'];
  260. }
  261. return null;
  262. }
  263. protected function _loadExtensions()
  264. {
  265. $all = Reader\Reader::getExtensions();
  266. $feed = $all['feed'];
  267. foreach ($feed as $extension) {
  268. if (in_array($extension, $all['core'])) {
  269. continue;
  270. }
  271. $className = Reader\Reader::getPluginLoader()->getClassName($extension);
  272. $this->_extensions[$extension] = new $className(
  273. $this->getDomDocument(), $this->_data['type'], $this->_xpath
  274. );
  275. }
  276. }
  277. /**
  278. * Read all entries to the internal entries array
  279. *
  280. */
  281. abstract protected function _indexEntries();
  282. /**
  283. * Register the default namespaces for the current feed format
  284. *
  285. */
  286. abstract protected function _registerNamespaces();
  287. }