/libs/Zend/Feed/Reader/Extension/DublinCore/Feed.php

https://github.com/quarkness/piwik · PHP · 309 lines · 167 code · 54 blank · 88 comment · 28 complexity · 5b54ec94d0b145cb7cb328e66fa0fc76 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-2011 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id: Feed.php 23953 2011-05-03 05:47:39Z ralph $
  20. */
  21. /**
  22. * @see Zend_Feed_Reader_Extension_FeedAbstract
  23. */
  24. // require_once 'Zend/Feed/Reader/Extension/FeedAbstract.php';
  25. /**
  26. * @see Zend_Date
  27. */
  28. // require_once 'Zend/Date.php';
  29. /**
  30. * @see Zend_Feed_Reader_Collection_Author
  31. */
  32. // require_once 'Zend/Feed/Reader/Collection/Author.php';
  33. /**
  34. * @category Zend
  35. * @package Zend_Feed_Reader
  36. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  37. * @license http://framework.zend.com/license/new-bsd New BSD License
  38. */
  39. class Zend_Feed_Reader_Extension_DublinCore_Feed
  40. extends Zend_Feed_Reader_Extension_FeedAbstract
  41. {
  42. /**
  43. * Get a single author
  44. *
  45. * @param int $index
  46. * @return string|null
  47. */
  48. public function getAuthor($index = 0)
  49. {
  50. $authors = $this->getAuthors();
  51. if (isset($authors[$index])) {
  52. return $authors[$index];
  53. }
  54. return null;
  55. }
  56. /**
  57. * Get an array with feed authors
  58. *
  59. * @return array
  60. */
  61. public function getAuthors()
  62. {
  63. if (array_key_exists('authors', $this->_data)) {
  64. return $this->_data['authors'];
  65. }
  66. $authors = array();
  67. $list = $this->_xpath->query('//dc11:creator');
  68. if (!$list->length) {
  69. $list = $this->_xpath->query('//dc10:creator');
  70. }
  71. if (!$list->length) {
  72. $list = $this->_xpath->query('//dc11:publisher');
  73. if (!$list->length) {
  74. $list = $this->_xpath->query('//dc10:publisher');
  75. }
  76. }
  77. if ($list->length) {
  78. foreach ($list as $author) {
  79. $authors[] = array(
  80. 'name' => $author->nodeValue
  81. );
  82. }
  83. $authors = new Zend_Feed_Reader_Collection_Author(
  84. Zend_Feed_Reader::arrayUnique($authors)
  85. );
  86. } else {
  87. $authors = null;
  88. }
  89. $this->_data['authors'] = $authors;
  90. return $this->_data['authors'];
  91. }
  92. /**
  93. * Get the copyright entry
  94. *
  95. * @return string|null
  96. */
  97. public function getCopyright()
  98. {
  99. if (array_key_exists('copyright', $this->_data)) {
  100. return $this->_data['copyright'];
  101. }
  102. $copyright = null;
  103. $copyright = $this->_xpath->evaluate('string(' . $this->getXpathPrefix() . '/dc11:rights)');
  104. if (!$copyright) {
  105. $copyright = $this->_xpath->evaluate('string(' . $this->getXpathPrefix() . '/dc10:rights)');
  106. }
  107. if (!$copyright) {
  108. $copyright = null;
  109. }
  110. $this->_data['copyright'] = $copyright;
  111. return $this->_data['copyright'];
  112. }
  113. /**
  114. * Get the feed description
  115. *
  116. * @return string|null
  117. */
  118. public function getDescription()
  119. {
  120. if (array_key_exists('description', $this->_data)) {
  121. return $this->_data['description'];
  122. }
  123. $description = null;
  124. $description = $this->_xpath->evaluate('string(' . $this->getXpathPrefix() . '/dc11:description)');
  125. if (!$description) {
  126. $description = $this->_xpath->evaluate('string(' . $this->getXpathPrefix() . '/dc10:description)');
  127. }
  128. if (!$description) {
  129. $description = null;
  130. }
  131. $this->_data['description'] = $description;
  132. return $this->_data['description'];
  133. }
  134. /**
  135. * Get the feed ID
  136. *
  137. * @return string|null
  138. */
  139. public function getId()
  140. {
  141. if (array_key_exists('id', $this->_data)) {
  142. return $this->_data['id'];
  143. }
  144. $id = null;
  145. $id = $this->_xpath->evaluate('string(' . $this->getXpathPrefix() . '/dc11:identifier)');
  146. if (!$id) {
  147. $id = $this->_xpath->evaluate('string(' . $this->getXpathPrefix() . '/dc10:identifier)');
  148. }
  149. $this->_data['id'] = $id;
  150. return $this->_data['id'];
  151. }
  152. /**
  153. * Get the feed language
  154. *
  155. * @return string|null
  156. */
  157. public function getLanguage()
  158. {
  159. if (array_key_exists('language', $this->_data)) {
  160. return $this->_data['language'];
  161. }
  162. $language = null;
  163. $language = $this->_xpath->evaluate('string(' . $this->getXpathPrefix() . '/dc11:language)');
  164. if (!$language) {
  165. $language = $this->_xpath->evaluate('string(' . $this->getXpathPrefix() . '/dc10:language)');
  166. }
  167. if (!$language) {
  168. $language = null;
  169. }
  170. $this->_data['language'] = $language;
  171. return $this->_data['language'];
  172. }
  173. /**
  174. * Get the feed title
  175. *
  176. * @return string|null
  177. */
  178. public function getTitle()
  179. {
  180. if (array_key_exists('title', $this->_data)) {
  181. return $this->_data['title'];
  182. }
  183. $title = null;
  184. $title = $this->_xpath->evaluate('string(' . $this->getXpathPrefix() . '/dc11:title)');
  185. if (!$title) {
  186. $title = $this->_xpath->evaluate('string(' . $this->getXpathPrefix() . '/dc10:title)');
  187. }
  188. if (!$title) {
  189. $title = null;
  190. }
  191. $this->_data['title'] = $title;
  192. return $this->_data['title'];
  193. }
  194. /**
  195. *
  196. *
  197. * @return Zend_Date|null
  198. */
  199. public function getDate()
  200. {
  201. if (array_key_exists('date', $this->_data)) {
  202. return $this->_data['date'];
  203. }
  204. $d = null;
  205. $date = $this->_xpath->evaluate('string(' . $this->getXpathPrefix() . '/dc11:date)');
  206. if (!$date) {
  207. $date = $this->_xpath->evaluate('string(' . $this->getXpathPrefix() . '/dc10:date)');
  208. }
  209. if ($date) {
  210. $d = new Zend_Date;
  211. $d->set($date, Zend_Date::ISO_8601);
  212. }
  213. $this->_data['date'] = $d;
  214. return $this->_data['date'];
  215. }
  216. /**
  217. * Get categories (subjects under DC)
  218. *
  219. * @return Zend_Feed_Reader_Collection_Category
  220. */
  221. public function getCategories()
  222. {
  223. if (array_key_exists('categories', $this->_data)) {
  224. return $this->_data['categories'];
  225. }
  226. $list = $this->_xpath->evaluate($this->getXpathPrefix() . '//dc11:subject');
  227. if (!$list->length) {
  228. $list = $this->_xpath->evaluate($this->getXpathPrefix() . '//dc10:subject');
  229. }
  230. if ($list->length) {
  231. $categoryCollection = new Zend_Feed_Reader_Collection_Category;
  232. foreach ($list as $category) {
  233. $categoryCollection[] = array(
  234. 'term' => $category->nodeValue,
  235. 'scheme' => null,
  236. 'label' => $category->nodeValue,
  237. );
  238. }
  239. } else {
  240. $categoryCollection = new Zend_Feed_Reader_Collection_Category;
  241. }
  242. $this->_data['categories'] = $categoryCollection;
  243. return $this->_data['categories'];
  244. }
  245. /**
  246. * Register the default namespaces for the current feed format
  247. *
  248. * @return void
  249. */
  250. protected function _registerNamespaces()
  251. {
  252. $this->_xpath->registerNamespace('dc10', 'http://purl.org/dc/elements/1.0/');
  253. $this->_xpath->registerNamespace('dc11', 'http://purl.org/dc/elements/1.1/');
  254. }
  255. }