Rss20Helper.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. namespace PicoFeed\Syndication;
  3. use DateTime;
  4. use DOMDocument;
  5. use DOMElement;
  6. /**
  7. * Class Rss20Helper
  8. *
  9. * @package PicoFeed\Syndication
  10. * @author Frederic Guillot
  11. */
  12. class Rss20Helper
  13. {
  14. /**
  15. * @var DOMDocument
  16. */
  17. protected $document;
  18. /**
  19. * Constructor
  20. *
  21. * @param DOMDocument $document
  22. */
  23. public function __construct(DOMDocument $document)
  24. {
  25. $this->document = $document;
  26. }
  27. /**
  28. * Build node
  29. *
  30. * @access public
  31. * @param DOMElement $element
  32. * @param string $tag
  33. * @param string $value
  34. * @return $this
  35. */
  36. public function buildNode(DOMElement $element, $tag, $value)
  37. {
  38. $node = $this->document->createElement($tag);
  39. $node->appendChild($this->document->createTextNode($value));
  40. $element->appendChild($node);
  41. return $this;
  42. }
  43. /**
  44. * Build title
  45. *
  46. * @access public
  47. * @param DOMElement $element
  48. * @param string $title
  49. * @return $this
  50. */
  51. public function buildTitle(DOMElement $element, $title)
  52. {
  53. return $this->buildNode($element, 'title', $title);
  54. }
  55. /**
  56. * Build date element
  57. *
  58. * @access public
  59. * @param DOMElement $element
  60. * @param DateTime $date
  61. * @param string $type
  62. * @return $this
  63. */
  64. public function buildDate(DOMElement $element, DateTime $date, $type = 'pubDate')
  65. {
  66. return $this->buildNode($element, $type, $date->format(DateTime::RSS));
  67. }
  68. /**
  69. * Build link element
  70. *
  71. * @access public
  72. * @param DOMElement $element
  73. * @param string $url
  74. * @return $this
  75. */
  76. public function buildLink(DOMElement $element, $url)
  77. {
  78. return $this->buildNode($element, 'link', $url);
  79. }
  80. /**
  81. * Build author element
  82. *
  83. * @access public
  84. * @param DOMElement $element
  85. * @param string $tag
  86. * @param string $authorName
  87. * @param string $authorEmail
  88. * @return $this
  89. */
  90. public function buildAuthor(DOMElement $element, $tag, $authorName, $authorEmail)
  91. {
  92. if (!empty($authorName)) {
  93. $value = '';
  94. if (!empty($authorEmail)) {
  95. $value .= $authorEmail.' ('.$authorName.')';
  96. } else {
  97. $value = $authorName;
  98. }
  99. $this->buildNode($element, $tag, $value);
  100. }
  101. return $this;
  102. }
  103. }