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 | */ |
||
104 | 7 | public function __construct(ClientInterface $client, LoggerInterface $logger) |
|
105 | { |
||
106 | 7 | $this->logger = $logger; |
|
107 | 7 | $this->dateTimeBuilder = new DateTimeBuilder(); |
|
108 | 7 | $this->setReader(new Reader($client, $logger)); |
|
109 | 7 | $this->loadCommonStandards(); |
|
110 | 7 | $this->loadFixerSet(); |
|
111 | 7 | } |
|
112 | |||
113 | /** |
||
114 | * Loads main standards (RSS, RDF, Atom) in current object's attributes |
||
115 | * |
||
116 | * @return $this |
||
117 | */ |
||
118 | 7 | protected function loadCommonStandards() |
|
127 | |||
128 | /** |
||
129 | * adds a filter to the reader |
||
130 | * |
||
131 | * @param \FeedIo\FilterInterface $filter |
||
132 | * @return $this |
||
133 | */ |
||
134 | 1 | public function addFilter(FilterInterface $filter) |
|
135 | { |
||
136 | 1 | $this->getReader()->addFilter($filter); |
|
137 | |||
138 | 1 | return $this; |
|
139 | } |
||
140 | |||
141 | /** |
||
142 | * Returns main standards |
||
143 | * |
||
144 | * @return array |
||
145 | */ |
||
146 | 7 | public function getCommonStandards() |
|
147 | { |
||
148 | return array( |
||
149 | 7 | 'atom' => new Atom($this->dateTimeBuilder), |
|
150 | 7 | 'rss' => new Rss($this->dateTimeBuilder), |
|
151 | 7 | 'rdf' => new Rdf($this->dateTimeBuilder), |
|
152 | ); |
||
153 | } |
||
154 | |||
155 | /** |
||
156 | * @param string $name |
||
157 | * @param \FeedIo\StandardAbstract $standard |
||
158 | * @return $this |
||
159 | */ |
||
160 | 7 | 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 | 6 | protected function loadFixerSet() |
|
193 | |||
194 | /** |
||
195 | * @param FixerAbstract $fixer |
||
196 | * @return $this |
||
197 | */ |
||
198 | 6 | public function addFixer(FixerAbstract $fixer) |
|
205 | |||
206 | /** |
||
207 | * @return array |
||
208 | */ |
||
209 | 6 | public function getBaseFixers() |
|
210 | { |
||
211 | return array( |
||
212 | 6 | new Reader\Fixer\LastModified(), |
|
213 | 6 | new Reader\Fixer\PublicId(), |
|
214 | |||
215 | ); |
||
216 | } |
||
217 | |||
218 | /** |
||
219 | * @return \FeedIo\Rule\DateTimeBuilder |
||
220 | */ |
||
221 | 1 | public function getDateTimeBuilder() |
|
225 | |||
226 | /** |
||
227 | * @return \FeedIo\Reader |
||
228 | */ |
||
229 | 2 | public function getReader() |
|
233 | |||
234 | /** |
||
235 | * @param \FeedIo\Reader |
||
236 | * @return $this |
||
237 | */ |
||
238 | 7 | public function setReader(Reader $reader) |
|
244 | |||
245 | /** |
||
246 | * @param $url |
||
247 | * @param FeedInterface $feed |
||
248 | * @param \DateTime $modifiedSince |
||
249 | * @return \FeedIo\Reader\Result |
||
250 | */ |
||
251 | 1 | public function read($url, FeedInterface $feed = null, \DateTime $modifiedSince = null) |
|
252 | { |
||
253 | 1 | if (is_null($feed)) { |
|
254 | 1 | $feed = new Feed(); |
|
255 | } |
||
256 | |||
257 | 1 | if ($modifiedSince instanceof \DateTime) { |
|
258 | $this->addFilter(new ModifiedSince($modifiedSince)); |
||
259 | } |
||
260 | |||
261 | 1 | $this->logAction($feed, "read access : $url into a %s instance"); |
|
262 | 1 | $result = $this->reader->read($url, $feed, $modifiedSince); |
|
263 | |||
264 | 1 | $this->fixerSet->correct($result->getFeed()); |
|
265 | |||
266 | 1 | return $result; |
|
267 | } |
||
268 | |||
269 | /** |
||
270 | * @param $url |
||
271 | * @param \DateTime $modifiedSince |
||
272 | * @return \FeedIo\Reader\Result |
||
273 | */ |
||
274 | 1 | public function readSince($url, \DateTime $modifiedSince) |
|
278 | |||
279 | /** |
||
280 | * @param FeedInterface $feed |
||
281 | * @param string $standard Standard's name |
||
282 | * @return \DomDocument |
||
283 | */ |
||
284 | 1 | public function format(FeedInterface $feed, $standard) |
|
292 | |||
293 | /** |
||
294 | * @param \FeedIo\FeedInterface $feed |
||
295 | * @return \DomDocument |
||
296 | */ |
||
297 | 1 | public function toRss(FeedInterface $feed) |
|
301 | |||
302 | /** |
||
303 | * @param \FeedIo\FeedInterface $feed |
||
304 | * @return \DomDocument |
||
305 | */ |
||
306 | 1 | public function toAtom(FeedInterface $feed) |
|
310 | |||
311 | /** |
||
312 | * @param string $name |
||
313 | * @return \FeedIo\StandardAbstract |
||
314 | * @throws \OutOfBoundsException |
||
315 | */ |
||
316 | 2 | public function getStandard($name) |
|
325 | |||
326 | /** |
||
327 | * @param \FeedIo\FeedInterface $feed |
||
328 | * @param string $message |
||
329 | * @return $this |
||
330 | */ |
||
331 | 1 | protected function logAction(FeedInterface $feed, $message) |
|
338 | } |
||
339 |