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