FeedBuilder.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <?php
  2. namespace PicoFeed\Syndication;
  3. use DateTime;
  4. use DOMDocument;
  5. /**
  6. * Class FeedBuilder
  7. *
  8. * @package PicoFeed\Syndication
  9. * @author Frederic Guillot
  10. */
  11. abstract class FeedBuilder
  12. {
  13. /**
  14. * @var DOMDocument
  15. */
  16. protected $document;
  17. /**
  18. * @var string
  19. */
  20. protected $feedTitle;
  21. /**
  22. * @var string
  23. */
  24. protected $feedUrl;
  25. /**
  26. * @var string
  27. */
  28. protected $siteUrl;
  29. /**
  30. * @var string
  31. */
  32. protected $authorName;
  33. /**
  34. * @var string
  35. */
  36. protected $authorEmail;
  37. /**
  38. * @var string
  39. */
  40. protected $authorUrl;
  41. /**
  42. * @var DateTime
  43. */
  44. protected $feedDate;
  45. /**
  46. * @var ItemBuilder[]
  47. */
  48. protected $items = array();
  49. /**
  50. * Constructor
  51. *
  52. * @access public
  53. */
  54. public function __construct()
  55. {
  56. $this->document = new DomDocument('1.0', 'UTF-8');
  57. $this->document->formatOutput = true;
  58. }
  59. /**
  60. * Get new object instance
  61. *
  62. * @access public
  63. * @return static
  64. */
  65. public static function create()
  66. {
  67. return new static();
  68. }
  69. /**
  70. * Add feed title
  71. *
  72. * @access public
  73. * @param string $title
  74. * @return $this
  75. */
  76. public function withTitle($title)
  77. {
  78. $this->feedTitle = $title;
  79. return $this;
  80. }
  81. /**
  82. * Add feed url
  83. *
  84. * @access public
  85. * @param string $url
  86. * @return $this
  87. */
  88. public function withFeedUrl($url)
  89. {
  90. $this->feedUrl = $url;
  91. return $this;
  92. }
  93. /**
  94. * Add website url
  95. *
  96. * @access public
  97. * @param string $url
  98. * @return $this
  99. */
  100. public function withSiteUrl($url)
  101. {
  102. $this->siteUrl = $url;
  103. return $this;
  104. }
  105. /**
  106. * Add feed date
  107. *
  108. * @access public
  109. * @param DateTime $date
  110. * @return $this
  111. */
  112. public function withDate(DateTime $date)
  113. {
  114. $this->feedDate = $date;
  115. return $this;
  116. }
  117. /**
  118. * Add feed author
  119. *
  120. * @access public
  121. * @param string $name
  122. * @param string $email
  123. * @param string $url
  124. * @return $this
  125. */
  126. public function withAuthor($name, $email = '', $url ='')
  127. {
  128. $this->authorName = $name;
  129. $this->authorEmail = $email;
  130. $this->authorUrl = $url;
  131. return $this;
  132. }
  133. /**
  134. * Add feed item
  135. *
  136. * @access public
  137. * @param ItemBuilder $item
  138. * @return $this
  139. */
  140. public function withItem(ItemBuilder $item)
  141. {
  142. $this->items[] = $item;
  143. return $this;
  144. }
  145. /**
  146. * Get DOM document
  147. *
  148. * @access public
  149. * @return DOMDocument
  150. */
  151. public function getDocument()
  152. {
  153. return $this->document;
  154. }
  155. /**
  156. * Build feed
  157. *
  158. * @abstract
  159. * @access public
  160. * @param string $filename
  161. * @return string
  162. */
  163. abstract public function build($filename = '');
  164. }