1 | <?php declare(strict_types=1); |
||
73 | class FeedIo |
||
74 | { |
||
75 | |||
76 | /** |
||
77 | * @var \FeedIo\Reader |
||
78 | */ |
||
79 | protected $reader; |
||
80 | |||
81 | /** |
||
82 | * @var \FeedIo\Rule\DateTimeBuilder |
||
83 | */ |
||
84 | protected $dateTimeBuilder; |
||
85 | |||
86 | /** |
||
87 | * @var \Psr\Log\LoggerInterface |
||
88 | */ |
||
89 | protected $logger; |
||
90 | |||
91 | /** |
||
92 | * @var array |
||
93 | */ |
||
94 | protected $standards; |
||
95 | |||
96 | /** |
||
97 | * @var \FeedIo\Reader\FixerSet |
||
98 | */ |
||
99 | protected $fixerSet; |
||
100 | |||
101 | /** |
||
102 | * @param \FeedIo\Adapter\ClientInterface $client |
||
103 | * @param \Psr\Log\LoggerInterface $logger |
||
104 | */ |
||
105 | 9 | public function __construct(ClientInterface $client, LoggerInterface $logger) |
|
106 | { |
||
107 | 9 | $this->logger = $logger; |
|
108 | 9 | $this->dateTimeBuilder = new DateTimeBuilder($logger); |
|
109 | 9 | $this->setReader(new Reader($client, $logger)); |
|
110 | 9 | $this->loadCommonStandards(); |
|
111 | 9 | $this->loadFixerSet(); |
|
112 | 9 | } |
|
113 | |||
114 | /** |
||
115 | * Loads main standards (RSS, RDF, Atom) in current object's attributes |
||
116 | * |
||
117 | * @return FeedIo |
||
118 | */ |
||
119 | 9 | protected function loadCommonStandards() : FeedIo |
|
120 | { |
||
121 | 9 | $standards = $this->getCommonStandards(); |
|
122 | 9 | foreach ($standards as $name => $standard) { |
|
123 | 9 | $this->addStandard($name, $standard); |
|
124 | } |
||
125 | |||
126 | 9 | return $this; |
|
127 | } |
||
128 | |||
129 | /** |
||
130 | * adds a filter to the reader |
||
131 | * |
||
132 | * @param \FeedIo\FilterInterface $filter |
||
133 | * @return FeedIo |
||
134 | */ |
||
135 | public function addFilter(FilterInterface $filter) : FeedIo |
||
136 | { |
||
137 | $this->getReader()->addFilter($filter); |
||
138 | |||
139 | return $this; |
||
140 | } |
||
141 | |||
142 | /** |
||
143 | * Returns main standards |
||
144 | * |
||
145 | * @return array |
||
146 | */ |
||
147 | 9 | public function getCommonStandards() : array |
|
148 | { |
||
149 | 9 | $loader = new Loader(); |
|
150 | |||
151 | 9 | return $loader->getCommonStandards($this->getDateTimeBuilder()); |
|
152 | } |
||
153 | |||
154 | /** |
||
155 | * @param string $name |
||
156 | * @param \FeedIo\StandardAbstract $standard |
||
157 | * @return FeedIo |
||
158 | */ |
||
159 | 9 | public function addStandard(string $name, StandardAbstract $standard) : FeedIo |
|
160 | { |
||
161 | 9 | $name = strtolower($name); |
|
162 | 9 | $this->standards[$name] = $standard; |
|
163 | 9 | $parser = $this->newParser($standard->getSyntaxFormat(), $standard); |
|
164 | 9 | $this->reader->addParser($parser); |
|
165 | |||
166 | 9 | return $this; |
|
167 | } |
||
168 | |||
169 | /** |
||
170 | * @param string $format |
||
171 | * @param StandardAbstract $standard |
||
172 | * @return ParserAbstract |
||
173 | */ |
||
174 | 8 | public function newParser(string $format, StandardAbstract $standard) : ParserAbstract |
|
175 | { |
||
176 | 8 | $reflection = new \ReflectionClass("FeedIo\\Parser\\{$format}Parser"); |
|
177 | |||
178 | 8 | if (! $reflection->isSubclassOf('FeedIo\ParserAbstract')) { |
|
179 | throw new \InvalidArgumentException(); |
||
180 | } |
||
181 | |||
182 | 8 | return $reflection->newInstanceArgs([$standard, $this->logger]); |
|
183 | } |
||
184 | |||
185 | /** |
||
186 | * @return \FeedIo\Reader\FixerSet |
||
187 | */ |
||
188 | 1 | public function getFixerSet() : FixerSet |
|
189 | { |
||
190 | 1 | return $this->fixerSet; |
|
191 | } |
||
192 | |||
193 | /** |
||
194 | * @return FeedIo |
||
195 | */ |
||
196 | 8 | protected function loadFixerSet() : FeedIo |
|
197 | { |
||
198 | 8 | $this->fixerSet = new FixerSet(); |
|
199 | 8 | $fixers = $this->getBaseFixers(); |
|
200 | |||
201 | 8 | foreach ($fixers as $fixer) { |
|
202 | 8 | $this->addFixer($fixer); |
|
203 | } |
||
204 | |||
205 | 8 | return $this; |
|
206 | } |
||
207 | |||
208 | /** |
||
209 | * @param FixerAbstract $fixer |
||
210 | * @return FeedIo |
||
211 | */ |
||
212 | 8 | public function addFixer(FixerAbstract $fixer) : FeedIo |
|
219 | |||
220 | /** |
||
221 | * @return array |
||
222 | */ |
||
223 | 8 | public function getBaseFixers() : array |
|
224 | { |
||
225 | return array( |
||
226 | 8 | new Reader\Fixer\LastModified(), |
|
227 | 8 | new Reader\Fixer\PublicId(), |
|
230 | |||
231 | /** |
||
232 | * @param array $formats |
||
233 | * @return FeedIo |
||
234 | */ |
||
235 | 1 | public function addDateFormats(array $formats) : FeedIo |
|
243 | |||
244 | /** |
||
245 | * @return \FeedIo\Rule\DateTimeBuilder |
||
246 | */ |
||
247 | 9 | public function getDateTimeBuilder() : DateTimeBuilder |
|
251 | |||
252 | /** |
||
253 | * @return \FeedIo\Reader |
||
254 | */ |
||
255 | 1 | public function getReader() : Reader |
|
259 | |||
260 | /** |
||
261 | * @param \FeedIo\Reader |
||
262 | * @return FeedIo |
||
263 | */ |
||
264 | 9 | public function setReader(Reader $reader) : FeedIo |
|
270 | |||
271 | /** |
||
272 | * @param string $url |
||
273 | * @param FeedInterface $feed |
||
274 | * @param \DateTime $modifiedSince |
||
275 | * @return \FeedIo\Reader\Result |
||
276 | */ |
||
277 | 1 | public function read(string $url, FeedInterface $feed = null, \DateTime $modifiedSince = null) : Result |
|
294 | |||
295 | /** |
||
296 | * @param string $url |
||
297 | * @param \DateTime $modifiedSince |
||
298 | * @return \FeedIo\Reader\Result |
||
299 | */ |
||
300 | 1 | public function readSince(string $url, \DateTime $modifiedSince) : Result |
|
304 | |||
305 | /** |
||
306 | * @return FeedIo |
||
307 | */ |
||
308 | public function resetFilters() : FeedIo |
||
314 | |||
315 | 1 | public function getPsrResponse(FeedInterface $feed, string $standard, int $maxAge = 600, bool $public = true) : ResponseInterface |
|
324 | |||
325 | /** |
||
326 | * @param FeedInterface $feed |
||
327 | * @param string $standard Standard's name |
||
328 | * @return string |
||
329 | */ |
||
330 | 1 | public function format(FeedInterface $feed, string $standard) : string |
|
338 | |||
339 | /** |
||
340 | * @param \FeedIo\FeedInterface $feed |
||
341 | * @return string |
||
342 | */ |
||
343 | 1 | public function toRss(FeedInterface $feed) : string |
|
347 | |||
348 | /** |
||
349 | * @param \FeedIo\FeedInterface $feed |
||
350 | * @return string |
||
351 | */ |
||
352 | 1 | public function toAtom(FeedInterface $feed) : string |
|
356 | |||
357 | /** |
||
358 | * @param \FeedIo\FeedInterface $feed |
||
359 | * @return string |
||
360 | */ |
||
361 | 1 | public function toJson(FeedInterface $feed) : string |
|
365 | |||
366 | |||
367 | /** |
||
368 | * @param string $name |
||
369 | * @return \FeedIo\StandardAbstract |
||
370 | * @throws \OutOfBoundsException |
||
371 | */ |
||
372 | 3 | public function getStandard(string $name) : StandardAbstract |
|
381 | |||
382 | /** |
||
383 | * @param \FeedIo\FeedInterface $feed |
||
384 | * @param string $message |
||
385 | * @return FeedIo |
||
386 | */ |
||
387 | 2 | protected function logAction(FeedInterface $feed, string $message) : FeedIo |
|
394 | } |
||
395 |