1 | <?php |
||
72 | class FeedIo |
||
73 | { |
||
74 | |||
75 | /** |
||
76 | * @var \FeedIo\Reader |
||
77 | */ |
||
78 | protected $reader; |
||
79 | |||
80 | /** |
||
81 | * @var \FeedIo\Rule\DateTimeBuilder |
||
82 | */ |
||
83 | protected $dateTimeBuilder; |
||
84 | |||
85 | /** |
||
86 | * @var \Psr\Log\LoggerInterface |
||
87 | */ |
||
88 | protected $logger; |
||
89 | |||
90 | /** |
||
91 | * @var array |
||
92 | */ |
||
93 | protected $standards; |
||
94 | |||
95 | /** |
||
96 | * @var \FeedIo\Reader\FixerSet |
||
97 | */ |
||
98 | protected $fixerSet; |
||
99 | |||
100 | /** |
||
101 | * @param \FeedIo\Adapter\ClientInterface $client |
||
102 | * @param \Psr\Log\LoggerInterface $logger |
||
103 | 8 | */ |
|
104 | public function __construct(ClientInterface $client, LoggerInterface $logger) |
||
105 | 8 | { |
|
106 | 8 | $this->logger = $logger; |
|
107 | 8 | $this->dateTimeBuilder = new DateTimeBuilder(); |
|
108 | 8 | $this->setReader(new Reader($client, $logger)); |
|
109 | 8 | $this->loadCommonStandards(); |
|
110 | 8 | $this->loadFixerSet(); |
|
111 | 8 | } |
|
112 | |||
113 | /** |
||
114 | * Loads main standards (RSS, RDF, Atom) in current object's attributes |
||
115 | * |
||
116 | * @return $this |
||
117 | */ |
||
118 | 8 | protected function loadCommonStandards() |
|
127 | |||
128 | /** |
||
129 | * adds a filter to the reader |
||
130 | * |
||
131 | * @param \FeedIo\FilterInterface $filter |
||
132 | * @return $this |
||
133 | */ |
||
134 | 7 | public function addFilter(FilterInterface $filter) |
|
135 | { |
||
136 | 7 | $this->getReader()->addFilter($filter); |
|
137 | |||
138 | 7 | return $this; |
|
139 | } |
||
140 | |||
141 | /** |
||
142 | * Returns main standards |
||
143 | * |
||
144 | * @return array |
||
145 | */ |
||
146 | 8 | public function getCommonStandards() |
|
147 | { |
||
148 | return array( |
||
149 | 8 | 'atom' => new Atom($this->dateTimeBuilder), |
|
150 | 8 | 'rss' => new Rss($this->dateTimeBuilder), |
|
151 | 8 | 'rdf' => new Rdf($this->dateTimeBuilder), |
|
152 | ); |
||
153 | } |
||
154 | |||
155 | /** |
||
156 | * @param string $name |
||
157 | * @param \FeedIo\StandardAbstract $standard |
||
158 | * @return $this |
||
159 | */ |
||
160 | 8 | public function addStandard($name, StandardAbstract $standard) |
|
170 | |||
171 | /** |
||
172 | * @return \FeedIo\Reader\FixerSet |
||
173 | */ |
||
174 | 1 | public function getFixerSet() |
|
178 | |||
179 | /** |
||
180 | * @return $this |
||
181 | */ |
||
182 | 7 | protected function loadFixerSet() |
|
193 | |||
194 | /** |
||
195 | * @param FixerAbstract $fixer |
||
196 | * @return $this |
||
197 | 7 | */ |
|
198 | public function addFixer(FixerAbstract $fixer) |
||
199 | 7 | { |
|
200 | $fixer->setLogger($this->logger); |
||
201 | 7 | $this->fixerSet->add($fixer); |
|
202 | 7 | ||
203 | return $this; |
||
204 | } |
||
205 | 7 | ||
206 | /** |
||
207 | * @return array |
||
208 | */ |
||
209 | public function getBaseFixers() |
||
210 | { |
||
211 | return array( |
||
212 | 7 | new Reader\Fixer\LastModified(), |
|
213 | new Reader\Fixer\PublicId(), |
||
214 | 7 | ||
215 | 7 | ); |
|
216 | } |
||
217 | 7 | ||
218 | /** |
||
219 | * @return \FeedIo\Rule\DateTimeBuilder |
||
220 | */ |
||
221 | public function getDateTimeBuilder() |
||
222 | { |
||
223 | 7 | return $this->dateTimeBuilder; |
|
224 | } |
||
225 | |||
226 | 7 | /** |
|
227 | 7 | * @return \FeedIo\Reader |
|
228 | */ |
||
229 | public function getReader() |
||
230 | { |
||
231 | return $this->reader; |
||
232 | } |
||
233 | |||
234 | /** |
||
235 | 7 | * @param \FeedIo\Reader |
|
236 | * @return $this |
||
237 | */ |
||
238 | 7 | public function setReader(Reader $reader) |
|
239 | { |
||
240 | $this->reader = $reader; |
||
241 | |||
242 | return $this; |
||
243 | } |
||
244 | |||
245 | 1 | /** |
|
246 | * @param $url |
||
247 | 1 | * @param FeedInterface $feed |
|
248 | * @param \DateTime $modifiedSince |
||
249 | * @return \FeedIo\Reader\Result |
||
250 | */ |
||
251 | public function read($url, FeedInterface $feed = null, \DateTime $modifiedSince = null) |
||
252 | { |
||
253 | 8 | if (is_null($feed)) { |
|
254 | $feed = new Feed(); |
||
255 | 8 | } |
|
256 | |||
257 | if ($modifiedSince instanceof \DateTime) { |
||
258 | $this->addFilter(new ModifiedSince($modifiedSince)); |
||
259 | } |
||
260 | |||
261 | $this->logAction($feed, "read access : $url into a %s instance"); |
||
262 | 8 | $result = $this->reader->read($url, $feed, $modifiedSince); |
|
263 | |||
264 | 8 | $this->fixerSet->correct($result->getFeed()); |
|
265 | |||
266 | 8 | return $result; |
|
267 | } |
||
268 | |||
269 | /** |
||
270 | * @param $url |
||
271 | * @param \DateTime $modifiedSince |
||
272 | * @return \FeedIo\Reader\Result |
||
273 | */ |
||
274 | public function readSince($url, \DateTime $modifiedSince) |
||
278 | 1 | ||
279 | /** |
||
280 | * @param FeedInterface $feed |
||
281 | 1 | * @param string $standard Standard's name |
|
282 | 1 | * @return \DomDocument |
|
283 | */ |
||
284 | 1 | public function format(FeedInterface $feed, $standard) |
|
285 | { |
||
286 | 1 | $this->logAction($feed, "formatting a %s in $standard format"); |
|
287 | |||
288 | $formatter = new Formatter($this->getStandard($standard), $this->logger); |
||
289 | |||
290 | return $formatter->toDom($feed); |
||
291 | } |
||
292 | |||
293 | /** |
||
294 | 1 | * @param \FeedIo\FeedInterface $feed |
|
295 | * @return \DomDocument |
||
296 | 1 | */ |
|
297 | public function toRss(FeedInterface $feed) |
||
298 | { |
||
299 | return $this->format($feed, 'rss'); |
||
300 | } |
||
301 | |||
302 | /** |
||
303 | * @param \FeedIo\FeedInterface $feed |
||
304 | 1 | * @return \DomDocument |
|
305 | */ |
||
306 | 1 | public function toAtom(FeedInterface $feed) |
|
310 | 1 | ||
311 | /** |
||
312 | * @param string $name |
||
313 | * @return \FeedIo\StandardAbstract |
||
314 | * @throws \OutOfBoundsException |
||
315 | */ |
||
316 | public function getStandard($name) |
||
317 | 1 | { |
|
318 | $name = strtolower($name); |
||
325 | |||
326 | 1 | /** |
|
327 | * @param \FeedIo\FeedInterface $feed |
||
328 | 1 | * @param string $message |
|
329 | * @return $this |
||
330 | */ |
||
331 | protected function logAction(FeedInterface $feed, $message) |
||
338 | } |
||
339 |