Rss20FeedBuilder.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. namespace PicoFeed\Syndication;
  3. use DOMAttr;
  4. use DOMElement;
  5. /**
  6. * Rss20 Feed Builder
  7. *
  8. * @package PicoFeed\Syndication
  9. * @author Frederic Guillot
  10. */
  11. class Rss20FeedBuilder extends FeedBuilder
  12. {
  13. /**
  14. * @var DOMElement
  15. */
  16. protected $rssElement;
  17. /**
  18. * @var Rss20Helper
  19. */
  20. protected $helper;
  21. /**
  22. * @var DOMElement
  23. */
  24. protected $channelElement;
  25. /**
  26. * Build feed
  27. *
  28. * @access public
  29. * @param string $filename
  30. * @return string
  31. */
  32. public function build($filename = '')
  33. {
  34. $this->helper = new Rss20Helper($this->getDocument());
  35. $this->rssElement = $this->getDocument()->createElement('rss');
  36. $this->rssElement->setAttribute('version', '2.0');
  37. $this->rssElement->setAttributeNodeNS(new DomAttr('xmlns:content', 'http://purl.org/rss/1.0/modules/content/'));
  38. $this->rssElement->setAttributeNodeNS(new DomAttr('xmlns:atom', 'http://www.w3.org/2005/Atom'));
  39. $this->channelElement = $this->getDocument()->createElement('channel');
  40. $this->helper
  41. ->buildNode($this->channelElement, 'generator', 'PicoFeed (https://github.com/miniflux/picoFeed)')
  42. ->buildTitle($this->channelElement, $this->feedTitle)
  43. ->buildNode($this->channelElement, 'description', $this->feedTitle)
  44. ->buildDate($this->channelElement, $this->feedDate)
  45. ->buildAuthor($this->channelElement, 'webMaster', $this->authorName, $this->authorEmail)
  46. ->buildLink($this->channelElement, $this->siteUrl)
  47. ;
  48. $link = $this->getDocument()->createElement('atom:link');
  49. $link->setAttribute('href', $this->feedUrl);
  50. $link->setAttribute('rel', 'self');
  51. $link->setAttribute('type', 'application/rss+xml');
  52. $this->channelElement->appendChild($link);
  53. foreach ($this->items as $item) {
  54. $this->channelElement->appendChild($item->build());
  55. }
  56. $this->rssElement->appendChild($this->channelElement);
  57. $this->getDocument()->appendChild($this->rssElement);
  58. if ($filename !== '') {
  59. $this->getDocument()->save($filename);
  60. }
  61. return $this->getDocument()->saveXML();
  62. }
  63. }