Feed.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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_Extension_RendererAbstract
  23. */
  24. require_once 'Zend/Feed/Writer/Extension/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_Extension_ITunes_Renderer_Feed
  32. extends Zend_Feed_Writer_Extension_RendererAbstract
  33. {
  34. /**
  35. * Render feed
  36. *
  37. * @return void
  38. */
  39. public function render()
  40. {
  41. $this->_appendNamespaces();
  42. $this->_setAuthors($this->_dom, $this->_base);
  43. $this->_setBlock($this->_dom, $this->_base);
  44. $this->_setCategories($this->_dom, $this->_base);
  45. $this->_setImage($this->_dom, $this->_base);
  46. $this->_setDuration($this->_dom, $this->_base);
  47. $this->_setExplicit($this->_dom, $this->_base);
  48. $this->_setKeywords($this->_dom, $this->_base);
  49. $this->_setNewFeedUrl($this->_dom, $this->_base);
  50. $this->_setOwners($this->_dom, $this->_base);
  51. $this->_setSubtitle($this->_dom, $this->_base);
  52. $this->_setSummary($this->_dom, $this->_base);
  53. }
  54. /**
  55. * Append feed namespaces
  56. *
  57. * @return void
  58. */
  59. protected function _appendNamespaces()
  60. {
  61. $this->getRootElement()->setAttribute('xmlns:itunes',
  62. 'http://www.itunes.com/dtds/podcast-1.0.dtd');
  63. }
  64. /**
  65. * Set feed authors
  66. *
  67. * @param DOMDocument $dom
  68. * @param DOMElement $root
  69. * @return void
  70. */
  71. protected function _setAuthors(DOMDocument $dom, DOMElement $root)
  72. {
  73. $authors = $this->getDataContainer()->getItunesAuthors();
  74. if (!$authors || empty($authors)) {
  75. return;
  76. }
  77. foreach ($authors as $author) {
  78. $el = $dom->createElement('itunes:author');
  79. $text = $dom->createTextNode($author);
  80. $el->appendChild($text);
  81. $root->appendChild($el);
  82. }
  83. }
  84. /**
  85. * Set feed itunes block
  86. *
  87. * @param DOMDocument $dom
  88. * @param DOMElement $root
  89. * @return void
  90. */
  91. protected function _setBlock(DOMDocument $dom, DOMElement $root)
  92. {
  93. $block = $this->getDataContainer()->getItunesBlock();
  94. if (is_null($block)) {
  95. return;
  96. }
  97. $el = $dom->createElement('itunes:block');
  98. $text = $dom->createTextNode($block);
  99. $el->appendChild($text);
  100. $root->appendChild($el);
  101. }
  102. /**
  103. * Set feed categories
  104. *
  105. * @param DOMDocument $dom
  106. * @param DOMElement $root
  107. * @return void
  108. */
  109. protected function _setCategories(DOMDocument $dom, DOMElement $root)
  110. {
  111. $cats = $this->getDataContainer()->getItunesCategories();
  112. if (!$cats || empty($cats)) {
  113. return;
  114. }
  115. foreach ($cats as $key=>$cat) {
  116. if (!is_array($cat)) {
  117. $el = $dom->createElement('itunes:category');
  118. $el->setAttribute('text', $cat);
  119. $root->appendChild($el);
  120. } else {
  121. $el = $dom->createElement('itunes:category');
  122. $el->setAttribute('text', $key);
  123. $root->appendChild($el);
  124. foreach ($cat as $subcat) {
  125. $el2 = $dom->createElement('itunes:category');
  126. $el2->setAttribute('text', $subcat);
  127. $el->appendChild($el2);
  128. }
  129. }
  130. }
  131. }
  132. /**
  133. * Set feed image (icon)
  134. *
  135. * @param DOMDocument $dom
  136. * @param DOMElement $root
  137. * @return void
  138. */
  139. protected function _setImage(DOMDocument $dom, DOMElement $root)
  140. {
  141. $image = $this->getDataContainer()->getItunesImage();
  142. if (!$image) {
  143. return;
  144. }
  145. $el = $dom->createElement('itunes:image');
  146. $el->setAttribute('href', $image);
  147. $root->appendChild($el);
  148. }
  149. /**
  150. * Set feed cumulative duration
  151. *
  152. * @param DOMDocument $dom
  153. * @param DOMElement $root
  154. * @return void
  155. */
  156. protected function _setDuration(DOMDocument $dom, DOMElement $root)
  157. {
  158. $duration = $this->getDataContainer()->getItunesDuration();
  159. if (!$duration) {
  160. return;
  161. }
  162. $el = $dom->createElement('itunes:duration');
  163. $text = $dom->createTextNode($duration);
  164. $el->appendChild($text);
  165. $root->appendChild($el);
  166. }
  167. /**
  168. * Set explicit flag
  169. *
  170. * @param DOMDocument $dom
  171. * @param DOMElement $root
  172. * @return void
  173. */
  174. protected function _setExplicit(DOMDocument $dom, DOMElement $root)
  175. {
  176. $explicit = $this->getDataContainer()->getItunesExplicit();
  177. if (is_null($explicit)) {
  178. return;
  179. }
  180. $el = $dom->createElement('itunes:explicit');
  181. $text = $dom->createTextNode($explicit);
  182. $el->appendChild($text);
  183. $root->appendChild($el);
  184. }
  185. /**
  186. * Set feed keywords
  187. *
  188. * @param DOMDocument $dom
  189. * @param DOMElement $root
  190. * @return void
  191. */
  192. protected function _setKeywords(DOMDocument $dom, DOMElement $root)
  193. {
  194. $keywords = $this->getDataContainer()->getItunesKeywords();
  195. if (!$keywords || empty($keywords)) {
  196. return;
  197. }
  198. $el = $dom->createElement('itunes:keywords');
  199. $text = $dom->createTextNode(implode(',', $keywords));
  200. $el->appendChild($text);
  201. $root->appendChild($el);
  202. }
  203. /**
  204. * Set feed's new URL
  205. *
  206. * @param DOMDocument $dom
  207. * @param DOMElement $root
  208. * @return void
  209. */
  210. protected function _setNewFeedUrl(DOMDocument $dom, DOMElement $root)
  211. {
  212. $url = $this->getDataContainer()->getItunesNewFeedUrl();
  213. if (!$url) {
  214. return;
  215. }
  216. $el = $dom->createElement('itunes:new-feed-url');
  217. $text = $dom->createTextNode($url);
  218. $el->appendChild($text);
  219. $root->appendChild($el);
  220. }
  221. /**
  222. * Set feed owners
  223. *
  224. * @param DOMDocument $dom
  225. * @param DOMElement $root
  226. * @return void
  227. */
  228. protected function _setOwners(DOMDocument $dom, DOMElement $root)
  229. {
  230. $owners = $this->getDataContainer()->getItunesOwners();
  231. if (!$owners || empty($owners)) {
  232. return;
  233. }
  234. foreach ($owners as $owner) {
  235. $el = $dom->createElement('itunes:owner');
  236. $name = $dom->createElement('itunes:name');
  237. $text = $dom->createTextNode($owner['name']);
  238. $name->appendChild($text);
  239. $email = $dom->createElement('itunes:email');
  240. $text = $dom->createTextNode($owner['email']);
  241. $email->appendChild($text);
  242. $root->appendChild($el);
  243. $el->appendChild($name);
  244. $el->appendChild($email);
  245. }
  246. }
  247. /**
  248. * Set feed subtitle
  249. *
  250. * @param DOMDocument $dom
  251. * @param DOMElement $root
  252. * @return void
  253. */
  254. protected function _setSubtitle(DOMDocument $dom, DOMElement $root)
  255. {
  256. $subtitle = $this->getDataContainer()->getItunesSubtitle();
  257. if (!$subtitle) {
  258. return;
  259. }
  260. $el = $dom->createElement('itunes:subtitle');
  261. $text = $dom->createTextNode($subtitle);
  262. $el->appendChild($text);
  263. $root->appendChild($el);
  264. }
  265. /**
  266. * Set feed summary
  267. *
  268. * @param DOMDocument $dom
  269. * @param DOMElement $root
  270. * @return void
  271. */
  272. protected function _setSummary(DOMDocument $dom, DOMElement $root)
  273. {
  274. $summary = $this->getDataContainer()->getItunesSummary();
  275. if (!$summary) {
  276. return;
  277. }
  278. $el = $dom->createElement('itunes:summary');
  279. $text = $dom->createTextNode($summary);
  280. $el->appendChild($text);
  281. $root->appendChild($el);
  282. }
  283. }