/library/Zend/Feed/Writer/Renderer/Entry/Rss.php

https://github.com/taste/zf2 · PHP · 347 lines · 217 code · 17 blank · 113 comment · 35 complexity · b15459b048ddc51fe1f62d43034f785f 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_Writer
  17. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. */
  20. /**
  21. * @namespace
  22. */
  23. namespace Zend\Feed\Writer\Renderer\Entry;
  24. use Zend\Feed\Writer\Renderer;
  25. use Zend\Feed\Writer;
  26. use Zend\Date;
  27. use Zend\URI;
  28. /**
  29. * @uses \Zend\Date\Date
  30. * @uses \Zend\Feed\Exception
  31. * @uses \Zend\Feed\Writer\Renderer\RendererAbstract
  32. * @uses \Zend\Feed\Writer\Renderer\RendererInterface
  33. * @uses \Zend\Uri\Uri
  34. * @category Zend
  35. * @package Zend_Feed_Writer
  36. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  37. * @license http://framework.zend.com/license/new-bsd New BSD License
  38. */
  39. class Rss extends Renderer\AbstractRenderer implements Renderer\Renderer
  40. {
  41. /**
  42. * Constructor
  43. *
  44. * @param Zend_Feed_Writer_Entry $container
  45. * @return void
  46. */
  47. public function __construct (Writer\Entry $container)
  48. {
  49. parent::__construct($container);
  50. }
  51. /**
  52. * Render RSS entry
  53. *
  54. * @return Zend_Feed_Writer_Renderer_Entry_Rss
  55. */
  56. public function render()
  57. {
  58. $this->_dom = new \DOMDocument('1.0', $this->_container->getEncoding());
  59. $this->_dom->formatOutput = true;
  60. $this->_dom->substituteEntities = false;
  61. $entry = $this->_dom->createElement('item');
  62. $this->_dom->appendChild($entry);
  63. $this->_setTitle($this->_dom, $entry);
  64. $this->_setDescription($this->_dom, $entry);
  65. $this->_setDateCreated($this->_dom, $entry);
  66. $this->_setDateModified($this->_dom, $entry);
  67. $this->_setLink($this->_dom, $entry);
  68. $this->_setId($this->_dom, $entry);
  69. $this->_setAuthors($this->_dom, $entry);
  70. $this->_setEnclosure($this->_dom, $entry);
  71. $this->_setCommentLink($this->_dom, $entry);
  72. $this->_setCategories($this->_dom, $entry);
  73. foreach ($this->_extensions as $ext) {
  74. $ext->setType($this->getType());
  75. $ext->setRootElement($this->getRootElement());
  76. $ext->setDOMDocument($this->getDOMDocument(), $entry);
  77. $ext->render();
  78. }
  79. return $this;
  80. }
  81. /**
  82. * Set entry title
  83. *
  84. * @param \DOMDocument $dom
  85. * @param \DOMElement $root
  86. * @return void
  87. */
  88. protected function _setTitle(\DOMDocument $dom, \DOMElement $root)
  89. {
  90. if(!$this->getDataContainer()->getDescription()
  91. && !$this->getDataContainer()->getTitle()) {
  92. $message = 'RSS 2.0 entry elements SHOULD contain exactly one'
  93. . ' title element but a title has not been set. In addition, there'
  94. . ' is no description as required in the absence of a title.';
  95. $exception = new Writer\Exception($message);
  96. if (!$this->_ignoreExceptions) {
  97. throw $exception;
  98. } else {
  99. $this->_exceptions[] = $exception;
  100. return;
  101. }
  102. }
  103. $title = $dom->createElement('title');
  104. $root->appendChild($title);
  105. $text = $dom->createTextNode($this->getDataContainer()->getTitle());
  106. $title->appendChild($text);
  107. }
  108. /**
  109. * Set entry description
  110. *
  111. * @param \DOMDocument $dom
  112. * @param \DOMElement $root
  113. * @return void
  114. */
  115. protected function _setDescription(\DOMDocument $dom, \DOMElement $root)
  116. {
  117. if(!$this->getDataContainer()->getDescription()
  118. && !$this->getDataContainer()->getTitle()) {
  119. $message = 'RSS 2.0 entry elements SHOULD contain exactly one'
  120. . ' description element but a description has not been set. In'
  121. . ' addition, there is no title element as required in the absence'
  122. . ' of a description.';
  123. $exception = new Writer\Exception($message);
  124. if (!$this->_ignoreExceptions) {
  125. throw $exception;
  126. } else {
  127. $this->_exceptions[] = $exception;
  128. return;
  129. }
  130. }
  131. if (!$this->getDataContainer()->getDescription()) {
  132. return;
  133. }
  134. $subtitle = $dom->createElement('description');
  135. $root->appendChild($subtitle);
  136. $text = $dom->createCDATASection($this->getDataContainer()->getDescription());
  137. $subtitle->appendChild($text);
  138. }
  139. /**
  140. * Set date entry was last modified
  141. *
  142. * @param \DOMDocument $dom
  143. * @param \DOMElement $root
  144. * @return void
  145. */
  146. protected function _setDateModified(\DOMDocument $dom, \DOMElement $root)
  147. {
  148. if(!$this->getDataContainer()->getDateModified()) {
  149. return;
  150. }
  151. $updated = $dom->createElement('pubDate');
  152. $root->appendChild($updated);
  153. $text = $dom->createTextNode(
  154. $this->getDataContainer()->getDateModified()->get(Date\Date::RSS)
  155. );
  156. $updated->appendChild($text);
  157. }
  158. /**
  159. * Set date entry was created
  160. *
  161. * @param \DOMDocument $dom
  162. * @param \DOMElement $root
  163. * @return void
  164. */
  165. protected function _setDateCreated(\DOMDocument $dom, \DOMElement $root)
  166. {
  167. if (!$this->getDataContainer()->getDateCreated()) {
  168. return;
  169. }
  170. if (!$this->getDataContainer()->getDateModified()) {
  171. $this->getDataContainer()->setDateModified(
  172. $this->getDataContainer()->getDateCreated()
  173. );
  174. }
  175. }
  176. /**
  177. * Set entry authors
  178. *
  179. * @param \DOMDocument $dom
  180. * @param \DOMElement $root
  181. * @return void
  182. */
  183. protected function _setAuthors(\DOMDocument $dom, \DOMElement $root)
  184. {
  185. $authors = $this->_container->getAuthors();
  186. if ((!$authors || empty($authors))) {
  187. return;
  188. }
  189. foreach ($authors as $data) {
  190. $author = $this->_dom->createElement('author');
  191. $name = $data['name'];
  192. if (array_key_exists('email', $data)) {
  193. $name = $data['email'] . ' (' . $data['name'] . ')';
  194. }
  195. $text = $dom->createTextNode($name);
  196. $author->appendChild($text);
  197. $root->appendChild($author);
  198. }
  199. }
  200. /**
  201. * Set entry enclosure
  202. *
  203. * @param \DOMDocument $dom
  204. * @param \DOMElement $root
  205. * @return void
  206. */
  207. protected function _setEnclosure(\DOMDocument $dom, \DOMElement $root)
  208. {
  209. $data = $this->_container->getEnclosure();
  210. if ((!$data || empty($data))) {
  211. return;
  212. }
  213. if (!isset($data['type'])) {
  214. $exception = new Writer\Exception('Enclosure "type" is not set');
  215. if (!$this->_ignoreExceptions) {
  216. throw $exception;
  217. } else {
  218. $this->_exceptions[] = $exception;
  219. return;
  220. }
  221. }
  222. if (!isset($data['length'])) {
  223. $exception = new Writer\Exception('Enclosure "length" is not set');
  224. if (!$this->_ignoreExceptions) {
  225. throw $exception;
  226. } else {
  227. $this->_exceptions[] = $exception;
  228. return;
  229. }
  230. }
  231. if (isset($data['length']) && (int) $data['length'] <= 0) {
  232. $exception = new Writer\Exception('Enclosure "length" must be an integer'
  233. . ' indicating the content\'s length in bytes');
  234. if (!$this->_ignoreExceptions) {
  235. throw $exception;
  236. } else {
  237. $this->_exceptions[] = $exception;
  238. return;
  239. }
  240. }
  241. $enclosure = $this->_dom->createElement('enclosure');
  242. $enclosure->setAttribute('type', $data['type']);
  243. $enclosure->setAttribute('length', $data['length']);
  244. $enclosure->setAttribute('url', $data['uri']);
  245. $root->appendChild($enclosure);
  246. }
  247. /**
  248. * Set link to entry
  249. *
  250. * @param \DOMDocument $dom
  251. * @param \DOMElement $root
  252. * @return void
  253. */
  254. protected function _setLink(\DOMDocument $dom, \DOMElement $root)
  255. {
  256. if(!$this->getDataContainer()->getLink()) {
  257. return;
  258. }
  259. $link = $dom->createElement('link');
  260. $root->appendChild($link);
  261. $text = $dom->createTextNode($this->getDataContainer()->getLink());
  262. $link->appendChild($text);
  263. }
  264. /**
  265. * Set entry identifier
  266. *
  267. * @param \DOMDocument $dom
  268. * @param \DOMElement $root
  269. * @return void
  270. */
  271. protected function _setId(\DOMDocument $dom, \DOMElement $root)
  272. {
  273. if(!$this->getDataContainer()->getId()
  274. && !$this->getDataContainer()->getLink()) {
  275. return;
  276. }
  277. $id = $dom->createElement('guid');
  278. $root->appendChild($id);
  279. if (!$this->getDataContainer()->getId()) {
  280. $this->getDataContainer()->setId(
  281. $this->getDataContainer()->getLink());
  282. }
  283. $text = $dom->createTextNode($this->getDataContainer()->getId());
  284. $id->appendChild($text);
  285. if (!Uri\Url::validate($this->getDataContainer()->getId())) {
  286. $id->setAttribute('isPermaLink', 'false');
  287. }
  288. }
  289. /**
  290. * Set link to entry comments
  291. *
  292. * @param \DOMDocument $dom
  293. * @param \DOMElement $root
  294. * @return void
  295. */
  296. protected function _setCommentLink(\DOMDocument $dom, \DOMElement $root)
  297. {
  298. $link = $this->getDataContainer()->getCommentLink();
  299. if (!$link) {
  300. return;
  301. }
  302. $clink = $this->_dom->createElement('comments');
  303. $text = $dom->createTextNode($link);
  304. $clink->appendChild($text);
  305. $root->appendChild($clink);
  306. }
  307. /**
  308. * Set entry categories
  309. *
  310. * @param \DOMDocument $dom
  311. * @param \DOMElement $root
  312. * @return void
  313. */
  314. protected function _setCategories(\DOMDocument $dom, \DOMElement $root)
  315. {
  316. $categories = $this->getDataContainer()->getCategories();
  317. if (!$categories) {
  318. return;
  319. }
  320. foreach ($categories as $cat) {
  321. $category = $dom->createElement('category');
  322. if (isset($cat['scheme'])) {
  323. $category->setAttribute('domain', $cat['scheme']);
  324. }
  325. $text = $dom->createCDATASection($cat['term']);
  326. $category->appendChild($text);
  327. $root->appendChild($category);
  328. }
  329. }
  330. }