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

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