-
Notifications
You must be signed in to change notification settings - Fork 54
/
FeedIo.php
252 lines (224 loc) · 6.54 KB
/
FeedIo.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
<?php
declare(strict_types=1);
namespace FeedIo;
use DateTime;
use FeedIo\Adapter\ClientInterface;
use FeedIo\Adapter\NullClient;
use FeedIo\Http\ResponseBuilder;
use FeedIo\Reader\Result;
use FeedIo\Rule\DateTimeBuilderInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
/**
* This class acts as a facade. It provides methods to access feed-io main features
*
* <code>
* // $client is a \FeedIo\Adapter\ClientInterface, $logger a \Psr\Log\LoggerInterface
* $feedIo = new FeedIo($client, $logger);
*
* // read a feed. Output is a Result instance
* $result = $feedIo->read('http://somefeed.org/feed.rss');
*
* // use the feed
* $feed = $result->getFeed();
* echo $feed->getTitle();
*
* // and its items
* foreach ( $feed as $item ) {
* echo $item->getTitle();
* echo $item->getDescription();
* }
*
* </code>
*
* <code>
* // build the feed to publish
* $feed = new \FeedIo\Feed;
* $feed->setTitle('title');
* // ...
*
* // add items to it
* $item = new \FeedIo\Feed\Item
* $item->setTitle('my great post');
*
* // want to publish a media ? no problem
* $media = new \FeedIo\Feed\Item\Media
* $media->setUrl('http://yourdomain.tld/medias/some-podcast.mp3');
* $media->setType('audio/mpeg');
*
* // add it to the item
* $item->addMedia($media);
*
* // add the item to the feed (almost there)
* $feed->add($item);
*
* // format it in atom
* $feedIo->toAtom($feed);
* </code>
*
*/
class FeedIo
{
protected Reader $reader;
public function __construct(
protected ClientInterface $client = new NullClient(),
protected LoggerInterface $logger = new NullLogger(),
protected ?SpecificationInterface $specification = null,
) {
if (is_null($this->specification)) {
$this->specification = new Specification($this->logger);
}
$this->reader = new Reader($this->client, $this->logger);
foreach ($this->specification->getAllStandards() as $standard) {
$parser = $this->specification->newParser($standard->getSyntaxFormat(), $standard);
$this->reader->addParser($parser);
}
}
/**
* Adds a standard to the Specification.
*
* @see Specification
* @param string $name
* @param StandardAbstract $standard
* @return $this
*/
public function addStandard(string $name, StandardAbstract $standard): self
{
$this->specification->addStandard($name, $standard);
return $this;
}
/**
* Discover feeds from the webpage's headers
*
* @param string $url
* @return array
*/
public function discover(string $url): array
{
$explorer = new Explorer($this->client, $this->logger);
return $explorer->discover($url);
}
/**
* Read the feed hosted at `$url` and populate the `$feed` accordingly.
* `$modifiedSince` is used to return an empty result if the HTTP's response is 'not modified'
*
* @param string $url
* @param FeedInterface|null $feed
* @param DateTime|null $modifiedSince
* @return Result
*/
public function read(string $url, FeedInterface $feed = null, DateTime $modifiedSince = null): Result
{
if (is_null($feed)) {
$feed = new Feed();
}
$this->logAction($feed, "read access : $url into a feed instance");
$result = $this->reader->read($url, $feed, $modifiedSince);
$this->specification->getFixerSet()->correct($result);
return $result;
}
/**
* Get a PSR-7 compliant response for the given feed
*
* @param FeedInterface $feed
* @param string $standard
* @param int $maxAge
* @param bool $public
* @return ResponseInterface
*/
public function getPsrResponse(FeedInterface $feed, string $standard, int $maxAge = 600, bool $public = true): ResponseInterface
{
$this->logAction($feed, "creating a PSR 7 Response in $standard format");
$feedStandard = $this->specification->getStandard($standard);
$responseBuilder = new ResponseBuilder($maxAge, $public);
return $responseBuilder->createResponse($feedStandard->getMimeType(), $feedStandard->getFormatter(), $feed);
}
/**
* Return the feed in the XML or JSON format according to the `$standard` value (can be "rss", "atom" or "json").
*
* @param FeedInterface $feed
* @param string $standard Standard's name
* @return string
*/
public function format(FeedInterface $feed, string $standard): string
{
$this->logAction($feed, "formatting a feed in $standard format");
$formatter = $this->specification->getStandard($standard)->getFormatter();
return $formatter->toString($feed);
}
/**
* Convert to RSS format
*
* @param FeedInterface $feed
* @return string
*/
public function toRss(FeedInterface $feed): string
{
return $this->format($feed, 'rss');
}
/**
* Convert to Atom
*
* @param FeedInterface $feed
* @return string
*/
public function toAtom(FeedInterface $feed): string
{
return $this->format($feed, 'atom');
}
/**
* Convert to JSON Feed
*
* @param FeedInterface $feed
* @return string
*/
public function toJson(FeedInterface $feed): string
{
return $this->format($feed, 'json');
}
/**
* Add a new item to the list of the crooked date formats supported
*
* @param array $formats
* @return $this
*/
public function addDateFormats(array $formats): FeedIo
{
foreach ($formats as $format) {
$this->getDateTimeBuilder()->addDateFormat($format);
}
return $this;
}
/**
* Get a direct access to the DateTime builder
*
* @return DateTimeBuilderInterface
*/
public function getDateTimeBuilder(): DateTimeBuilderInterface
{
return $this->specification->getDateTimeBuilder();
}
/**
* Get the Reader used by feed-io
*
* @return Reader
*/
public function getReader(): Reader
{
return $this->reader;
}
/**
* Log what just happened
*
* @param FeedInterface $feed
* @param string $message
* @return $this
*/
protected function logAction(FeedInterface $feed, string $message): FeedIo
{
$class = get_class($feed);
$this->logger->debug("$message (feed class : $class)");
return $this;
}
}