1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bhaktaraz\RSSGenerator; |
4
|
|
|
|
5
|
|
|
use Bhaktaraz\RSSGenerator\FeedInterface; |
6
|
|
|
use Bhaktaraz\RSSGenerator\ItemInterface; |
7
|
|
|
use Bhaktaraz\RSSGenerator\SimpleXMLElement; |
8
|
|
|
use Bhaktaraz\RSSGenerator\ChannelInterface; |
9
|
|
|
|
10
|
|
|
class Channel implements ChannelInterface |
11
|
|
|
{ |
12
|
|
|
|
13
|
|
|
/** @var string */ |
14
|
|
|
protected $title; |
15
|
|
|
|
16
|
|
|
/** @var string */ |
17
|
|
|
protected $url; |
18
|
|
|
|
19
|
|
|
/** @var string */ |
20
|
|
|
protected $description; |
21
|
|
|
|
22
|
|
|
/** @var string */ |
23
|
|
|
protected $language; |
24
|
|
|
|
25
|
|
|
/** @var string */ |
26
|
|
|
protected $copyright; |
27
|
|
|
|
28
|
|
|
/** @var int */ |
29
|
|
|
protected $pubDate; |
30
|
|
|
|
31
|
|
|
/** @var int */ |
32
|
|
|
protected $lastBuildDate; |
33
|
|
|
|
34
|
|
|
/** @var string */ |
35
|
|
|
protected $updatePeriod; |
36
|
|
|
|
37
|
|
|
/** @var integer */ |
38
|
|
|
protected $updateFrequency; |
39
|
|
|
|
40
|
|
|
/** @var int */ |
41
|
|
|
protected $ttl; |
42
|
|
|
|
43
|
|
|
/** @var ItemInterface[] */ |
44
|
|
|
protected $items = []; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Set channel title |
48
|
|
|
* @param string $title |
49
|
|
|
* @return $this |
50
|
|
|
*/ |
51
|
|
|
public function title($title) |
52
|
|
|
{ |
53
|
|
|
$this->title = $title; |
54
|
|
|
|
55
|
|
|
return $this; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Set channel URL |
60
|
|
|
* @param string $url |
61
|
|
|
* @return $this |
62
|
|
|
*/ |
63
|
|
|
public function url($url) |
64
|
|
|
{ |
65
|
|
|
$this->url = $url; |
66
|
|
|
|
67
|
|
|
return $this; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Set channel description |
72
|
|
|
* @param string $description |
73
|
|
|
* @return $this |
74
|
|
|
*/ |
75
|
|
|
public function description($description) |
76
|
|
|
{ |
77
|
|
|
$this->description = $description; |
78
|
|
|
|
79
|
|
|
return $this; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Set ISO639 language code |
84
|
|
|
* |
85
|
|
|
* The language the channel is written in. This allows aggregators to group all |
86
|
|
|
* Italian language sites, for example, on a single page. A list of allowable |
87
|
|
|
* values for this element, as provided by Netscape, is here. You may also use |
88
|
|
|
* values defined by the W3C. |
89
|
|
|
* |
90
|
|
|
* @param string $language |
91
|
|
|
* @return $this |
92
|
|
|
*/ |
93
|
|
|
public function language($language) |
94
|
|
|
{ |
95
|
|
|
$this->language = $language; |
96
|
|
|
|
97
|
|
|
return $this; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Set channel copyright |
102
|
|
|
* @param string $copyright |
103
|
|
|
* @return $this |
104
|
|
|
*/ |
105
|
|
|
public function copyright($copyright) |
106
|
|
|
{ |
107
|
|
|
$this->copyright = $copyright; |
108
|
|
|
|
109
|
|
|
return $this; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Set channel published date |
114
|
|
|
* @param int $pubDate Unix timestamp |
115
|
|
|
* @return $this |
116
|
|
|
*/ |
117
|
|
|
public function pubDate($pubDate) |
118
|
|
|
{ |
119
|
|
|
$this->pubDate = $pubDate; |
120
|
|
|
|
121
|
|
|
return $this; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Set channel last build date |
126
|
|
|
* @param int $lastBuildDate Unix timestamp |
127
|
|
|
* @return $this |
128
|
|
|
*/ |
129
|
|
|
public function lastBuildDate($lastBuildDate) |
130
|
|
|
{ |
131
|
|
|
$this->lastBuildDate = $lastBuildDate; |
132
|
|
|
|
133
|
|
|
return $this; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @param $updatePeriod |
138
|
|
|
* @return $this |
139
|
|
|
*/ |
140
|
|
|
public function updatePeriod($updatePeriod) |
141
|
|
|
{ |
142
|
|
|
$this->updatePeriod = $updatePeriod; |
143
|
|
|
|
144
|
|
|
return $this; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @param $updateFrequency |
149
|
|
|
* @return $this |
150
|
|
|
*/ |
151
|
|
|
public function updateFrequency($updateFrequency) |
152
|
|
|
{ |
153
|
|
|
$this->updateFrequency = $updateFrequency; |
154
|
|
|
|
155
|
|
|
return $this; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Set channel ttl (minutes) |
160
|
|
|
* @param int $ttl |
161
|
|
|
* @return $this |
162
|
|
|
*/ |
163
|
|
|
public function ttl($ttl) |
164
|
|
|
{ |
165
|
|
|
$this->ttl = $ttl; |
166
|
|
|
|
167
|
|
|
return $this; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Add item object |
172
|
|
|
* @param ItemInterface $item |
173
|
|
|
* @return $this |
174
|
|
|
*/ |
175
|
|
|
public function addItem(ItemInterface $item) |
176
|
|
|
{ |
177
|
|
|
$this->items[] = $item; |
178
|
|
|
|
179
|
|
|
return $this; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Append to feed |
184
|
|
|
* @param FeedInterface $feed |
185
|
|
|
* @return $this |
186
|
|
|
*/ |
187
|
|
|
public function appendTo(FeedInterface $feed) |
188
|
|
|
{ |
189
|
|
|
$feed->addChannel($this); |
190
|
|
|
|
191
|
|
|
return $this; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* Return XML object |
196
|
|
|
* @return SimpleXMLElement |
197
|
|
|
*/ |
198
|
|
|
public function asXML() |
199
|
|
|
{ |
200
|
|
|
$xml = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8" ?><channel></channel>', |
201
|
|
|
LIBXML_NOERROR | LIBXML_ERR_NONE | LIBXML_ERR_FATAL); |
202
|
|
|
$xml->addChild('title', $this->title); |
203
|
|
|
$xml->addChild('link', $this->url); |
204
|
|
|
$xml->addChild('description', $this->description); |
205
|
|
|
|
206
|
|
|
if ($this->language !== null) { |
207
|
|
|
$xml->addChild('language', $this->language); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
if ($this->updatePeriod) { |
211
|
|
|
$xml->addChild("xmlns:sy:updatePeriod", $this->updatePeriod); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
if ($this->updateFrequency) { |
215
|
|
|
$xml->addChild("xmlns:sy:updateFrequency", $this->updateFrequency); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
if ($this->copyright !== null) { |
219
|
|
|
$xml->addChild('copyright', $this->copyright); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
if ($this->pubDate !== null) { |
223
|
|
|
$xml->addChild('pubDate', date(DATE_RSS, $this->pubDate)); |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
if ($this->lastBuildDate !== null) { |
227
|
|
|
$xml->addChild('lastBuildDate', date(DATE_RSS, $this->lastBuildDate)); |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
if ($this->ttl !== null) { |
231
|
|
|
$xml->addChild('ttl', $this->ttl); |
232
|
|
|
} |
233
|
|
|
|
234
|
|
View Code Duplication |
foreach ($this->items as $item) { |
|
|
|
|
235
|
|
|
$toDom = dom_import_simplexml($xml); |
236
|
|
|
$fromDom = dom_import_simplexml($item->asXML()); |
237
|
|
|
$toDom->appendChild($toDom->ownerDocument->importNode($fromDom, true)); |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
return $xml; |
241
|
|
|
} |
242
|
|
|
} |
243
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.