1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the feed-io package. |
4
|
|
|
* |
5
|
|
|
* (c) Alexandre Debril <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace FeedIo; |
12
|
|
|
|
13
|
|
|
use FeedIo\Filter\ModifiedSince; |
14
|
|
|
use FeedIo\Reader; |
15
|
|
|
use FeedIo\Reader\FixerSet; |
16
|
|
|
use FeedIo\Reader\FixerAbstract; |
17
|
|
|
use FeedIo\Rule\DateTimeBuilder; |
18
|
|
|
use FeedIo\Adapter\ClientInterface; |
19
|
|
|
use FeedIo\Standard\Atom; |
20
|
|
|
use FeedIo\Standard\Json; |
21
|
|
|
use FeedIo\Standard\Loader; |
22
|
|
|
use FeedIo\Standard\Rss; |
23
|
|
|
use FeedIo\Standard\Rdf; |
24
|
|
|
use Psr\Log\LoggerInterface; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* This class acts as a facade. It provides methods to access feed-io main features |
28
|
|
|
* |
29
|
|
|
* <code> |
30
|
|
|
* // $client is a \FeedIo\Adapter\ClientInterface instance, $logger a \Psr\Log\LoggerInterface |
31
|
|
|
* $feedIo = new FeedIo($client, $logger); |
32
|
|
|
* |
33
|
|
|
* // read a feed. Output is a Result instance |
34
|
|
|
* $result = $feedIo->read('http://somefeed.org/feed.rss'); |
35
|
|
|
* |
36
|
|
|
* // use the feed |
37
|
|
|
* $feed = $result->getFeed(); |
38
|
|
|
* echo $feed->getTitle(); |
39
|
|
|
* |
40
|
|
|
* // and its items |
41
|
|
|
* foreach ( $feed as $item ) { |
42
|
|
|
* echo $item->getTitle(); |
43
|
|
|
* echo $item->getDescription(); |
44
|
|
|
* } |
45
|
|
|
* |
46
|
|
|
* </code> |
47
|
|
|
* |
48
|
|
|
* <code> |
49
|
|
|
* // build the feed to publish |
50
|
|
|
* $feed = new \FeedIo\Feed; |
51
|
|
|
* $feed->setTitle('title'); |
52
|
|
|
* // ... |
53
|
|
|
* |
54
|
|
|
* // add items to it |
55
|
|
|
* $item = new \FeedIo\Feed\Item |
56
|
|
|
* $item->setTitle('my great post'); |
57
|
|
|
* |
58
|
|
|
* // want to publish a media ? no problem |
59
|
|
|
* $media = new \FeedIo\Feed\Item\Media |
60
|
|
|
* $media->setUrl('http://yourdomain.tld/medias/some-podcast.mp3'); |
61
|
|
|
* $media->setType('audio/mpeg'); |
62
|
|
|
* |
63
|
|
|
* // add it to the item |
64
|
|
|
* $item->addMedia($media); |
65
|
|
|
* |
66
|
|
|
* // add the item to the feed (almost there) |
67
|
|
|
* $feed->add($item); |
68
|
|
|
* |
69
|
|
|
* // format it in atom |
70
|
|
|
* $feedIo->toAtom($feed); |
71
|
|
|
* </code> |
72
|
|
|
* |
73
|
|
|
*/ |
74
|
|
|
class FeedIo |
75
|
|
|
{ |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @var \FeedIo\Reader |
79
|
|
|
*/ |
80
|
|
|
protected $reader; |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @var \FeedIo\Rule\DateTimeBuilder |
84
|
|
|
*/ |
85
|
|
|
protected $dateTimeBuilder; |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @var \Psr\Log\LoggerInterface |
89
|
|
|
*/ |
90
|
|
|
protected $logger; |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @var array |
94
|
|
|
*/ |
95
|
|
|
protected $standards; |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @var \FeedIo\Reader\FixerSet |
99
|
|
|
*/ |
100
|
|
|
protected $fixerSet; |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param \FeedIo\Adapter\ClientInterface $client |
104
|
|
|
* @param \Psr\Log\LoggerInterface $logger |
105
|
|
|
*/ |
106
|
10 |
|
public function __construct(ClientInterface $client, LoggerInterface $logger) |
107
|
|
|
{ |
108
|
10 |
|
$this->logger = $logger; |
109
|
10 |
|
$this->dateTimeBuilder = new DateTimeBuilder($logger); |
110
|
10 |
|
$this->setReader(new Reader($client, $logger)); |
111
|
10 |
|
$this->loadCommonStandards(); |
112
|
10 |
|
$this->loadFixerSet(); |
113
|
10 |
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Loads main standards (RSS, RDF, Atom) in current object's attributes |
117
|
|
|
* |
118
|
|
|
* @return $this |
119
|
|
|
*/ |
120
|
10 |
|
protected function loadCommonStandards() |
121
|
|
|
{ |
122
|
10 |
|
$standards = $this->getCommonStandards(); |
123
|
10 |
|
foreach ($standards as $name => $standard) { |
124
|
10 |
|
$this->addStandard($name, $standard); |
125
|
10 |
|
} |
126
|
|
|
|
127
|
10 |
|
return $this; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* adds a filter to the reader |
132
|
|
|
* |
133
|
|
|
* @param \FeedIo\FilterInterface $filter |
134
|
|
|
* @return $this |
135
|
|
|
*/ |
136
|
2 |
|
public function addFilter(FilterInterface $filter) |
137
|
|
|
{ |
138
|
2 |
|
$this->getReader()->addFilter($filter); |
139
|
|
|
|
140
|
2 |
|
return $this; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Returns main standards |
145
|
|
|
* |
146
|
|
|
* @return array |
147
|
|
|
*/ |
148
|
10 |
|
public function getCommonStandards() |
149
|
|
|
{ |
150
|
10 |
|
$loader = new Loader(); |
151
|
|
|
|
152
|
10 |
|
return $loader->getCommonStandards($this->getDateTimeBuilder()); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @param string $name |
157
|
|
|
* @param \FeedIo\StandardAbstract $standard |
158
|
|
|
* @return $this |
159
|
|
|
*/ |
160
|
10 |
|
public function addStandard($name, StandardAbstract $standard) |
161
|
|
|
{ |
162
|
10 |
|
$name = strtolower($name); |
163
|
10 |
|
$this->standards[$name] = $standard; |
164
|
10 |
|
$parser = $this->newParser($standard->getSyntaxFormat(), $standard); |
165
|
10 |
|
$this->reader->addParser($parser); |
166
|
|
|
|
167
|
10 |
|
return $this; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* @param string $format |
172
|
|
|
* @param StandardAbstract $standard |
173
|
|
|
* @return object |
174
|
|
|
*/ |
175
|
9 |
|
public function newParser($format, StandardAbstract $standard) |
176
|
|
|
{ |
177
|
9 |
|
$reflection = new \ReflectionClass("FeedIo\\Parser\\{$format}Parser"); |
178
|
|
|
|
179
|
9 |
|
if ( ! $reflection->isSubclassOf('FeedIo\ParserAbstract') ) { |
180
|
|
|
throw new \InvalidArgumentException(); |
181
|
|
|
} |
182
|
|
|
|
183
|
9 |
|
return $reflection->newInstanceArgs([$standard, $this->logger]); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* @return \FeedIo\Reader\FixerSet |
188
|
|
|
*/ |
189
|
1 |
|
public function getFixerSet() |
190
|
|
|
{ |
191
|
1 |
|
return $this->fixerSet; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* @return $this |
196
|
|
|
*/ |
197
|
9 |
|
protected function loadFixerSet() |
198
|
|
|
{ |
199
|
9 |
|
$this->fixerSet = new FixerSet(); |
200
|
9 |
|
$fixers = $this->getBaseFixers(); |
201
|
|
|
|
202
|
9 |
|
foreach ($fixers as $fixer) { |
203
|
9 |
|
$this->addFixer($fixer); |
204
|
9 |
|
} |
205
|
|
|
|
206
|
9 |
|
return $this; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* @param FixerAbstract $fixer |
211
|
|
|
* @return $this |
212
|
|
|
*/ |
213
|
9 |
|
public function addFixer(FixerAbstract $fixer) |
214
|
|
|
{ |
215
|
9 |
|
$fixer->setLogger($this->logger); |
216
|
9 |
|
$this->fixerSet->add($fixer); |
217
|
|
|
|
218
|
9 |
|
return $this; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* @return array |
223
|
|
|
*/ |
224
|
9 |
|
public function getBaseFixers() |
225
|
|
|
{ |
226
|
|
|
return array( |
227
|
9 |
|
new Reader\Fixer\LastModified(), |
228
|
9 |
|
new Reader\Fixer\PublicId(), |
229
|
|
|
|
230
|
9 |
|
); |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* @param array $formats |
235
|
|
|
* @return $this |
236
|
|
|
*/ |
237
|
1 |
|
public function addDateFormats(array $formats) |
238
|
|
|
{ |
239
|
1 |
|
foreach( $formats as $format ) { |
240
|
1 |
|
$this->getDateTimeBuilder()->addDateFormat($format); |
241
|
1 |
|
} |
242
|
|
|
|
243
|
1 |
|
return $this; |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* @return \FeedIo\Rule\DateTimeBuilder |
248
|
|
|
*/ |
249
|
10 |
|
public function getDateTimeBuilder() |
250
|
|
|
{ |
251
|
10 |
|
return $this->dateTimeBuilder; |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
/** |
255
|
|
|
* @return \FeedIo\Reader |
256
|
|
|
*/ |
257
|
4 |
|
public function getReader() |
258
|
|
|
{ |
259
|
4 |
|
return $this->reader; |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
/** |
263
|
|
|
* @param \FeedIo\Reader |
264
|
|
|
* @return $this |
265
|
|
|
*/ |
266
|
10 |
|
public function setReader(Reader $reader) |
267
|
|
|
{ |
268
|
10 |
|
$this->reader = $reader; |
269
|
|
|
|
270
|
10 |
|
return $this; |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
/** |
274
|
|
|
* @param $url |
275
|
|
|
* @param FeedInterface $feed |
276
|
|
|
* @param \DateTime $modifiedSince |
277
|
|
|
* @return \FeedIo\Reader\Result |
278
|
|
|
*/ |
279
|
2 |
|
public function read($url, FeedInterface $feed = null, \DateTime $modifiedSince = null) |
280
|
|
|
{ |
281
|
2 |
|
if (is_null($feed)) { |
282
|
1 |
|
$feed = new Feed(); |
283
|
1 |
|
} |
284
|
|
|
|
285
|
2 |
|
if ($modifiedSince instanceof \DateTime) { |
286
|
1 |
|
$this->addFilter(new ModifiedSince($modifiedSince)); |
287
|
1 |
|
} |
288
|
|
|
|
289
|
2 |
|
$this->logAction($feed, "read access : $url into a feed instance"); |
290
|
2 |
|
$result = $this->reader->read($url, $feed, $modifiedSince); |
291
|
|
|
|
292
|
2 |
|
$this->fixerSet->correct($result->getFeed()); |
293
|
|
|
|
294
|
2 |
|
return $result; |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
/** |
298
|
|
|
* @param $url |
299
|
|
|
* @param \DateTime $modifiedSince |
300
|
|
|
* @return \FeedIo\Reader\Result |
301
|
|
|
*/ |
302
|
1 |
|
public function readSince($url, \DateTime $modifiedSince) |
303
|
|
|
{ |
304
|
1 |
|
return $this->read($url, new Feed(), $modifiedSince); |
305
|
|
|
} |
306
|
|
|
|
307
|
|
|
/** |
308
|
|
|
* @return $this |
309
|
|
|
*/ |
310
|
1 |
|
public function resetFilters() |
311
|
|
|
{ |
312
|
1 |
|
$this->getReader()->resetFilters(); |
313
|
|
|
|
314
|
1 |
|
return $this; |
315
|
|
|
} |
316
|
|
|
|
317
|
|
|
/** |
318
|
|
|
* @param FeedInterface $feed |
319
|
|
|
* @param string $standard Standard's name |
320
|
|
|
* @return \DomDocument |
321
|
|
|
*/ |
322
|
1 |
|
public function format(FeedInterface $feed, $standard) |
323
|
|
|
{ |
324
|
1 |
|
$this->logAction($feed, "formatting a feed in $standard format"); |
325
|
|
|
|
326
|
1 |
|
$formatter = new Formatter($this->getStandard($standard), $this->logger); |
327
|
|
|
|
328
|
1 |
|
return $formatter->toDom($feed); |
329
|
|
|
} |
330
|
|
|
|
331
|
|
|
/** |
332
|
|
|
* @param \FeedIo\FeedInterface $feed |
333
|
|
|
* @return \DomDocument |
334
|
|
|
*/ |
335
|
1 |
|
public function toRss(FeedInterface $feed) |
336
|
|
|
{ |
337
|
1 |
|
return $this->format($feed, 'rss'); |
338
|
|
|
} |
339
|
|
|
|
340
|
|
|
/** |
341
|
|
|
* @param \FeedIo\FeedInterface $feed |
342
|
|
|
* @return \DomDocument |
343
|
|
|
*/ |
344
|
1 |
|
public function toAtom(FeedInterface $feed) |
345
|
|
|
{ |
346
|
1 |
|
return $this->format($feed, 'atom'); |
347
|
|
|
} |
348
|
|
|
|
349
|
|
|
/** |
350
|
|
|
* @param string $name |
351
|
|
|
* @return \FeedIo\StandardAbstract |
352
|
|
|
* @throws \OutOfBoundsException |
353
|
|
|
*/ |
354
|
2 |
|
public function getStandard($name) |
355
|
|
|
{ |
356
|
2 |
|
$name = strtolower($name); |
357
|
2 |
|
if (array_key_exists($name, $this->standards)) { |
358
|
1 |
|
return $this->standards[$name]; |
359
|
|
|
} |
360
|
|
|
|
361
|
1 |
|
throw new \OutOfBoundsException("no standard found for $name"); |
362
|
|
|
} |
363
|
|
|
|
364
|
|
|
/** |
365
|
|
|
* @param \FeedIo\FeedInterface $feed |
366
|
|
|
* @param string $message |
367
|
|
|
* @return $this |
368
|
|
|
*/ |
369
|
2 |
|
protected function logAction(FeedInterface $feed, $message) |
370
|
|
|
{ |
371
|
2 |
|
$class = get_class($feed); |
372
|
2 |
|
$this->logger->debug("$message (feed class : $class)"); |
373
|
|
|
|
374
|
2 |
|
return $this; |
375
|
|
|
} |
376
|
|
|
} |
377
|
|
|
|