PageRenderTime 49ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/library/Zend/Feed/Reader/Feed/Atom.php

https://github.com/shevron/zf2
PHP | 403 lines | 211 code | 79 blank | 113 comment | 30 complexity | 3cc8a64ac1c62db731b97e7b54bd1938 MD5 | raw file
  1. <?php
  2. /**
  3. * Zend Framework (http://framework.zend.com/)
  4. *
  5. * @link http://github.com/zendframework/zf2 for the canonical source repository
  6. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  7. * @license http://framework.zend.com/license/new-bsd New BSD License
  8. * @package Zend_Feed
  9. */
  10. namespace Zend\Feed\Reader\Feed;
  11. use DOMDocument;
  12. use Zend\Feed\Reader;
  13. /**
  14. * @category Zend
  15. * @package Reader
  16. */
  17. class Atom extends AbstractFeed
  18. {
  19. /**
  20. * Constructor
  21. *
  22. * @param DOMDocument $dom
  23. * @param string $type
  24. */
  25. public function __construct(DOMDocument $dom, $type = null)
  26. {
  27. parent::__construct($dom, $type);
  28. $atomClass = Reader\Reader::getPluginLoader()->getClassName('Atom\\Feed');
  29. $this->_extensions['Atom\\Feed'] = new $atomClass($dom, $this->_data['type'], $this->_xpath);
  30. $atomClass = Reader\Reader::getPluginLoader()->getClassName('DublinCore\\Feed');
  31. $this->_extensions['DublinCore\\Feed'] = new $atomClass($dom, $this->_data['type'], $this->_xpath);
  32. foreach ($this->_extensions as $extension) {
  33. $extension->setXpathPrefix('/atom:feed');
  34. }
  35. }
  36. /**
  37. * Get a single author
  38. *
  39. * @param int $index
  40. * @return string|null
  41. */
  42. public function getAuthor($index = 0)
  43. {
  44. $authors = $this->getAuthors();
  45. if (isset($authors[$index])) {
  46. return $authors[$index];
  47. }
  48. return null;
  49. }
  50. /**
  51. * Get an array with feed authors
  52. *
  53. * @return array
  54. */
  55. public function getAuthors()
  56. {
  57. if (array_key_exists('authors', $this->_data)) {
  58. return $this->_data['authors'];
  59. }
  60. $authors = $this->getExtension('Atom')->getAuthors();
  61. $this->_data['authors'] = $authors;
  62. return $this->_data['authors'];
  63. }
  64. /**
  65. * Get the copyright entry
  66. *
  67. * @return string|null
  68. */
  69. public function getCopyright()
  70. {
  71. if (array_key_exists('copyright', $this->_data)) {
  72. return $this->_data['copyright'];
  73. }
  74. $copyright = $this->getExtension('Atom')->getCopyright();
  75. if (!$copyright) {
  76. $copyright = null;
  77. }
  78. $this->_data['copyright'] = $copyright;
  79. return $this->_data['copyright'];
  80. }
  81. /**
  82. * Get the feed creation date
  83. *
  84. * @return string|null
  85. */
  86. public function getDateCreated()
  87. {
  88. if (array_key_exists('datecreated', $this->_data)) {
  89. return $this->_data['datecreated'];
  90. }
  91. $dateCreated = $this->getExtension('Atom')->getDateCreated();
  92. if (!$dateCreated) {
  93. $dateCreated = null;
  94. }
  95. $this->_data['datecreated'] = $dateCreated;
  96. return $this->_data['datecreated'];
  97. }
  98. /**
  99. * Get the feed modification date
  100. *
  101. * @return string|null
  102. */
  103. public function getDateModified()
  104. {
  105. if (array_key_exists('datemodified', $this->_data)) {
  106. return $this->_data['datemodified'];
  107. }
  108. $dateModified = $this->getExtension('Atom')->getDateModified();
  109. if (!$dateModified) {
  110. $dateModified = null;
  111. }
  112. $this->_data['datemodified'] = $dateModified;
  113. return $this->_data['datemodified'];
  114. }
  115. /**
  116. * Get the feed lastBuild date. This is not implemented in Atom.
  117. *
  118. * @return string|null
  119. */
  120. public function getLastBuildDate()
  121. {
  122. return null;
  123. }
  124. /**
  125. * Get the feed description
  126. *
  127. * @return string|null
  128. */
  129. public function getDescription()
  130. {
  131. if (array_key_exists('description', $this->_data)) {
  132. return $this->_data['description'];
  133. }
  134. $description = $this->getExtension('Atom')->getDescription();
  135. if (!$description) {
  136. $description = null;
  137. }
  138. $this->_data['description'] = $description;
  139. return $this->_data['description'];
  140. }
  141. /**
  142. * Get the feed generator entry
  143. *
  144. * @return string|null
  145. */
  146. public function getGenerator()
  147. {
  148. if (array_key_exists('generator', $this->_data)) {
  149. return $this->_data['generator'];
  150. }
  151. $generator = $this->getExtension('Atom')->getGenerator();
  152. $this->_data['generator'] = $generator;
  153. return $this->_data['generator'];
  154. }
  155. /**
  156. * Get the feed ID
  157. *
  158. * @return string|null
  159. */
  160. public function getId()
  161. {
  162. if (array_key_exists('id', $this->_data)) {
  163. return $this->_data['id'];
  164. }
  165. $id = $this->getExtension('Atom')->getId();
  166. $this->_data['id'] = $id;
  167. return $this->_data['id'];
  168. }
  169. /**
  170. * Get the feed language
  171. *
  172. * @return string|null
  173. */
  174. public function getLanguage()
  175. {
  176. if (array_key_exists('language', $this->_data)) {
  177. return $this->_data['language'];
  178. }
  179. $language = $this->getExtension('Atom')->getLanguage();
  180. if (!$language) {
  181. $language = $this->_xpath->evaluate('string(//@xml:lang[1])');
  182. }
  183. if (!$language) {
  184. $language = null;
  185. }
  186. $this->_data['language'] = $language;
  187. return $this->_data['language'];
  188. }
  189. /**
  190. * Get a link to the source website
  191. *
  192. * @return string|null
  193. */
  194. public function getBaseUrl()
  195. {
  196. if (array_key_exists('baseUrl', $this->_data)) {
  197. return $this->_data['baseUrl'];
  198. }
  199. $baseUrl = $this->getExtension('Atom')->getBaseUrl();
  200. $this->_data['baseUrl'] = $baseUrl;
  201. return $this->_data['baseUrl'];
  202. }
  203. /**
  204. * Get a link to the source website
  205. *
  206. * @return string|null
  207. */
  208. public function getLink()
  209. {
  210. if (array_key_exists('link', $this->_data)) {
  211. return $this->_data['link'];
  212. }
  213. $link = $this->getExtension('Atom')->getLink();
  214. $this->_data['link'] = $link;
  215. return $this->_data['link'];
  216. }
  217. /**
  218. * Get feed image data
  219. *
  220. * @return array|null
  221. */
  222. public function getImage()
  223. {
  224. if (array_key_exists('image', $this->_data)) {
  225. return $this->_data['image'];
  226. }
  227. $link = $this->getExtension('Atom')->getImage();
  228. $this->_data['image'] = $link;
  229. return $this->_data['image'];
  230. }
  231. /**
  232. * Get a link to the feed's XML Url
  233. *
  234. * @return string|null
  235. */
  236. public function getFeedLink()
  237. {
  238. if (array_key_exists('feedlink', $this->_data)) {
  239. return $this->_data['feedlink'];
  240. }
  241. $link = $this->getExtension('Atom')->getFeedLink();
  242. if ($link === null || empty($link)) {
  243. $link = $this->getOriginalSourceUri();
  244. }
  245. $this->_data['feedlink'] = $link;
  246. return $this->_data['feedlink'];
  247. }
  248. /**
  249. * Get the feed title
  250. *
  251. * @return string|null
  252. */
  253. public function getTitle()
  254. {
  255. if (array_key_exists('title', $this->_data)) {
  256. return $this->_data['title'];
  257. }
  258. $title = $this->getExtension('Atom')->getTitle();
  259. $this->_data['title'] = $title;
  260. return $this->_data['title'];
  261. }
  262. /**
  263. * Get an array of any supported Pusubhubbub endpoints
  264. *
  265. * @return array|null
  266. */
  267. public function getHubs()
  268. {
  269. if (array_key_exists('hubs', $this->_data)) {
  270. return $this->_data['hubs'];
  271. }
  272. $hubs = $this->getExtension('Atom')->getHubs();
  273. $this->_data['hubs'] = $hubs;
  274. return $this->_data['hubs'];
  275. }
  276. /**
  277. * Get all categories
  278. *
  279. * @return Reader\Collection\Category
  280. */
  281. public function getCategories()
  282. {
  283. if (array_key_exists('categories', $this->_data)) {
  284. return $this->_data['categories'];
  285. }
  286. $categoryCollection = $this->getExtension('Atom')->getCategories();
  287. if (count($categoryCollection) == 0) {
  288. $categoryCollection = $this->getExtension('DublinCore')->getCategories();
  289. }
  290. $this->_data['categories'] = $categoryCollection;
  291. return $this->_data['categories'];
  292. }
  293. /**
  294. * Read all entries to the internal entries array
  295. *
  296. * @return void
  297. */
  298. protected function _indexEntries()
  299. {
  300. if ($this->getType() == Reader\Reader::TYPE_ATOM_10 ||
  301. $this->getType() == Reader\Reader::TYPE_ATOM_03) {
  302. $entries = array();
  303. $entries = $this->_xpath->evaluate('//atom:entry');
  304. foreach($entries as $index=>$entry) {
  305. $this->_entries[$index] = $entry;
  306. }
  307. }
  308. }
  309. /**
  310. * Register the default namespaces for the current feed format
  311. *
  312. */
  313. protected function _registerNamespaces()
  314. {
  315. switch ($this->_data['type']) {
  316. case Reader\Reader::TYPE_ATOM_03:
  317. $this->_xpath->registerNamespace('atom', Reader\Reader::NAMESPACE_ATOM_03);
  318. break;
  319. case Reader\Reader::TYPE_ATOM_10:
  320. default:
  321. $this->_xpath->registerNamespace('atom', Reader\Reader::NAMESPACE_ATOM_10);
  322. }
  323. }
  324. }