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