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