Atom.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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. /**
  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 atom entry
  47. *
  48. * @return Zend_Feed_Writer_Renderer_Entry_Atom
  49. */
  50. public function render()
  51. {
  52. $this->_dom = new DOMDocument('1.0', $this->_container->getEncoding());
  53. $this->_dom->formatOutput = true;
  54. $entry = $this->_dom->createElementNS(Zend_Feed_Writer::NAMESPACE_ATOM_10, 'entry');
  55. $this->_dom->appendChild($entry);
  56. $this->_setTitle($this->_dom, $entry);
  57. $this->_setDescription($this->_dom, $entry);
  58. $this->_setDateCreated($this->_dom, $entry);
  59. $this->_setDateModified($this->_dom, $entry);
  60. $this->_setLink($this->_dom, $entry);
  61. $this->_setId($this->_dom, $entry);
  62. $this->_setAuthors($this->_dom, $entry);
  63. $this->_setEnclosure($this->_dom, $entry);
  64. $this->_setContent($this->_dom, $entry);
  65. foreach ($this->_extensions as $ext) {
  66. $ext->setType($this->getType());
  67. $ext->setRootElement($this->getRootElement());
  68. $ext->setDomDocument($this->getDomDocument(), $entry);
  69. $ext->render();
  70. }
  71. return $this;
  72. }
  73. /**
  74. * Set entry title
  75. *
  76. * @param DOMDocument $dom
  77. * @param DOMElement $root
  78. * @return void
  79. */
  80. protected function _setTitle(DOMDocument $dom, DOMElement $root)
  81. {
  82. if(!$this->getDataContainer()->getTitle()) {
  83. require_once 'Zend/Feed/Exception.php';
  84. $message = 'Atom 1.0 entry elements MUST contain exactly one'
  85. . ' atom:title element but a title has not been set';
  86. $exception = new Zend_Feed_Exception($message);
  87. if (!$this->_ignoreExceptions) {
  88. throw $exception;
  89. } else {
  90. $this->_exceptions[] = $exception;
  91. return;
  92. }
  93. }
  94. $title = $dom->createElement('title');
  95. $root->appendChild($title);
  96. $title->setAttribute('type', 'html');
  97. $cdata = $dom->createCDATASection($this->getDataContainer()->getTitle());
  98. $title->appendChild($cdata);
  99. }
  100. /**
  101. * Set entry description
  102. *
  103. * @param DOMDocument $dom
  104. * @param DOMElement $root
  105. * @return void
  106. */
  107. protected function _setDescription(DOMDocument $dom, DOMElement $root)
  108. {
  109. if(!$this->getDataContainer()->getDescription()) {
  110. return; // unless src content or base64
  111. }
  112. $subtitle = $dom->createElement('summary');
  113. $root->appendChild($subtitle);
  114. $subtitle->setAttribute('type', 'html');
  115. $cdata = $dom->createCDATASection(
  116. $this->getDataContainer()->getDescription()
  117. );
  118. $subtitle->appendChild($cdata);
  119. }
  120. /**
  121. * Set date entry was modified
  122. *
  123. * @param DOMDocument $dom
  124. * @param DOMElement $root
  125. * @return void
  126. */
  127. protected function _setDateModified(DOMDocument $dom, DOMElement $root)
  128. {
  129. if(!$this->getDataContainer()->getDateModified()) {
  130. require_once 'Zend/Feed/Exception.php';
  131. $message = 'Atom 1.0 entry elements MUST contain exactly one'
  132. . ' atom:updated element but a modification date has not been set';
  133. $exception = new Zend_Feed_Exception($message);
  134. if (!$this->_ignoreExceptions) {
  135. throw $exception;
  136. } else {
  137. $this->_exceptions[] = $exception;
  138. return;
  139. }
  140. }
  141. $updated = $dom->createElement('updated');
  142. $root->appendChild($updated);
  143. $updated->nodeValue = $this->getDataContainer()->getDateModified()
  144. ->get(Zend_Date::ISO_8601);
  145. }
  146. /**
  147. * Set date entry was created
  148. *
  149. * @param DOMDocument $dom
  150. * @param DOMElement $root
  151. * @return void
  152. */
  153. protected function _setDateCreated(DOMDocument $dom, DOMElement $root)
  154. {
  155. if (!$this->getDataContainer()->getDateCreated()) {
  156. return;
  157. }
  158. $updated = $dom->createElement('published');
  159. $root->appendChild($updated);
  160. $updated->nodeValue = $this->getDataContainer()->getDateCreated()
  161. ->get(Zend_Date::ISO_8601);
  162. }
  163. /**
  164. * Set entry authors
  165. *
  166. * @param DOMDocument $dom
  167. * @param DOMElement $root
  168. * @return void
  169. */
  170. protected function _setAuthors(DOMDocument $dom, DOMElement $root)
  171. {
  172. $authors = $this->_container->getAuthors();
  173. if ((!$authors || empty($authors))) {
  174. /**
  175. * This will actually trigger an Exception at the feed level if
  176. * a feed level author is not set.
  177. */
  178. return;
  179. }
  180. foreach ($authors as $data) {
  181. $author = $this->_dom->createElement('author');
  182. $name = $this->_dom->createElement('name');
  183. $author->appendChild($name);
  184. $root->appendChild($author);
  185. $name->nodeValue = $data['name'];
  186. if (array_key_exists('email', $data)) {
  187. $email = $this->_dom->createElement('email');
  188. $author->appendChild($email);
  189. $email->nodeValue = $data['email'];
  190. }
  191. if (array_key_exists('uri', $data)) {
  192. $uri = $this->_dom->createElement('uri');
  193. $author->appendChild($uri);
  194. $uri->nodeValue = $data['uri'];
  195. }
  196. }
  197. }
  198. /**
  199. * Set entry enclosure
  200. *
  201. * @param DOMDocument $dom
  202. * @param DOMElement $root
  203. * @return void
  204. */
  205. protected function _setEnclosure(DOMDocument $dom, DOMElement $root)
  206. {
  207. $data = $this->_container->getEnclosure();
  208. if ((!$data || empty($data))) {
  209. return;
  210. }
  211. $enclosure = $this->_dom->createElement('link');
  212. $enclosure->setAttribute('rel', 'enclosure');
  213. $enclosure->setAttribute('type', $data['type']);
  214. $enclosure->setAttribute('length', $data['length']);
  215. $enclosure->setAttribute('href', $data['uri']);
  216. $root->appendChild($enclosure);
  217. }
  218. protected function _setLink(DOMDocument $dom, DOMElement $root)
  219. {
  220. if(!$this->getDataContainer()->getLink()) {
  221. return;
  222. }
  223. $link = $dom->createElement('link');
  224. $root->appendChild($link);
  225. $link->setAttribute('rel', 'alternate');
  226. $link->setAttribute('type', 'text/html');
  227. $link->setAttribute('href', $this->getDataContainer()->getLink());
  228. }
  229. /**
  230. * Set entry identifier
  231. *
  232. * @param DOMDocument $dom
  233. * @param DOMElement $root
  234. * @return void
  235. */
  236. protected function _setId(DOMDocument $dom, DOMElement $root)
  237. {
  238. if(!$this->getDataContainer()->getId()
  239. && !$this->getDataContainer()->getLink()) {
  240. require_once 'Zend/Feed/Exception.php';
  241. $message = 'Atom 1.0 entry elements MUST contain exactly one '
  242. . 'atom:id element, or as an alternative, we can use the same '
  243. . 'value as atom:link however neither a suitable link nor an '
  244. . 'id have been set';
  245. $exception = new Zend_Feed_Exception($message);
  246. if (!$this->_ignoreExceptions) {
  247. throw $exception;
  248. } else {
  249. $this->_exceptions[] = $exception;
  250. return;
  251. }
  252. }
  253. if (!$this->getDataContainer()->getId()) {
  254. $this->getDataContainer()->setId(
  255. $this->getDataContainer()->getLink());
  256. }
  257. if (!Zend_Uri::check($this->getDataContainer()->getId()) &&
  258. !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())) {
  259. require_once 'Zend/Feed/Exception.php';
  260. throw new Zend_Feed_Exception('Atom 1.0 IDs must be a valid URI/IRI');
  261. }
  262. $id = $dom->createElement('id');
  263. $root->appendChild($id);
  264. $id->nodeValue = $this->getDataContainer()->getId();
  265. }
  266. /**
  267. * Set entry content
  268. *
  269. * @param DOMDocument $dom
  270. * @param DOMElement $root
  271. * @return void
  272. */
  273. protected function _setContent(DOMDocument $dom, DOMElement $root)
  274. {
  275. $content = $this->getDataContainer()->getContent();
  276. if (!$content && !$this->getDataContainer()->getLink()) {
  277. require_once 'Zend/Feed/Exception.php';
  278. $message = 'Atom 1.0 entry elements MUST contain exactly one '
  279. . 'atom:content element, or as an alternative, at least one link '
  280. . 'with a rel attribute of "alternate" to indicate an alternate '
  281. . 'method to consume the content.';
  282. $exception = new Zend_Feed_Exception($message);
  283. if (!$this->_ignoreExceptions) {
  284. throw $exception;
  285. } else {
  286. $this->_exceptions[] = $exception;
  287. return;
  288. }
  289. }
  290. $element = $dom->createElement('content');
  291. $element->setAttribute('type', 'html');
  292. $cdata = $dom->createCDATASection($content);
  293. $element->appendChild($cdata);
  294. $root->appendChild($element);
  295. }
  296. }