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