Atom.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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-2009 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id$
  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-2009 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_Atom
  32. extends Zend_Feed_Writer_Renderer_RendererAbstract
  33. implements Zend_Feed_Writer_Renderer_RendererInterface
  34. {
  35. public function __construct (Zend_Feed_Writer_Entry $container)
  36. {
  37. parent::__construct($container);
  38. }
  39. public function render()
  40. {
  41. $this->_dom = new DOMDocument('1.0', $this->_container->getEncoding());
  42. $this->_dom->formatOutput = true;
  43. $entry = $this->_dom->createElementNS(Zend_Feed_Writer::NAMESPACE_ATOM_10, 'entry');
  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->_setContent($this->_dom, $entry);
  54. foreach ($this->_extensions as $ext) {
  55. $ext->setType($this->getType());
  56. $ext->setRootElement($this->getRootElement());
  57. $ext->setDomDocument($this->getDomDocument(), $entry);
  58. $ext->render();
  59. }
  60. return $this;
  61. }
  62. protected function _setTitle(DOMDocument $dom, DOMElement $root)
  63. {
  64. if(!$this->getDataContainer()->getTitle()) {
  65. require_once 'Zend/Feed/Exception.php';
  66. $message = 'Atom 1.0 entry elements MUST contain exactly one'
  67. . ' atom:title element but a title has not been set';
  68. $exception = new Zend_Feed_Exception($message);
  69. if (!$this->_ignoreExceptions) {
  70. throw $exception;
  71. } else {
  72. $this->_exceptions[] = $exception;
  73. return;
  74. }
  75. }
  76. $title = $dom->createElement('title');
  77. $root->appendChild($title);
  78. $title->setAttribute('type', 'html');
  79. $cdata = $dom->createCDATASection($this->getDataContainer()->getTitle());
  80. $title->appendChild($cdata);
  81. }
  82. protected function _setDescription(DOMDocument $dom, DOMElement $root)
  83. {
  84. if(!$this->getDataContainer()->getDescription()) {
  85. return; // unless src content or base64
  86. }
  87. $subtitle = $dom->createElement('summary');
  88. $root->appendChild($subtitle);
  89. $subtitle->setAttribute('type', 'html');
  90. $cdata = $dom->createCDATASection(
  91. $this->getDataContainer()->getDescription()
  92. );
  93. $subtitle->appendChild($cdata);
  94. }
  95. protected function _setDateModified(DOMDocument $dom, DOMElement $root)
  96. {
  97. if(!$this->getDataContainer()->getDateModified()) {
  98. require_once 'Zend/Feed/Exception.php';
  99. $message = 'Atom 1.0 entry elements MUST contain exactly one'
  100. . ' atom:updated element but a modification date has not been set';
  101. $exception = new Zend_Feed_Exception($message);
  102. if (!$this->_ignoreExceptions) {
  103. throw $exception;
  104. } else {
  105. $this->_exceptions[] = $exception;
  106. return;
  107. }
  108. }
  109. $updated = $dom->createElement('updated');
  110. $root->appendChild($updated);
  111. $updated->nodeValue = $this->getDataContainer()->getDateModified()
  112. ->get(Zend_Date::ISO_8601);
  113. }
  114. protected function _setDateCreated(DOMDocument $dom, DOMElement $root)
  115. {
  116. if (!$this->getDataContainer()->getDateCreated()) {
  117. return;
  118. }
  119. $updated = $dom->createElement('published');
  120. $root->appendChild($updated);
  121. $updated->nodeValue = $this->getDataContainer()->getDateCreated()
  122. ->get(Zend_Date::ISO_8601);
  123. }
  124. protected function _setAuthors(DOMDocument $dom, DOMElement $root)
  125. {
  126. $authors = $this->_container->getAuthors();
  127. if ((!$authors || empty($authors))) {
  128. /**
  129. * This will actually trigger an Exception at the feed level if
  130. * a feed level author is not set.
  131. */
  132. return;
  133. }
  134. foreach ($authors as $data) {
  135. $author = $this->_dom->createElement('author');
  136. $name = $this->_dom->createElement('name');
  137. $author->appendChild($name);
  138. $root->appendChild($author);
  139. $name->nodeValue = $data['name'];
  140. if (array_key_exists('email', $data)) {
  141. $email = $this->_dom->createElement('email');
  142. $author->appendChild($email);
  143. $email->nodeValue = $data['email'];
  144. }
  145. if (array_key_exists('uri', $data)) {
  146. $uri = $this->_dom->createElement('uri');
  147. $author->appendChild($uri);
  148. $uri->nodeValue = $data['uri'];
  149. }
  150. }
  151. }
  152. protected function _setEnclosure(DOMDocument $dom, DOMElement $root)
  153. {
  154. $data = $this->_container->getEnclosure();
  155. if ((!$data || empty($data))) {
  156. return;
  157. }
  158. $enclosure = $this->_dom->createElement('link');
  159. $enclosure->setAttribute('rel', 'enclosure');
  160. $enclosure->setAttribute('type', $data['type']);
  161. $enclosure->setAttribute('length', $data['length']);
  162. $enclosure->setAttribute('href', $data['uri']);
  163. $root->appendChild($enclosure);
  164. }
  165. protected function _setLink(DOMDocument $dom, DOMElement $root)
  166. {
  167. if(!$this->getDataContainer()->getLink()) {
  168. return;
  169. }
  170. $link = $dom->createElement('link');
  171. $root->appendChild($link);
  172. $link->setAttribute('rel', 'alternate');
  173. $link->setAttribute('type', 'text/html');
  174. $link->setAttribute('href', $this->getDataContainer()->getLink());
  175. }
  176. protected function _setId(DOMDocument $dom, DOMElement $root)
  177. {
  178. if(!$this->getDataContainer()->getId()
  179. && !$this->getDataContainer()->getLink()) {
  180. require_once 'Zend/Feed/Exception.php';
  181. $message = 'Atom 1.0 entry elements MUST contain exactly one '
  182. . 'atom:id element, or as an alternative, we can use the same '
  183. . 'value as atom:link however neither a suitable link nor an '
  184. . 'id have been set';
  185. $exception = new Zend_Feed_Exception($message);
  186. if (!$this->_ignoreExceptions) {
  187. throw $exception;
  188. } else {
  189. $this->_exceptions[] = $exception;
  190. return;
  191. }
  192. }
  193. if (!$this->getDataContainer()->getId()) {
  194. $this->getDataContainer()->setId(
  195. $this->getDataContainer()->getLink());
  196. }
  197. if (!Zend_Uri::check($this->getDataContainer()->getId()) &&
  198. !preg_match("#^urn:[a-zA-Z0-9][a-zA-Z0-9\-]{1,31}:([a-zA-Z0-9\(\)\+\,\.\:\=\@\;\$\_\!\*\-]|%[0-9a-fA-F]{2})*#", $this->getDataContainer()->getId())) {
  199. require_once 'Zend/Feed/Exception.php';
  200. throw new Zend_Feed_Exception('Atom 1.0 IDs must be a valid URI/IRI');
  201. }
  202. $id = $dom->createElement('id');
  203. $root->appendChild($id);
  204. $id->nodeValue = $this->getDataContainer()->getId();
  205. }
  206. protected function _setContent(DOMDocument $dom, DOMElement $root)
  207. {
  208. $content = $this->getDataContainer()->getContent();
  209. if (!$content && !$this->getDataContainer()->getLink()) {
  210. require_once 'Zend/Feed/Exception.php';
  211. $message = 'Atom 1.0 entry elements MUST contain exactly one '
  212. . 'atom:content element, or as an alternative, at least one link '
  213. . 'with a rel attribute of "alternate" to indicate an alternate '
  214. . 'method to consume the content.';
  215. $exception = new Zend_Feed_Exception($message);
  216. if (!$this->_ignoreExceptions) {
  217. throw $exception;
  218. } else {
  219. $this->_exceptions[] = $exception;
  220. return;
  221. }
  222. }
  223. $element = $dom->createElement('content');
  224. $element->setAttribute('type', 'html');
  225. $cdata = $dom->createCDATASection($content);
  226. $element->appendChild($cdata);
  227. $root->appendChild($element);
  228. }
  229. }