1 | <?php declare(strict_types=1); |
||
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 | 11 | public function __construct(ClientInterface $client, LoggerInterface $logger) |
|
110 | |||
111 | /** |
||
112 | * Loads main standards (RSS, RDF, Atom) in current object's attributes |
||
113 | * |
||
114 | * @return FeedIo |
||
115 | */ |
||
116 | 11 | protected function loadCommonStandards() : FeedIo |
|
117 | { |
||
118 | 11 | $standards = $this->getCommonStandards(); |
|
119 | 11 | foreach ($standards as $name => $standard) { |
|
120 | 11 | $this->addStandard($name, $standard); |
|
121 | } |
||
122 | |||
123 | 11 | return $this; |
|
124 | } |
||
125 | |||
126 | /** |
||
127 | * adds a filter to the reader |
||
128 | * |
||
129 | * @param \FeedIo\FilterInterface $filter |
||
130 | * @return FeedIo |
||
131 | */ |
||
132 | 2 | public function addFilter(FilterInterface $filter) : FeedIo |
|
138 | |||
139 | /** |
||
140 | * Returns main standards |
||
141 | * |
||
142 | * @return array |
||
143 | */ |
||
144 | 11 | public function getCommonStandards() : array |
|
150 | |||
151 | /** |
||
152 | * @param string $name |
||
153 | * @param \FeedIo\StandardAbstract $standard |
||
154 | * @return FeedIo |
||
155 | */ |
||
156 | 11 | public function addStandard(string $name, StandardAbstract $standard) : FeedIo |
|
165 | |||
166 | /** |
||
167 | * @param string $format |
||
168 | * @param StandardAbstract $standard |
||
169 | * @return ParserAbstract |
||
170 | */ |
||
171 | 10 | public function newParser(string $format, StandardAbstract $standard) : ParserAbstract |
|
181 | |||
182 | /** |
||
183 | * @return \FeedIo\Reader\FixerSet |
||
184 | */ |
||
185 | 1 | public function getFixerSet() : FixerSet |
|
189 | |||
190 | /** |
||
191 | * @return FeedIo |
||
192 | */ |
||
193 | 10 | protected function loadFixerSet() : FeedIo |
|
194 | { |
||
195 | 10 | $this->fixerSet = new FixerSet(); |
|
196 | 10 | $fixers = $this->getBaseFixers(); |
|
197 | |||
198 | 10 | foreach ($fixers as $fixer) { |
|
199 | 10 | $this->addFixer($fixer); |
|
200 | } |
||
201 | |||
202 | 10 | return $this; |
|
203 | } |
||
204 | |||
205 | /** |
||
206 | * @param FixerAbstract $fixer |
||
207 | * @return FeedIo |
||
208 | */ |
||
209 | 10 | public function addFixer(FixerAbstract $fixer) : FeedIo |
|
216 | |||
217 | /** |
||
218 | * @return array |
||
219 | */ |
||
220 | 10 | public function getBaseFixers() : array |
|
227 | |||
228 | /** |
||
229 | * @param array $formats |
||
230 | * @return FeedIo |
||
231 | */ |
||
232 | 1 | public function addDateFormats(array $formats) : FeedIo |
|
240 | |||
241 | /** |
||
242 | * @return \FeedIo\Rule\DateTimeBuilder |
||
243 | */ |
||
244 | 11 | public function getDateTimeBuilder() : DateTimeBuilder |
|
248 | |||
249 | /** |
||
250 | * @return \FeedIo\Reader |
||
251 | */ |
||
252 | 4 | public function getReader() : Reader |
|
256 | |||
257 | /** |
||
258 | * @param \FeedIo\Reader |
||
259 | * @return FeedIo |
||
260 | */ |
||
261 | 11 | public function setReader(Reader $reader) : FeedIo |
|
267 | |||
268 | /** |
||
269 | * @param string $url |
||
270 | * @param FeedInterface $feed |
||
271 | * @param \DateTime $modifiedSince |
||
272 | * @return \FeedIo\Reader\Result |
||
273 | */ |
||
274 | 2 | public function read(string $url, FeedInterface $feed = null, \DateTime $modifiedSince = null) : Result |
|
275 | { |
||
276 | 2 | if (is_null($feed)) { |
|
277 | 1 | $feed = new Feed(); |
|
278 | } |
||
279 | |||
280 | 2 | if ($modifiedSince instanceof \DateTime) { |
|
281 | 1 | $this->addFilter(new ModifiedSince($modifiedSince)); |
|
282 | } |
||
283 | |||
284 | 2 | $this->logAction($feed, "read access : $url into a feed instance"); |
|
285 | 2 | $result = $this->reader->read($url, $feed, $modifiedSince); |
|
286 | |||
287 | 2 | $this->fixerSet->correct($result->getFeed()); |
|
288 | |||
289 | 2 | return $result; |
|
290 | } |
||
291 | |||
292 | /** |
||
293 | * @param string $url |
||
294 | * @param \DateTime $modifiedSince |
||
295 | * @return \FeedIo\Reader\Result |
||
296 | */ |
||
297 | 1 | public function readSince(string $url, \DateTime $modifiedSince) : Result |
|
301 | |||
302 | /** |
||
303 | * @return FeedIo |
||
304 | */ |
||
305 | 1 | public function resetFilters() : FeedIo |
|
306 | { |
||
307 | 1 | $this->getReader()->resetFilters(); |
|
308 | |||
309 | 1 | return $this; |
|
310 | } |
||
311 | |||
312 | /** |
||
313 | * @param FeedInterface $feed |
||
314 | * @param string $standard Standard's name |
||
315 | * @return string |
||
316 | */ |
||
317 | 1 | public function format(FeedInterface $feed, string $standard) : string |
|
325 | |||
326 | /** |
||
327 | * @param \FeedIo\FeedInterface $feed |
||
328 | * @return string |
||
329 | */ |
||
330 | 1 | public function toRss(FeedInterface $feed) : string |
|
334 | |||
335 | /** |
||
336 | * @param \FeedIo\FeedInterface $feed |
||
337 | * @return string |
||
338 | */ |
||
339 | 1 | public function toAtom(FeedInterface $feed) : string |
|
343 | |||
344 | /** |
||
345 | * @param \FeedIo\FeedInterface $feed |
||
346 | * @return string |
||
347 | */ |
||
348 | 1 | public function toJson(FeedInterface $feed) : string |
|
352 | |||
353 | |||
354 | /** |
||
355 | * @param string $name |
||
356 | * @return \FeedIo\StandardAbstract |
||
357 | * @throws \OutOfBoundsException |
||
358 | */ |
||
359 | 2 | public function getStandard(string $name) : StandardAbstract |
|
368 | |||
369 | /** |
||
370 | * @param \FeedIo\FeedInterface $feed |
||
371 | * @param string $message |
||
372 | * @return FeedIo |
||
373 | */ |
||
374 | 2 | protected function logAction(FeedInterface $feed, string $message) : FeedIo |
|
381 | } |
||
382 |