PageRenderTime 48ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

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

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