1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Nexendrie\Rss; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* RSS Channel Generator |
8
|
|
|
* |
9
|
|
|
* @author Jakub Konečný |
10
|
|
|
* @property string $title |
11
|
|
|
* @property string $description |
12
|
|
|
* @property string $link |
13
|
|
|
* @property string $language |
14
|
|
|
* @property string $copyright |
15
|
|
|
* @property string $managingEditor |
16
|
|
|
* @property string $webMaster |
17
|
|
|
* @property int|null $ttl |
18
|
|
|
* @property callable $dataSource |
19
|
|
|
* @property int $shortenDescription |
20
|
|
|
* @property string $dateTimeFormat |
21
|
|
|
* @property callable $lastBuildDate |
22
|
|
|
* @property callable|null $pubDate |
23
|
|
|
* @property string $generator |
24
|
|
|
* @property string $docs |
25
|
|
|
* @property string $template |
26
|
|
|
* @method void onBeforeGenerate(Generator $generator) |
27
|
|
|
* @method void onAddItem(Generator $generator, \SimpleXMLElement $channel, RssChannelItem $itemDefinition, \SimpleXMLElement $item) |
|
|
|
|
28
|
|
|
* @method void onAfterGenerate(Generator $generator) |
29
|
|
|
*/ |
30
|
1 |
|
final class Generator { |
31
|
1 |
|
use \Nette\SmartObject; |
32
|
|
|
|
33
|
|
|
/** @var string */ |
34
|
|
|
protected $title = ""; |
35
|
|
|
/** @var string */ |
36
|
|
|
protected $description = ""; |
37
|
|
|
/** @var string */ |
38
|
|
|
protected $link = ""; |
39
|
|
|
/** @var string */ |
40
|
|
|
protected $language = ""; |
41
|
|
|
/** @var string */ |
42
|
|
|
protected $copyright = ""; |
43
|
|
|
/** @var string */ |
44
|
|
|
protected $managingEditor = ""; |
45
|
|
|
/** @var string */ |
46
|
|
|
protected $webMaster = ""; |
47
|
|
|
/** @var int|null */ |
48
|
|
|
protected $ttl = null; |
49
|
|
|
/** @var string */ |
50
|
|
|
protected $dateTimeFormat = "D, d M Y H:i:s"; |
51
|
|
|
/** @var callable|null */ |
52
|
|
|
protected $dataSource = null; |
53
|
|
|
/** @var int */ |
54
|
|
|
protected $shortenDescription = 150; |
55
|
|
|
/** @var callable */ |
56
|
|
|
protected $lastBuildDate = "time"; |
57
|
|
|
/** @var callable|null */ |
58
|
|
|
protected $pubDate = null; |
59
|
|
|
/** @var string */ |
60
|
|
|
protected $generator = "Nexendrie RSS"; |
61
|
|
|
/** @var string */ |
62
|
|
|
protected $docs = "http://blogs.law.harvard.edu/tech/rss"; |
63
|
|
|
/** @var string */ |
64
|
|
|
protected $template = __DIR__ . "/template.xml"; |
65
|
|
|
/** @var callable[] */ |
66
|
|
|
public $onBeforeGenerate = []; |
67
|
|
|
/** @var callable[] */ |
68
|
|
|
public $onAddItem = []; |
69
|
|
|
/** @var callable[] */ |
70
|
|
|
public $onAfterGenerate = []; |
71
|
|
|
|
72
|
|
|
public function getTitle(): string { |
73
|
1 |
|
return $this->title; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function setTitle(string $title): void { |
77
|
1 |
|
$this->title = $title; |
78
|
1 |
|
} |
79
|
|
|
|
80
|
|
|
public function getDescription(): string { |
81
|
1 |
|
return $this->description; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function setDescription(string $description): void { |
85
|
1 |
|
$this->description = $description; |
86
|
1 |
|
} |
87
|
|
|
|
88
|
|
|
public function getLink(): string { |
89
|
1 |
|
return $this->link; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function setLink(string $link): void { |
93
|
1 |
|
$this->link = $link; |
94
|
1 |
|
} |
95
|
|
|
|
96
|
|
|
public function getLanguage(): string { |
97
|
1 |
|
return $this->language; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function setLanguage(string $language): void { |
101
|
1 |
|
$this->language = $language; |
102
|
1 |
|
} |
103
|
|
|
|
104
|
|
|
public function getCopyright(): string { |
105
|
1 |
|
return $this->copyright; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
public function setCopyright(string $copyright): void { |
109
|
1 |
|
$this->copyright = $copyright; |
110
|
1 |
|
} |
111
|
|
|
|
112
|
|
|
public function getManagingEditor(): string { |
113
|
1 |
|
return $this->managingEditor; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
public function setManagingEditor(string $managingEditor): void { |
117
|
1 |
|
$this->managingEditor = $managingEditor; |
118
|
1 |
|
} |
119
|
|
|
|
120
|
|
|
public function getWebMaster(): string { |
121
|
1 |
|
return $this->webMaster; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
public function setWebMaster(string $webMaster): void { |
125
|
1 |
|
$this->webMaster = $webMaster; |
126
|
1 |
|
} |
127
|
|
|
|
128
|
|
|
public function getTtl(): ?int { |
129
|
1 |
|
return $this->ttl; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
public function setTtl(int $ttl): void { |
133
|
1 |
|
$this->ttl = $ttl; |
134
|
1 |
|
} |
135
|
|
|
|
136
|
|
|
public function setDataSource(callable $dataSource): void { |
137
|
1 |
|
$this->dataSource = $dataSource; |
138
|
1 |
|
} |
139
|
|
|
|
140
|
|
|
public function getShortenDescription(): int { |
141
|
1 |
|
return $this->shortenDescription; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
public function setShortenDescription(int $value): void { |
145
|
1 |
|
$this->shortenDescription = $value; |
146
|
1 |
|
} |
147
|
|
|
|
148
|
|
|
public function getDateTimeFormat(): string { |
149
|
1 |
|
return $this->dateTimeFormat; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
public function setDateTimeFormat(string $format): void { |
153
|
1 |
|
$this->dateTimeFormat = $format; |
154
|
1 |
|
} |
155
|
|
|
|
156
|
|
|
public function getLastBuildDate(): callable { |
157
|
1 |
|
return $this->lastBuildDate; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
public function setLastBuildDate(callable $lastBuildDate): void { |
161
|
1 |
|
$this->lastBuildDate = $lastBuildDate; |
162
|
1 |
|
} |
163
|
|
|
|
164
|
|
|
public function getPubDate(): ?callable { |
165
|
1 |
|
return $this->pubDate; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
public function setPubDate(callable $pubDate): void { |
169
|
1 |
|
$this->pubDate = $pubDate; |
170
|
1 |
|
} |
171
|
|
|
|
172
|
|
|
public function getGenerator(): string { |
173
|
1 |
|
return $this->generator; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
public function setGenerator(string $generator): void { |
177
|
1 |
|
$this->generator = $generator; |
178
|
1 |
|
} |
179
|
|
|
|
180
|
|
|
public function getDocs(): string { |
181
|
1 |
|
return $this->docs; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
public function setDocs(string $docs): void { |
185
|
1 |
|
$this->docs = $docs; |
186
|
1 |
|
} |
187
|
|
|
|
188
|
|
|
public function getTemplate(): string { |
189
|
1 |
|
return $this->template; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* @throws \RuntimeException |
194
|
|
|
*/ |
195
|
|
|
public function setTemplate(string $template): void { |
196
|
1 |
|
if(!is_file($template)) { |
197
|
1 |
|
throw new \RuntimeException("File $template does not exist."); |
198
|
|
|
} |
199
|
1 |
|
$this->template = $template; |
200
|
1 |
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* @throws InvalidStateException |
204
|
|
|
* @throws \InvalidArgumentException |
205
|
|
|
*/ |
206
|
|
|
protected function getData(): Collection { |
207
|
1 |
|
if(is_null($this->dataSource)) { |
|
|
|
|
208
|
1 |
|
throw new InvalidStateException("Data source for RSS generator is not set."); |
209
|
|
|
} |
210
|
1 |
|
$items = call_user_func($this->dataSource); |
211
|
1 |
|
if(!$items instanceof Collection) { |
212
|
1 |
|
throw new \InvalidArgumentException("Callback for data source for RSS generator has to return an instance of " . Collection::class . "."); |
|
|
|
|
213
|
|
|
} |
214
|
1 |
|
return $items; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
protected function shortenDescription(string $description): string { |
218
|
1 |
|
if($this->shortenDescription < 1) { |
219
|
1 |
|
return $description; |
220
|
|
|
} |
221
|
1 |
|
$originalDescription = $description; |
222
|
1 |
|
$description = substr($description, 0, $this->shortenDescription); |
223
|
1 |
|
if($description !== $originalDescription) { |
224
|
1 |
|
$description .= "..."; |
225
|
|
|
} |
226
|
1 |
|
return $description; |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
protected function writeProperty(\SimpleXMLElement &$channel, string $property): void { |
230
|
1 |
|
if(isset($this->$property) AND $this->$property !== "") { |
231
|
1 |
|
$channel->channel->{$property}[0][0] = $this->$property; |
232
|
|
|
} |
233
|
1 |
|
} |
234
|
|
|
|
235
|
|
|
protected function writeItemProperty(\SimpleXMLElement $element, RssChannelItem $item, string $property, callable $callback = null): void { |
|
|
|
|
236
|
1 |
|
if(isset($item->$property) AND $item->$property !== "") { |
237
|
1 |
|
$value = $item->$property; |
238
|
1 |
|
if(!is_null($callback)) { |
239
|
1 |
|
$value = $callback($value); |
240
|
|
|
} |
241
|
1 |
|
$element->addChild($property, $value); |
242
|
|
|
} |
243
|
1 |
|
} |
244
|
|
|
|
245
|
|
|
/** |
246
|
|
|
* @throws InvalidStateException |
247
|
|
|
* @throws \InvalidArgumentException |
248
|
|
|
*/ |
249
|
|
|
public function generate(): string { |
250
|
1 |
|
$this->onBeforeGenerate($this); |
251
|
1 |
|
$items = $this->getData(); |
252
|
1 |
|
$lastBuildDate = call_user_func($this->lastBuildDate); |
253
|
1 |
|
if(!is_int($lastBuildDate)) { |
254
|
1 |
|
throw new \InvalidArgumentException("Callback for last build date for RSS generator has to return integer."); |
255
|
|
|
} |
256
|
|
|
/** @var \SimpleXMLElement $channel */ |
257
|
1 |
|
$channel = simplexml_load_file($this->template); |
258
|
1 |
|
$channel->channel->lastBuildDate[0][0] = date($this->dateTimeFormat, $lastBuildDate); |
259
|
1 |
|
if(isset($this->pubDate)) { |
260
|
1 |
|
$pubDate = call_user_func($this->pubDate); |
261
|
1 |
|
if(!is_int($pubDate)) { |
262
|
1 |
|
throw new \InvalidArgumentException("Callback for pub date for RSS generator has to return integer."); |
263
|
|
|
} |
264
|
1 |
|
$channel->channel->addChild("pubDate", date($this->dateTimeFormat, $pubDate)); |
265
|
|
|
} |
266
|
1 |
|
$this->writeProperty($channel, "link"); |
267
|
1 |
|
$this->writeProperty($channel, "title"); |
268
|
1 |
|
$this->writeProperty($channel, "description"); |
269
|
1 |
|
$this->writeProperty($channel, "language"); |
270
|
1 |
|
$this->writeProperty($channel, "copyright"); |
271
|
1 |
|
$this->writeProperty($channel, "managingEditor"); |
272
|
1 |
|
$this->writeProperty($channel, "webMaster"); |
273
|
1 |
|
$this->writeProperty($channel, "ttl"); |
274
|
1 |
|
$this->writeProperty($channel, "generator"); |
275
|
1 |
|
$this->writeProperty($channel, "docs"); |
276
|
|
|
/** @var RssChannelItem $item */ |
277
|
1 |
|
foreach($items as $item) { |
278
|
|
|
/** @var \SimpleXMLElement $i */ |
279
|
1 |
|
$i = $channel->channel->addChild("item"); |
280
|
1 |
|
$this->writeItemProperty($i, $item, "title"); |
281
|
1 |
|
$this->writeItemProperty($i, $item, "link"); |
282
|
1 |
|
$this->writeItemProperty($i, $item, "pubDate", function($value) { |
283
|
1 |
|
return date($this->dateTimeFormat, $value); |
284
|
1 |
|
}); |
285
|
1 |
|
$this->writeItemProperty($i, $item, "description", [$this, "shortenDescription"]); |
286
|
1 |
|
$this->writeItemProperty($i, $item, "author"); |
287
|
1 |
|
$this->writeItemProperty($i, $item, "comments"); |
288
|
1 |
|
$this->writeItemProperty($i, $item, "guid"); |
289
|
1 |
|
$this->onAddItem($this, $channel, $item, $i); |
290
|
|
|
} |
291
|
1 |
|
$this->onAfterGenerate($this); |
292
|
1 |
|
return $channel->asXML(); |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
/** |
296
|
|
|
* @throws InvalidStateException |
297
|
|
|
* @throws \InvalidArgumentException |
298
|
|
|
*/ |
299
|
|
|
public function response(): RssResponse { |
300
|
1 |
|
return new RssResponse($this->generate()); |
301
|
|
|
} |
302
|
|
|
} |
303
|
|
|
?> |