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