1 | <?php |
||
74 | class FeedIo |
||
75 | { |
||
76 | |||
77 | /** |
||
78 | * @var \FeedIo\Reader |
||
79 | */ |
||
80 | protected $reader; |
||
81 | |||
82 | /** |
||
83 | * @var \FeedIo\Rule\DateTimeBuilder |
||
84 | */ |
||
85 | protected $dateTimeBuilder; |
||
86 | |||
87 | /** |
||
88 | * @var \Psr\Log\LoggerInterface |
||
89 | */ |
||
90 | protected $logger; |
||
91 | |||
92 | /** |
||
93 | * @var array |
||
94 | */ |
||
95 | protected $standards; |
||
96 | |||
97 | /** |
||
98 | * @var \FeedIo\Reader\FixerSet |
||
99 | */ |
||
100 | protected $fixerSet; |
||
101 | |||
102 | /** |
||
103 | * @param \FeedIo\Adapter\ClientInterface $client |
||
104 | * @param \Psr\Log\LoggerInterface $logger |
||
105 | */ |
||
106 | 10 | public function __construct(ClientInterface $client, LoggerInterface $logger) |
|
107 | { |
||
108 | 10 | $this->logger = $logger; |
|
109 | 10 | $this->dateTimeBuilder = new DateTimeBuilder($logger); |
|
110 | 10 | $this->setReader(new Reader($client, $logger)); |
|
111 | 10 | $this->loadCommonStandards(); |
|
112 | 10 | $this->loadFixerSet(); |
|
113 | 10 | } |
|
114 | |||
115 | /** |
||
116 | * Loads main standards (RSS, RDF, Atom) in current object's attributes |
||
117 | * |
||
118 | * @return $this |
||
119 | */ |
||
120 | 10 | protected function loadCommonStandards() |
|
121 | { |
||
122 | 10 | $standards = $this->getCommonStandards(); |
|
123 | 10 | foreach ($standards as $name => $standard) { |
|
124 | 10 | $this->addStandard($name, $standard); |
|
125 | 10 | } |
|
126 | |||
127 | 10 | return $this; |
|
128 | } |
||
129 | |||
130 | /** |
||
131 | * adds a filter to the reader |
||
132 | * |
||
133 | * @param \FeedIo\FilterInterface $filter |
||
134 | * @return $this |
||
135 | */ |
||
136 | 2 | public function addFilter(FilterInterface $filter) |
|
137 | { |
||
138 | 2 | $this->getReader()->addFilter($filter); |
|
139 | |||
140 | 2 | return $this; |
|
141 | } |
||
142 | |||
143 | /** |
||
144 | * Returns main standards |
||
145 | * |
||
146 | * @return array |
||
147 | */ |
||
148 | 10 | public function getCommonStandards() |
|
149 | { |
||
150 | 10 | $loader = new Loader(); |
|
151 | |||
152 | 10 | return $loader->getCommonStandards($this->getDateTimeBuilder()); |
|
153 | } |
||
154 | |||
155 | /** |
||
156 | * @param string $name |
||
157 | * @param \FeedIo\StandardAbstract $standard |
||
158 | * @return $this |
||
159 | */ |
||
160 | 10 | public function addStandard($name, StandardAbstract $standard) |
|
169 | |||
170 | /** |
||
171 | * @param string $format |
||
172 | * @param StandardAbstract $standard |
||
173 | * @return object |
||
174 | */ |
||
175 | 9 | public function newParser($format, StandardAbstract $standard) |
|
185 | |||
186 | /** |
||
187 | * @return \FeedIo\Reader\FixerSet |
||
188 | */ |
||
189 | 1 | public function getFixerSet() |
|
193 | |||
194 | /** |
||
195 | * @return $this |
||
196 | */ |
||
197 | 9 | protected function loadFixerSet() |
|
208 | |||
209 | /** |
||
210 | * @param FixerAbstract $fixer |
||
211 | * @return $this |
||
212 | */ |
||
213 | 9 | public function addFixer(FixerAbstract $fixer) |
|
220 | |||
221 | /** |
||
222 | * @return array |
||
223 | */ |
||
224 | 9 | public function getBaseFixers() |
|
232 | |||
233 | /** |
||
234 | * @param array $formats |
||
235 | * @return $this |
||
236 | */ |
||
237 | 1 | public function addDateFormats(array $formats) |
|
245 | |||
246 | /** |
||
247 | * @return \FeedIo\Rule\DateTimeBuilder |
||
248 | */ |
||
249 | 10 | public function getDateTimeBuilder() |
|
253 | |||
254 | /** |
||
255 | * @return \FeedIo\Reader |
||
256 | */ |
||
257 | 4 | public function getReader() |
|
261 | |||
262 | /** |
||
263 | * @param \FeedIo\Reader |
||
264 | * @return $this |
||
265 | */ |
||
266 | 10 | public function setReader(Reader $reader) |
|
272 | |||
273 | /** |
||
274 | * @param $url |
||
275 | * @param FeedInterface $feed |
||
276 | * @param \DateTime $modifiedSince |
||
277 | * @return \FeedIo\Reader\Result |
||
278 | */ |
||
279 | 2 | public function read($url, FeedInterface $feed = null, \DateTime $modifiedSince = null) |
|
280 | { |
||
281 | 2 | if (is_null($feed)) { |
|
282 | 1 | $feed = new Feed(); |
|
283 | 1 | } |
|
284 | |||
285 | 2 | if ($modifiedSince instanceof \DateTime) { |
|
286 | 1 | $this->addFilter(new ModifiedSince($modifiedSince)); |
|
287 | 1 | } |
|
288 | |||
289 | 2 | $this->logAction($feed, "read access : $url into a feed instance"); |
|
290 | 2 | $result = $this->reader->read($url, $feed, $modifiedSince); |
|
291 | |||
292 | 2 | $this->fixerSet->correct($result->getFeed()); |
|
293 | |||
294 | 2 | return $result; |
|
295 | } |
||
296 | |||
297 | /** |
||
298 | * @param $url |
||
299 | * @param \DateTime $modifiedSince |
||
300 | * @return \FeedIo\Reader\Result |
||
301 | */ |
||
302 | 1 | public function readSince($url, \DateTime $modifiedSince) |
|
306 | |||
307 | /** |
||
308 | * @return $this |
||
309 | */ |
||
310 | 1 | public function resetFilters() |
|
311 | { |
||
312 | 1 | $this->getReader()->resetFilters(); |
|
313 | |||
314 | 1 | return $this; |
|
315 | } |
||
316 | |||
317 | /** |
||
318 | * @param FeedInterface $feed |
||
319 | * @param string $standard Standard's name |
||
320 | * @return \DomDocument |
||
321 | */ |
||
322 | 1 | public function format(FeedInterface $feed, $standard) |
|
330 | |||
331 | /** |
||
332 | * @param \FeedIo\FeedInterface $feed |
||
333 | * @return \DomDocument |
||
334 | */ |
||
335 | 1 | public function toRss(FeedInterface $feed) |
|
339 | |||
340 | /** |
||
341 | * @param \FeedIo\FeedInterface $feed |
||
342 | * @return \DomDocument |
||
343 | */ |
||
344 | 1 | public function toAtom(FeedInterface $feed) |
|
348 | |||
349 | /** |
||
350 | * @param string $name |
||
351 | * @return \FeedIo\StandardAbstract |
||
352 | * @throws \OutOfBoundsException |
||
353 | */ |
||
354 | 2 | public function getStandard($name) |
|
363 | |||
364 | /** |
||
365 | * @param \FeedIo\FeedInterface $feed |
||
366 | * @param string $message |
||
367 | * @return $this |
||
368 | */ |
||
369 | 2 | protected function logAction(FeedInterface $feed, $message) |
|
376 | } |
||
377 |