diff options
author | Thomas Citharel <tcit@tcit.fr> | 2015-10-18 15:49:00 +0200 |
---|---|---|
committer | Nicolas LÅ“uillet <nicolas.loeuillet@smile.fr> | 2015-11-09 16:32:48 +0100 |
commit | b3cc1a14e7b9939fdaf7e71fac40ed7c42727854 (patch) | |
tree | e473e4c70a81167ded9de691a713b83894af9d72 /src/Wallabag/CoreBundle/Helper | |
parent | 33c36f6b482dd512a43b86c0e965bc09a67cf555 (diff) | |
download | wallabag-b3cc1a14e7b9939fdaf7e71fac40ed7c42727854.tar.gz wallabag-b3cc1a14e7b9939fdaf7e71fac40ed7c42727854.tar.zst wallabag-b3cc1a14e7b9939fdaf7e71fac40ed7c42727854.zip |
add json & xml
Diffstat (limited to 'src/Wallabag/CoreBundle/Helper')
-rw-r--r-- | src/Wallabag/CoreBundle/Helper/EntriesExport.php | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/src/Wallabag/CoreBundle/Helper/EntriesExport.php b/src/Wallabag/CoreBundle/Helper/EntriesExport.php index 806319b1..33ff6311 100644 --- a/src/Wallabag/CoreBundle/Helper/EntriesExport.php +++ b/src/Wallabag/CoreBundle/Helper/EntriesExport.php | |||
@@ -5,6 +5,10 @@ namespace Wallabag\CoreBundle\Helper; | |||
5 | use PHPePub\Core\EPub; | 5 | use PHPePub\Core\EPub; |
6 | use PHPePub\Core\Structure\OPF\DublinCore; | 6 | use PHPePub\Core\Structure\OPF\DublinCore; |
7 | use Symfony\Component\HttpFoundation\Response; | 7 | use Symfony\Component\HttpFoundation\Response; |
8 | use Symfony\Component\Serializer\Serializer; | ||
9 | use Symfony\Component\Serializer\Encoder\XmlEncoder; | ||
10 | use Symfony\Component\Serializer\Encoder\JsonEncoder; | ||
11 | use Symfony\Component\Serializer\Normalizer\ObjectNormalizer; | ||
8 | 12 | ||
9 | class EntriesExport | 13 | class EntriesExport |
10 | { | 14 | { |
@@ -86,6 +90,12 @@ class EntriesExport | |||
86 | 90 | ||
87 | case 'csv': | 91 | case 'csv': |
88 | return $this->produceCSV(); | 92 | return $this->produceCSV(); |
93 | |||
94 | case 'json': | ||
95 | return $this->produceJSON(); | ||
96 | |||
97 | case 'xml': | ||
98 | return $this->produceXML(); | ||
89 | } | 99 | } |
90 | 100 | ||
91 | throw new \InvalidArgumentException(sprintf('The format "%s" is not yet supported.', $format)); | 101 | throw new \InvalidArgumentException(sprintf('The format "%s" is not yet supported.', $format)); |
@@ -319,6 +329,51 @@ class EntriesExport | |||
319 | )->send(); | 329 | )->send(); |
320 | } | 330 | } |
321 | 331 | ||
332 | private function produceJSON() | ||
333 | { | ||
334 | $serializer = $this->prepareSerializingContent(); | ||
335 | $jsonContent = $serializer->serialize($this->entries, 'json'); | ||
336 | |||
337 | return Response::create( | ||
338 | $jsonContent, | ||
339 | 200, | ||
340 | array( | ||
341 | 'Content-type' => 'application/json', | ||
342 | 'Content-Disposition' => 'attachment; filename="'.$this->title.'.json"', | ||
343 | 'Content-Transfer-Encoding' => 'UTF-8', | ||
344 | ) | ||
345 | )->send(); | ||
346 | } | ||
347 | |||
348 | private function produceXML() | ||
349 | { | ||
350 | $serializer = $this->prepareSerializingContent(); | ||
351 | $xmlContent = $serializer->serialize($this->entries, 'xml'); | ||
352 | |||
353 | return Response::create( | ||
354 | $xmlContent, | ||
355 | 200, | ||
356 | array( | ||
357 | 'Content-type' => 'application/xml', | ||
358 | 'Content-Disposition' => 'attachment; filename="'.$this->title.'.xml"', | ||
359 | 'Content-Transfer-Encoding' => 'UTF-8', | ||
360 | ) | ||
361 | )->send(); | ||
362 | } | ||
363 | /** | ||
364 | * Return a Serializer object for producing processes that need it (JSON & XML). | ||
365 | * | ||
366 | * @return Serializer | ||
367 | */ | ||
368 | private function prepareSerializingContent() | ||
369 | { | ||
370 | $encoders = array(new XmlEncoder(), new JsonEncoder()); | ||
371 | $normalizers = array(new ObjectNormalizer()); | ||
372 | $normalizers[0]->setIgnoredAttributes(array('user', 'createdAt', 'updatedAt')); | ||
373 | |||
374 | return new Serializer($normalizers, $encoders); | ||
375 | } | ||
376 | |||
322 | /** | 377 | /** |
323 | * Return a kind of footer / information for the epub. | 378 | * Return a kind of footer / information for the epub. |
324 | * | 379 | * |