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

https://github.com/zucchi/zf2 · PHP · 375 lines · 184 code · 73 blank · 118 comment · 20 complexity · 02005574a6ef8810ae6c3195b68851cc 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\Entry;
  11. use DOMElement;
  12. use DOMXPath;
  13. use Zend\Feed\Reader;
  14. /**
  15. * @category Zend
  16. * @package Zend_Feed_Reader
  17. */
  18. class Atom extends AbstractEntry implements EntryInterface
  19. {
  20. /**
  21. * XPath query
  22. *
  23. * @var string
  24. */
  25. protected $xpathQuery = '';
  26. /**
  27. * Constructor
  28. *
  29. * @param DOMElement $entry
  30. * @param int $entryKey
  31. * @param string $type
  32. */
  33. public function __construct(DOMElement $entry, $entryKey, $type = null)
  34. {
  35. parent::__construct($entry, $entryKey, $type);
  36. // Everyone by now should know XPath indices start from 1 not 0
  37. $this->xpathQuery = '//atom:entry[' . ($this->entryKey + 1) . ']';
  38. $manager = Reader\Reader::getExtensionManager();
  39. $extensions = array('Atom\Entry', 'Thread\Entry', 'DublinCore\Entry');
  40. foreach ($extensions as $name) {
  41. $extension = $manager->get($name);
  42. $extension->setEntryElement($entry);
  43. $extension->setEntryKey($entryKey);
  44. $extension->setType($type);
  45. $this->extensions[$name] = $extension;
  46. }
  47. }
  48. /**
  49. * Get the specified author
  50. *
  51. * @param int $index
  52. * @return string|null
  53. */
  54. public function getAuthor($index = 0)
  55. {
  56. $authors = $this->getAuthors();
  57. if (isset($authors[$index])) {
  58. return $authors[$index];
  59. }
  60. return null;
  61. }
  62. /**
  63. * Get an array with feed authors
  64. *
  65. * @return array
  66. */
  67. public function getAuthors()
  68. {
  69. if (array_key_exists('authors', $this->data)) {
  70. return $this->data['authors'];
  71. }
  72. $people = $this->getExtension('Atom')->getAuthors();
  73. $this->data['authors'] = $people;
  74. return $this->data['authors'];
  75. }
  76. /**
  77. * Get the entry content
  78. *
  79. * @return string
  80. */
  81. public function getContent()
  82. {
  83. if (array_key_exists('content', $this->data)) {
  84. return $this->data['content'];
  85. }
  86. $content = $this->getExtension('Atom')->getContent();
  87. $this->data['content'] = $content;
  88. return $this->data['content'];
  89. }
  90. /**
  91. * Get the entry creation date
  92. *
  93. * @return string
  94. */
  95. public function getDateCreated()
  96. {
  97. if (array_key_exists('datecreated', $this->data)) {
  98. return $this->data['datecreated'];
  99. }
  100. $dateCreated = $this->getExtension('Atom')->getDateCreated();
  101. $this->data['datecreated'] = $dateCreated;
  102. return $this->data['datecreated'];
  103. }
  104. /**
  105. * Get the entry modification date
  106. *
  107. * @return string
  108. */
  109. public function getDateModified()
  110. {
  111. if (array_key_exists('datemodified', $this->data)) {
  112. return $this->data['datemodified'];
  113. }
  114. $dateModified = $this->getExtension('Atom')->getDateModified();
  115. $this->data['datemodified'] = $dateModified;
  116. return $this->data['datemodified'];
  117. }
  118. /**
  119. * Get the entry description
  120. *
  121. * @return string
  122. */
  123. public function getDescription()
  124. {
  125. if (array_key_exists('description', $this->data)) {
  126. return $this->data['description'];
  127. }
  128. $description = $this->getExtension('Atom')->getDescription();
  129. $this->data['description'] = $description;
  130. return $this->data['description'];
  131. }
  132. /**
  133. * Get the entry enclosure
  134. *
  135. * @return string
  136. */
  137. public function getEnclosure()
  138. {
  139. if (array_key_exists('enclosure', $this->data)) {
  140. return $this->data['enclosure'];
  141. }
  142. $enclosure = $this->getExtension('Atom')->getEnclosure();
  143. $this->data['enclosure'] = $enclosure;
  144. return $this->data['enclosure'];
  145. }
  146. /**
  147. * Get the entry ID
  148. *
  149. * @return string
  150. */
  151. public function getId()
  152. {
  153. if (array_key_exists('id', $this->data)) {
  154. return $this->data['id'];
  155. }
  156. $id = $this->getExtension('Atom')->getId();
  157. $this->data['id'] = $id;
  158. return $this->data['id'];
  159. }
  160. /**
  161. * Get a specific link
  162. *
  163. * @param int $index
  164. * @return string
  165. */
  166. public function getLink($index = 0)
  167. {
  168. if (!array_key_exists('links', $this->data)) {
  169. $this->getLinks();
  170. }
  171. if (isset($this->data['links'][$index])) {
  172. return $this->data['links'][$index];
  173. }
  174. return null;
  175. }
  176. /**
  177. * Get all links
  178. *
  179. * @return array
  180. */
  181. public function getLinks()
  182. {
  183. if (array_key_exists('links', $this->data)) {
  184. return $this->data['links'];
  185. }
  186. $links = $this->getExtension('Atom')->getLinks();
  187. $this->data['links'] = $links;
  188. return $this->data['links'];
  189. }
  190. /**
  191. * Get a permalink to the entry
  192. *
  193. * @return string
  194. */
  195. public function getPermalink()
  196. {
  197. return $this->getLink(0);
  198. }
  199. /**
  200. * Get the entry title
  201. *
  202. * @return string
  203. */
  204. public function getTitle()
  205. {
  206. if (array_key_exists('title', $this->data)) {
  207. return $this->data['title'];
  208. }
  209. $title = $this->getExtension('Atom')->getTitle();
  210. $this->data['title'] = $title;
  211. return $this->data['title'];
  212. }
  213. /**
  214. * Get the number of comments/replies for current entry
  215. *
  216. * @return integer
  217. */
  218. public function getCommentCount()
  219. {
  220. if (array_key_exists('commentcount', $this->data)) {
  221. return $this->data['commentcount'];
  222. }
  223. $commentcount = $this->getExtension('Thread')->getCommentCount();
  224. if (!$commentcount) {
  225. $commentcount = $this->getExtension('Atom')->getCommentCount();
  226. }
  227. $this->data['commentcount'] = $commentcount;
  228. return $this->data['commentcount'];
  229. }
  230. /**
  231. * Returns a URI pointing to the HTML page where comments can be made on this entry
  232. *
  233. * @return string
  234. */
  235. public function getCommentLink()
  236. {
  237. if (array_key_exists('commentlink', $this->data)) {
  238. return $this->data['commentlink'];
  239. }
  240. $commentlink = $this->getExtension('Atom')->getCommentLink();
  241. $this->data['commentlink'] = $commentlink;
  242. return $this->data['commentlink'];
  243. }
  244. /**
  245. * Returns a URI pointing to a feed of all comments for this entry
  246. *
  247. * @return string
  248. */
  249. public function getCommentFeedLink()
  250. {
  251. if (array_key_exists('commentfeedlink', $this->data)) {
  252. return $this->data['commentfeedlink'];
  253. }
  254. $commentfeedlink = $this->getExtension('Atom')->getCommentFeedLink();
  255. $this->data['commentfeedlink'] = $commentfeedlink;
  256. return $this->data['commentfeedlink'];
  257. }
  258. /**
  259. * Get category data as a Reader\Reader_Collection_Category object
  260. *
  261. * @return Reader\Collection\Category
  262. */
  263. public function getCategories()
  264. {
  265. if (array_key_exists('categories', $this->data)) {
  266. return $this->data['categories'];
  267. }
  268. $categoryCollection = $this->getExtension('Atom')->getCategories();
  269. if (count($categoryCollection) == 0) {
  270. $categoryCollection = $this->getExtension('DublinCore')->getCategories();
  271. }
  272. $this->data['categories'] = $categoryCollection;
  273. return $this->data['categories'];
  274. }
  275. /**
  276. * Get source feed metadata from the entry
  277. *
  278. * @return Reader\Feed\Atom\Source|null
  279. */
  280. public function getSource()
  281. {
  282. if (array_key_exists('source', $this->data)) {
  283. return $this->data['source'];
  284. }
  285. $source = $this->getExtension('Atom')->getSource();
  286. $this->data['source'] = $source;
  287. return $this->data['source'];
  288. }
  289. /**
  290. * Set the XPath query (incl. on all Extensions)
  291. *
  292. * @param DOMXPath $xpath
  293. * @return void
  294. */
  295. public function setXpath(DOMXPath $xpath)
  296. {
  297. parent::setXpath($xpath);
  298. foreach ($this->extensions as $extension) {
  299. $extension->setXpath($this->xpath);
  300. }
  301. }
  302. }