AtomFeedBuilder.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. namespace PicoFeed\Syndication;
  3. use DOMAttr;
  4. use DOMElement;
  5. /**
  6. * Atom Feed Builder
  7. *
  8. * @package PicoFeed\Syndication
  9. * @author Frederic Guillot
  10. */
  11. class AtomFeedBuilder extends FeedBuilder
  12. {
  13. /**
  14. * @var DOMElement
  15. */
  16. protected $feedElement;
  17. /**
  18. * @var AtomHelper
  19. */
  20. protected $helper;
  21. /**
  22. * Build feed
  23. *
  24. * @access public
  25. * @param string $filename
  26. * @return string
  27. */
  28. public function build($filename = '')
  29. {
  30. $this->helper = new AtomHelper($this->getDocument());
  31. $this->feedElement = $this->getDocument()->createElement('feed');
  32. $this->feedElement->setAttributeNodeNS(new DomAttr('xmlns', 'http://www.w3.org/2005/Atom'));
  33. $generator = $this->getDocument()->createElement('generator', 'PicoFeed');
  34. $generator->setAttribute('uri', 'https://github.com/miniflux/picoFeed');
  35. $this->feedElement->appendChild($generator);
  36. $this->helper
  37. ->buildTitle($this->feedElement, $this->feedTitle)
  38. ->buildId($this->feedElement, $this->feedUrl)
  39. ->buildDate($this->feedElement, $this->feedDate)
  40. ->buildLink($this->feedElement, $this->siteUrl)
  41. ->buildLink($this->feedElement, $this->feedUrl, 'self', 'application/atom+xml')
  42. ->buildAuthor($this->feedElement, $this->authorName, $this->authorEmail, $this->authorUrl)
  43. ;
  44. foreach ($this->items as $item) {
  45. $this->feedElement->appendChild($item->build());
  46. }
  47. $this->getDocument()->appendChild($this->feedElement);
  48. if ($filename !== '') {
  49. $this->getDocument()->save($filename);
  50. }
  51. return $this->getDocument()->saveXML();
  52. }
  53. }