class.rssbuilder.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. //-------------------------------------------------------------------------------
  3. // RSS Builder Class
  4. //-------------------------------------------------------------------------------
  5. // Author Mert Yazicioglu
  6. // Author URI http://www.mertyazicioglu.com
  7. // Date 13th September 2011
  8. // License GNU GPLv2
  9. //-------------------------------------------------------------------------------
  10. // Builds RSS Feed with given values.
  11. //-------------------------------------------------------------------------------
  12. class RSSBuilder extends DOMDocument {
  13. private $channel;
  14. private $currentItem;
  15. private $rss;
  16. /**
  17. * Class constructor.
  18. *
  19. * @param void
  20. * @return void
  21. */
  22. public function __construct() {
  23. parent::__construct('1.0','utf-8');
  24. $this->formatOutput = TRUE;
  25. $rssElement = $this->createElement( 'rss' );
  26. $rssElement->setAttribute( 'version', '2.0' );
  27. $this->rss = $this->appendChild( $rssElement );
  28. }
  29. /**
  30. * Adds a new channel to the feed.
  31. *
  32. * @param void
  33. * @return void
  34. */
  35. public function addChannel() {
  36. $channelElement = $this->createElement( 'channel' );
  37. $this->channel = $this->rss->appendChild( $channelElement );
  38. }
  39. /**
  40. * Adds a new element to the channel.
  41. *
  42. * @param string $element Name of the channel element.
  43. * @param string $value Value for the given element.
  44. * @param array $attr Two-dim array of attributes and their values for the element. (Optional)
  45. * @return void
  46. */
  47. public function addChannelElement( $element, $value, $attr = array() ) {
  48. $element = $this->createElement( $element, $value );
  49. foreach ( $attr as $key => $value )
  50. $element->setAttribute( $key, $value );
  51. $this->channel->appendChild( $element );
  52. }
  53. /**
  54. * Adds a new element with sub elements to the channel.
  55. *
  56. * @param string $element Name of the channel element.
  57. * @param array Two-dim array of sub elements and their values.
  58. * @return void
  59. */
  60. public function addChannelElementWithSub( $element, $sub ) {
  61. $element = $this->createElement( $element );
  62. foreach ( $sub as $key => $value ) {
  63. $subElement = $this->createElement( $key, $value );
  64. $element->appendChild( $subElement );
  65. }
  66. $this->channel->appendChild( $element );
  67. }
  68. /**
  69. * Adds a new channel item.
  70. *
  71. * @param void
  72. * @return void
  73. */
  74. public function addItem() {
  75. $item = $this->createElement( 'item' );
  76. $this->currentItem = $this->channel->appendChild( $item );
  77. }
  78. /**
  79. * Adds a new sub element to the recently added channel item.
  80. *
  81. * @param string $element Name of the sub item element.
  82. * @param string $value Value for the given element.
  83. * @param array $attr Two-dim array of attributes and their values for the element. (Optional)
  84. * @return void
  85. */
  86. public function addItemElement( $element, $value, $attr = array() ) {
  87. $element = $this->createElement( $element, $value );
  88. foreach ( $attr as $key => $value )
  89. $element->setAttribute( $key, $value );
  90. $this->currentItem->appendChild( $element );
  91. }
  92. /**
  93. * Prints the XML document created.
  94. *
  95. * @param void
  96. * @return string The created XML document.
  97. */
  98. public function __toString() {
  99. return $this->saveXML();
  100. }
  101. }