1 | <?php |
||
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 | public function __construct(ClientInterface $client, LoggerInterface $logger) |
||
103 | { |
||
104 | 8 | $this->logger = $logger; |
|
105 | $this->dateTimeBuilder = new DateTimeBuilder($logger); |
||
106 | 8 | $this->setReader(new Reader($client, $logger)); |
|
107 | 8 | $this->loadCommonStandards(); |
|
108 | 8 | $this->loadFixerSet(); |
|
109 | 8 | } |
|
110 | 8 | ||
111 | 8 | /** |
|
112 | * Loads main standards (RSS, RDF, Atom) in current object's attributes |
||
113 | * |
||
114 | * @return $this |
||
115 | */ |
||
116 | protected function loadCommonStandards() |
||
117 | { |
||
118 | 8 | $standards = $this->getCommonStandards(); |
|
119 | foreach ($standards as $name => $standard) { |
||
120 | 8 | $this->addStandard($name, $standard); |
|
121 | 8 | } |
|
122 | 8 | ||
123 | 8 | return $this; |
|
124 | } |
||
125 | 8 | ||
126 | /** |
||
127 | * adds a filter to the reader |
||
128 | * |
||
129 | * @param \FeedIo\FilterInterface $filter |
||
130 | * @return $this |
||
131 | */ |
||
132 | public function addFilter(FilterInterface $filter) |
||
133 | { |
||
134 | 1 | $this->getReader()->addFilter($filter); |
|
135 | |||
136 | 1 | return $this; |
|
137 | } |
||
138 | 1 | ||
139 | /** |
||
140 | * Returns main standards |
||
141 | * |
||
142 | * @return array |
||
143 | */ |
||
144 | public function getCommonStandards() |
||
145 | { |
||
146 | 8 | $loader = new Loader(); |
|
147 | |||
148 | return $loader->getCommonStandards($this->getDateTimeBuilder()); |
||
149 | 8 | } |
|
150 | 8 | ||
151 | 8 | /** |
|
152 | 8 | * @param string $name |
|
153 | * @param \FeedIo\StandardAbstract $standard |
||
154 | * @return $this |
||
155 | */ |
||
156 | public function addStandard($name, StandardAbstract $standard) |
||
157 | { |
||
158 | $name = strtolower($name); |
||
159 | $this->standards[$name] = $standard; |
||
160 | 8 | $parser = $this->newParser($standard->getSyntaxFormat(), $standard); |
|
161 | $this->reader->addParser($parser); |
||
162 | 8 | ||
163 | 8 | return $this; |
|
164 | 8 | } |
|
165 | 8 | ||
166 | 8 | /** |
|
167 | * @param string $format |
||
168 | 8 | * @param StandardAbstract $standard |
|
169 | * @return object |
||
170 | */ |
||
171 | public function newParser($format, StandardAbstract $standard) |
||
172 | { |
||
173 | $reflection = new \ReflectionClass("FeedIo\\Parser\\{$format}Parser"); |
||
174 | 1 | ||
175 | if ( ! $reflection->isSubclassOf('FeedIo\ParserAbstract') ) { |
||
176 | 1 | throw new \InvalidArgumentException(); |
|
177 | } |
||
178 | |||
179 | return $reflection->newInstanceArgs([$standard, $this->logger]); |
||
180 | } |
||
181 | |||
182 | 7 | /** |
|
183 | * @return \FeedIo\Reader\FixerSet |
||
184 | 7 | */ |
|
185 | 7 | public function getFixerSet() |
|
186 | { |
||
187 | 7 | return $this->fixerSet; |
|
188 | 7 | } |
|
189 | 7 | ||
190 | /** |
||
191 | 7 | * @return $this |
|
192 | */ |
||
193 | protected function loadFixerSet() |
||
194 | { |
||
195 | $this->fixerSet = new FixerSet(); |
||
196 | $fixers = $this->getBaseFixers(); |
||
197 | |||
198 | 7 | foreach ($fixers as $fixer) { |
|
199 | $this->addFixer($fixer); |
||
200 | 7 | } |
|
201 | 7 | ||
202 | return $this; |
||
203 | 7 | } |
|
204 | |||
205 | /** |
||
206 | * @param FixerAbstract $fixer |
||
207 | * @return $this |
||
208 | */ |
||
209 | 7 | public function addFixer(FixerAbstract $fixer) |
|
210 | { |
||
211 | $fixer->setLogger($this->logger); |
||
212 | 7 | $this->fixerSet->add($fixer); |
|
213 | 7 | ||
214 | return $this; |
||
215 | 7 | } |
|
216 | |||
217 | /** |
||
218 | * @return array |
||
219 | */ |
||
220 | public function getBaseFixers() |
||
221 | { |
||
222 | 1 | return array( |
|
223 | new Reader\Fixer\LastModified(), |
||
224 | 1 | new Reader\Fixer\PublicId(), |
|
225 | 1 | ||
226 | 1 | ); |
|
227 | } |
||
228 | 1 | ||
229 | /** |
||
230 | * @param array $formats |
||
231 | * @return $this |
||
232 | */ |
||
233 | public function addDateFormats(array $formats) |
||
234 | 2 | { |
|
235 | foreach( $formats as $format ) { |
||
236 | 2 | $this->getDateTimeBuilder()->addDateFormat($format); |
|
237 | } |
||
238 | |||
239 | return $this; |
||
240 | } |
||
241 | |||
242 | 2 | /** |
|
243 | * @return \FeedIo\Rule\DateTimeBuilder |
||
244 | 2 | */ |
|
245 | public function getDateTimeBuilder() |
||
246 | { |
||
247 | return $this->dateTimeBuilder; |
||
248 | } |
||
249 | |||
250 | /** |
||
251 | 8 | * @return \FeedIo\Reader |
|
252 | */ |
||
253 | 8 | public function getReader() |
|
257 | |||
258 | /** |
||
259 | * @param \FeedIo\Reader |
||
260 | * @return $this |
||
261 | */ |
||
262 | public function setReader(Reader $reader) |
||
268 | 1 | ||
269 | /** |
||
270 | 1 | * @param $url |
|
271 | * @param FeedInterface $feed |
||
272 | * @param \DateTime $modifiedSince |
||
273 | * @return \FeedIo\Reader\Result |
||
274 | 1 | */ |
|
275 | 1 | public function read($url, FeedInterface $feed = null, \DateTime $modifiedSince = null) |
|
276 | { |
||
277 | 1 | if (is_null($feed)) { |
|
278 | $feed = new Feed(); |
||
279 | 1 | } |
|
280 | |||
281 | if ($modifiedSince instanceof \DateTime) { |
||
282 | $this->addFilter(new ModifiedSince($modifiedSince)); |
||
283 | } |
||
284 | |||
285 | $this->logAction($feed, "read access : $url into a feed instance"); |
||
286 | $result = $this->reader->read($url, $feed, $modifiedSince); |
||
287 | 1 | ||
288 | $this->fixerSet->correct($result->getFeed()); |
||
289 | 1 | ||
290 | return $result; |
||
291 | } |
||
292 | |||
293 | /** |
||
294 | * @param $url |
||
295 | * @param \DateTime $modifiedSince |
||
296 | * @return \FeedIo\Reader\Result |
||
297 | */ |
||
298 | public function readSince($url, \DateTime $modifiedSince) |
||
299 | { |
||
300 | return $this->read($url, new Feed(), $modifiedSince); |
||
301 | } |
||
302 | |||
303 | /** |
||
304 | * @return $this |
||
305 | */ |
||
306 | public function resetFilters() |
||
312 | |||
313 | 1 | /** |
|
314 | * @param FeedInterface $feed |
||
315 | * @param string $standard Standard's name |
||
316 | * @return string |
||
317 | */ |
||
318 | public function format(FeedInterface $feed, $standard) |
||
319 | { |
||
320 | 1 | $this->logAction($feed, "formatting a feed in $standard format"); |
|
321 | |||
322 | 1 | $formatter = $this->getStandard($standard)->getFormatter(); |
|
323 | |||
324 | return $formatter->toString($feed); |
||
326 | |||
327 | /** |
||
328 | * @param \FeedIo\FeedInterface $feed |
||
329 | 1 | * @return string |
|
330 | */ |
||
331 | 1 | public function toRss(FeedInterface $feed) |
|
335 | |||
336 | /** |
||
337 | * @param \FeedIo\FeedInterface $feed |
||
338 | * @return string |
||
339 | 2 | */ |
|
340 | public function toAtom(FeedInterface $feed) |
||
344 | |||
345 | /** |
||
346 | 1 | * @param \FeedIo\FeedInterface $feed |
|
347 | * @return string |
||
348 | */ |
||
349 | public function toJson(FeedInterface $feed) |
||
353 | |||
354 | 1 | ||
355 | /** |
||
356 | 1 | * @param string $name |
|
357 | 1 | * @return \FeedIo\StandardAbstract |
|
358 | * @throws \OutOfBoundsException |
||
359 | 1 | */ |
|
360 | public function getStandard($name) |
||
369 | |||
370 | /** |
||
371 | * @param \FeedIo\FeedInterface $feed |
||
372 | * @param string $message |
||
373 | * @return $this |
||
374 | */ |
||
375 | protected function logAction(FeedInterface $feed, $message) |
||
382 | } |
||
383 |