/vendor/zendframework/zend-feed/src/Reader/Feed/Atom.php

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