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

/monica/vendor/zendframework/zendframework/library/Zend/Feed/Writer/Renderer/Entry/Rss.php

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