PageRenderTime 41ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/www/libs/Zend/Feed/Reader/Feed/Atom.php

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