1 | <?php declare(strict_types=1); |
||
75 | class FeedIo |
||
76 | { |
||
77 | |||
78 | /** |
||
79 | * @var \FeedIo\Reader |
||
80 | */ |
||
81 | protected $reader; |
||
82 | |||
83 | /** |
||
84 | * @var \FeedIo\Rule\DateTimeBuilder |
||
85 | */ |
||
86 | protected $dateTimeBuilder; |
||
87 | |||
88 | /** |
||
89 | * @var \Psr\Log\LoggerInterface |
||
90 | */ |
||
91 | protected $logger; |
||
92 | |||
93 | /** |
||
94 | * @var array |
||
95 | */ |
||
96 | protected $standards; |
||
97 | |||
98 | /** |
||
99 | * @var \FeedIo\Reader\FixerSet |
||
100 | */ |
||
101 | protected $fixerSet; |
||
102 | |||
103 | /** |
||
104 | * @param \FeedIo\Adapter\ClientInterface $client |
||
105 | * @param \Psr\Log\LoggerInterface $logger |
||
106 | */ |
||
107 | 12 | public function __construct(ClientInterface $client, LoggerInterface $logger) |
|
108 | { |
||
109 | 12 | $this->logger = $logger; |
|
110 | 12 | $this->dateTimeBuilder = new DateTimeBuilder($logger); |
|
111 | 12 | $this->setReader(new Reader($client, $logger)); |
|
112 | 12 | $this->loadCommonStandards(); |
|
113 | 12 | $this->loadFixerSet(); |
|
114 | 12 | } |
|
115 | |||
116 | /** |
||
117 | * Loads main standards (RSS, RDF, Atom) in current object's attributes |
||
118 | * |
||
119 | * @return FeedIo |
||
120 | */ |
||
121 | 12 | protected function loadCommonStandards() : FeedIo |
|
122 | { |
||
123 | 12 | $standards = $this->getCommonStandards(); |
|
124 | 12 | foreach ($standards as $name => $standard) { |
|
125 | 12 | $this->addStandard($name, $standard); |
|
126 | } |
||
127 | |||
128 | 12 | return $this; |
|
129 | } |
||
130 | |||
131 | /** |
||
132 | * adds a filter to the reader |
||
133 | * |
||
134 | * @param \FeedIo\FilterInterface $filter |
||
135 | * @return FeedIo |
||
136 | */ |
||
137 | 2 | public function addFilter(FilterInterface $filter) : FeedIo |
|
138 | { |
||
139 | 2 | $this->getReader()->addFilter($filter); |
|
140 | |||
141 | 2 | return $this; |
|
142 | } |
||
143 | |||
144 | /** |
||
145 | * Returns main standards |
||
146 | * |
||
147 | * @return array |
||
148 | */ |
||
149 | 12 | public function getCommonStandards() : array |
|
150 | { |
||
151 | 12 | $loader = new Loader(); |
|
152 | |||
153 | 12 | return $loader->getCommonStandards($this->getDateTimeBuilder()); |
|
154 | } |
||
155 | |||
156 | /** |
||
157 | * @param string $name |
||
158 | * @param \FeedIo\StandardAbstract $standard |
||
159 | * @return FeedIo |
||
160 | */ |
||
161 | 12 | public function addStandard(string $name, StandardAbstract $standard) : FeedIo |
|
162 | { |
||
163 | 12 | $name = strtolower($name); |
|
164 | 12 | $this->standards[$name] = $standard; |
|
165 | 12 | $parser = $this->newParser($standard->getSyntaxFormat(), $standard); |
|
166 | 12 | $this->reader->addParser($parser); |
|
167 | |||
168 | 12 | return $this; |
|
169 | } |
||
170 | |||
171 | /** |
||
172 | * @param string $format |
||
173 | * @param StandardAbstract $standard |
||
174 | * @return ParserAbstract |
||
175 | */ |
||
176 | 11 | public function newParser(string $format, StandardAbstract $standard) : ParserAbstract |
|
177 | { |
||
178 | 11 | $reflection = new \ReflectionClass("FeedIo\\Parser\\{$format}Parser"); |
|
179 | |||
180 | 11 | if (! $reflection->isSubclassOf('FeedIo\ParserAbstract')) { |
|
181 | throw new \InvalidArgumentException(); |
||
182 | } |
||
183 | |||
184 | 11 | return $reflection->newInstanceArgs([$standard, $this->logger]); |
|
185 | } |
||
186 | |||
187 | /** |
||
188 | * @return \FeedIo\Reader\FixerSet |
||
189 | */ |
||
190 | 1 | public function getFixerSet() : FixerSet |
|
191 | { |
||
192 | 1 | return $this->fixerSet; |
|
193 | } |
||
194 | |||
195 | /** |
||
196 | * @return FeedIo |
||
197 | */ |
||
198 | 11 | protected function loadFixerSet() : FeedIo |
|
199 | { |
||
200 | 11 | $this->fixerSet = new FixerSet(); |
|
201 | 11 | $fixers = $this->getBaseFixers(); |
|
202 | |||
203 | 11 | foreach ($fixers as $fixer) { |
|
204 | 11 | $this->addFixer($fixer); |
|
205 | } |
||
206 | |||
207 | 11 | return $this; |
|
208 | } |
||
209 | |||
210 | /** |
||
211 | * @param FixerAbstract $fixer |
||
212 | * @return FeedIo |
||
213 | */ |
||
214 | 11 | public function addFixer(FixerAbstract $fixer) : FeedIo |
|
215 | { |
||
216 | 11 | $fixer->setLogger($this->logger); |
|
217 | 11 | $this->fixerSet->add($fixer); |
|
218 | |||
219 | 11 | return $this; |
|
220 | } |
||
221 | |||
222 | /** |
||
223 | * @return array |
||
224 | */ |
||
225 | 11 | public function getBaseFixers() : array |
|
226 | { |
||
227 | return array( |
||
228 | 11 | new Reader\Fixer\LastModified(), |
|
229 | 11 | new Reader\Fixer\PublicId(), |
|
230 | ); |
||
231 | } |
||
232 | |||
233 | /** |
||
234 | * @param array $formats |
||
235 | * @return FeedIo |
||
236 | */ |
||
237 | 1 | public function addDateFormats(array $formats) : FeedIo |
|
238 | { |
||
239 | 1 | foreach ($formats as $format) { |
|
240 | 1 | $this->getDateTimeBuilder()->addDateFormat($format); |
|
241 | } |
||
242 | |||
243 | 1 | return $this; |
|
244 | } |
||
245 | |||
246 | /** |
||
247 | * @return \FeedIo\Rule\DateTimeBuilder |
||
248 | */ |
||
249 | 12 | public function getDateTimeBuilder() : DateTimeBuilder |
|
253 | |||
254 | /** |
||
255 | * @return \FeedIo\Reader |
||
256 | */ |
||
257 | 4 | public function getReader() : Reader |
|
261 | |||
262 | /** |
||
263 | * @param \FeedIo\Reader $reader |
||
264 | * @return FeedIo |
||
265 | */ |
||
266 | 12 | public function setReader(Reader $reader) : FeedIo |
|
272 | |||
273 | /** |
||
274 | * @param iterable $requests |
||
275 | * @param CallbackInterface $callback |
||
276 | * @param string $feedClass |
||
277 | */ |
||
278 | public function readAsync(iterable $requests, CallbackInterface $callback, string $feedClass = '\FeedIo\Feed') : void |
||
279 | { |
||
280 | $reader = new AsyncReader($this->reader, $callback, $feedClass); |
||
281 | |||
282 | $reader->process($requests); |
||
283 | } |
||
284 | |||
285 | /** |
||
286 | * @param string $url |
||
287 | * @param FeedInterface $feed |
||
288 | * @param \DateTime $modifiedSince |
||
289 | * @return \FeedIo\Reader\Result |
||
290 | */ |
||
291 | 2 | public function read(string $url, FeedInterface $feed = null, \DateTime $modifiedSince = null) : Result |
|
308 | |||
309 | /** |
||
310 | * @param string $url |
||
311 | * @param \DateTime $modifiedSince |
||
312 | * @return \FeedIo\Reader\Result |
||
313 | */ |
||
314 | 1 | public function readSince(string $url, \DateTime $modifiedSince) : Result |
|
318 | |||
319 | /** |
||
320 | * @return FeedIo |
||
321 | */ |
||
322 | 1 | public function resetFilters() : FeedIo |
|
328 | |||
329 | /** |
||
330 | * Get a PSR-7 compliant response for the given feed |
||
331 | * |
||
332 | * @param \FeedIo\FeedInterface $feed |
||
333 | * @param string $standard |
||
334 | * @param int $maxAge |
||
335 | * @param bool $public |
||
336 | * @return ResponseInterface |
||
337 | */ |
||
338 | 1 | public function getPsrResponse(FeedInterface $feed, string $standard, int $maxAge = 600, bool $public = true) : ResponseInterface |
|
347 | |||
348 | /** |
||
349 | * @param FeedInterface $feed |
||
350 | * @param string $standard Standard's name |
||
351 | * @return string |
||
352 | */ |
||
353 | 1 | public function format(FeedInterface $feed, string $standard) : string |
|
361 | |||
362 | /** |
||
363 | * @param \FeedIo\FeedInterface $feed |
||
364 | * @return string |
||
365 | */ |
||
366 | 1 | public function toRss(FeedInterface $feed) : string |
|
370 | |||
371 | /** |
||
372 | * @param \FeedIo\FeedInterface $feed |
||
373 | * @return string |
||
374 | */ |
||
375 | 1 | public function toAtom(FeedInterface $feed) : string |
|
379 | |||
380 | /** |
||
381 | * @param \FeedIo\FeedInterface $feed |
||
382 | * @return string |
||
383 | */ |
||
384 | 1 | public function toJson(FeedInterface $feed) : string |
|
388 | |||
389 | |||
390 | /** |
||
391 | * @param string $name |
||
392 | * @return \FeedIo\StandardAbstract |
||
393 | * @throws \OutOfBoundsException |
||
394 | */ |
||
395 | 3 | public function getStandard(string $name) : StandardAbstract |
|
404 | |||
405 | /** |
||
406 | * @param \FeedIo\FeedInterface $feed |
||
407 | * @param string $message |
||
408 | * @return FeedIo |
||
409 | */ |
||
410 | 3 | protected function logAction(FeedInterface $feed, string $message) : FeedIo |
|
417 | } |
||
418 |