Atom.php 12 KB

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