From 8f44a049899a6f4d6313d31145e0f6939991d963 Mon Sep 17 00:00:00 2001 From: Alexey Skobkin Date: Tue, 22 Mar 2016 22:57:18 +0300 Subject: [PATCH] PostsPage deserialization via JMS Serializer. --- .../Controller/Api/CrawlerController.php | 6 ++-- .../DTO/Api/Crawler/MetaPost.php | 14 +++++++-- .../PointToolsBundle/DTO/Api/Crawler/Post.php | 30 +++++++++++++------ .../DTO/Api/Crawler/PostsPage.php | 17 +++++++---- .../DTO/Api/Crawler/Recommendation.php | 28 ++++++++++------- .../PointToolsBundle/DTO/Api/Crawler/User.php | 15 +++++++--- 6 files changed, 76 insertions(+), 34 deletions(-) diff --git a/src/Skobkin/Bundle/PointToolsBundle/Controller/Api/CrawlerController.php b/src/Skobkin/Bundle/PointToolsBundle/Controller/Api/CrawlerController.php index d887e14..fba2400 100644 --- a/src/Skobkin/Bundle/PointToolsBundle/Controller/Api/CrawlerController.php +++ b/src/Skobkin/Bundle/PointToolsBundle/Controller/Api/CrawlerController.php @@ -11,12 +11,12 @@ class CrawlerController extends AbstractApiController $token = $request->request->get('token'); $json = $request->request->get('json'); - $serializer = $this->get('serializer'); + $serializer = $this->get('jms_serializer'); - $data = $serializer->deserialize($json, 'Skobkin\Bundle\PointToolsBundle\DTO\Api\Crawler\PostsPage', 'json', ['groups' => ['import_post_page']]); + $page = $serializer->deserialize($json, 'Skobkin\Bundle\PointToolsBundle\DTO\Api\Crawler\PostsPage', 'json'); return $this->createSuccessResponse([ - 'continue' => true, + 'continue' => false, ]); } } diff --git a/src/Skobkin/Bundle/PointToolsBundle/DTO/Api/Crawler/MetaPost.php b/src/Skobkin/Bundle/PointToolsBundle/DTO/Api/Crawler/MetaPost.php index c885785..1467c12 100644 --- a/src/Skobkin/Bundle/PointToolsBundle/DTO/Api/Crawler/MetaPost.php +++ b/src/Skobkin/Bundle/PointToolsBundle/DTO/Api/Crawler/MetaPost.php @@ -2,21 +2,29 @@ namespace Skobkin\Bundle\PointToolsBundle\DTO\Api\Crawler; -use Symfony\Component\Serializer\Annotation as Serializer; +use JMS\Serializer\Annotation as JMSS; +/** + * @JMSS\ExclusionPolicy("none") + * @JMSS\AccessType("public_method") + */ class MetaPost { /** * @var Recommendation * - * @Serializer\Groups({"import_post_page"}) + * @JMSS\SerializedName("rec") + * @JMSS\Type("Skobkin\Bundle\PointToolsBundle\DTO\Api\Crawler\Recommendation") + * @JMSS\MaxDepth(2) */ private $rec; /** * @var Post * - * @Serializer\Groups({"import_post_page"}) + * @JMSS\SerializedName("post") + * @JMSS\Type("Skobkin\Bundle\PointToolsBundle\DTO\Api\Crawler\Post") + * @JMSS\MaxDepth(2) */ private $post; diff --git a/src/Skobkin/Bundle/PointToolsBundle/DTO/Api/Crawler/Post.php b/src/Skobkin/Bundle/PointToolsBundle/DTO/Api/Crawler/Post.php index cd7fd8b..0e70096 100644 --- a/src/Skobkin/Bundle/PointToolsBundle/DTO/Api/Crawler/Post.php +++ b/src/Skobkin/Bundle/PointToolsBundle/DTO/Api/Crawler/Post.php @@ -2,56 +2,68 @@ namespace Skobkin\Bundle\PointToolsBundle\DTO\Api\Crawler; -use Symfony\Component\Serializer\Annotation as Serializer; +use JMS\Serializer\Annotation as JMSS; +/** + * @JMSS\ExclusionPolicy("none") + * @JMSS\AccessType("public_method") + */ class Post { /** * @var string * - * @Serializer\Groups({"import_post_page"}) + * @JMSS\SerializedName("id") + * @JMSS\Type("string") */ private $id; /** * @var string[] * - * @Serializer\Groups({"import_post_page"}) + * @JMSS\SerializedName("tags") + * @JMSS\Type("array") */ private $tags; /** * @var User * - * @Serializer\Groups({"import_post_page"}) + * @JMSS\SerializedName("author") + * @JMSS\Type("Skobkin\Bundle\PointToolsBundle\DTO\Api\Crawler\User") + * @JMSS\MaxDepth(1) */ private $author; /** * @var string * - * @Serializer\Groups({"import_post_page"}) + * @JMSS\SerializedName("text") + * @JMSS\Type("string") */ private $text; /** * @var string * - * @Serializer\Groups({"import_post_page"}) + * @JMSS\SerializedName("created") + * @JMSS\Type("string") */ private $created; /** * @var string * - * @Serializer\Groups({"import_post_page"}) + * @JMSS\SerializedName("type") + * @JMSS\Type("string") */ private $type; /** * @var bool * - * @Serializer\Groups({"import_post_page"}) + * @JMSS\SerializedName("private") + * @JMSS\Type("boolean") */ private $private; @@ -86,7 +98,7 @@ class Post * @param string[] $tags * @return Post */ - public function setTags(array $tags) + public function setTags($tags) { $this->tags = $tags; return $this; diff --git a/src/Skobkin/Bundle/PointToolsBundle/DTO/Api/Crawler/PostsPage.php b/src/Skobkin/Bundle/PointToolsBundle/DTO/Api/Crawler/PostsPage.php index 6096bd6..1dda918 100644 --- a/src/Skobkin/Bundle/PointToolsBundle/DTO/Api/Crawler/PostsPage.php +++ b/src/Skobkin/Bundle/PointToolsBundle/DTO/Api/Crawler/PostsPage.php @@ -2,19 +2,26 @@ namespace Skobkin\Bundle\PointToolsBundle\DTO\Api\Crawler; -use Symfony\Component\Serializer\Annotation as Serializer; +use Doctrine\Common\Collections\ArrayCollection; +use JMS\Serializer\Annotation as JMSS; +/** + * @JMSS\ExclusionPolicy("none") + * @JMSS\AccessType("public_method") + */ class PostsPage { /** * @var MetaPost[] * - * @Serializer\Groups({"import_post_page"}) + * @JMSS\SerializedName("posts") + * @JMSS\Type("array") + * @JMSS\MaxDepth(3) */ private $posts; /** - * @return MetaPost[] + * @return MetaPost[]|ArrayCollection */ public function getPosts() { @@ -22,10 +29,10 @@ class PostsPage } /** - * @param MetaPost[] $posts + * @param MetaPost[]|ArrayCollection $posts * @return PostsPage */ - public function setPosts(array $posts) + public function setPosts($posts) { $this->posts = $posts; return $this; diff --git a/src/Skobkin/Bundle/PointToolsBundle/DTO/Api/Crawler/Recommendation.php b/src/Skobkin/Bundle/PointToolsBundle/DTO/Api/Crawler/Recommendation.php index 087cc01..3c267db 100644 --- a/src/Skobkin/Bundle/PointToolsBundle/DTO/Api/Crawler/Recommendation.php +++ b/src/Skobkin/Bundle/PointToolsBundle/DTO/Api/Crawler/Recommendation.php @@ -2,28 +2,36 @@ namespace Skobkin\Bundle\PointToolsBundle\DTO\Api\Crawler; -use Symfony\Component\Serializer\Annotation as Serializer; +use JMS\Serializer\Annotation as JMSS; +/** + * @JMSS\ExclusionPolicy("none") + * @JMSS\AccessType("public_method") + */ class Recommendation { /** * @var string * - * @Serializer\Groups({"import_post_page"}) + * @JMSS\SerializedName("text") + * @JMSS\Type("string") */ private $text; /** * @var int|null * - * @Serializer\Groups({"import_post_page"}) + * @JMSS\SerializedName("comment_id") + * @JMSS\Type("integer") */ - private $comment_id; + private $commentId; /** * @var User * - * @Serializer\Groups({"import_post_page"}) + * @JMSS\SerializedName("author") + * @JMSS\Type("Skobkin\Bundle\PointToolsBundle\DTO\Api\Crawler\User") + * @JMSS\MaxDepth(1) */ private $author; @@ -49,18 +57,18 @@ class Recommendation /** * @return int|null */ - public function getComment_id() + public function getCommentId() { - return $this->comment_id; + return $this->commentId; } /** - * @param int|null $comment_id + * @param int|null $commentId * @return Recommendation */ - public function setComment_id($comment_id) + public function setCommentId($commentId) { - $this->comment_id = $comment_id; + $this->commentId = $commentId; return $this; } diff --git a/src/Skobkin/Bundle/PointToolsBundle/DTO/Api/Crawler/User.php b/src/Skobkin/Bundle/PointToolsBundle/DTO/Api/Crawler/User.php index e70bb0a..fdb0c0f 100644 --- a/src/Skobkin/Bundle/PointToolsBundle/DTO/Api/Crawler/User.php +++ b/src/Skobkin/Bundle/PointToolsBundle/DTO/Api/Crawler/User.php @@ -2,28 +2,35 @@ namespace Skobkin\Bundle\PointToolsBundle\DTO\Api\Crawler; -use Symfony\Component\Serializer\Annotation as Serializer; +use JMS\Serializer\Annotation as JMSS; +/** + * @JMSS\ExclusionPolicy("none") + * @JMSS\AccessType("public_method") + */ class User { /** * @var string * - * @Serializer\Groups({"import_post_page"}) + * @JMSS\SerializedName("id") + * @JMSS\Type("integer") */ private $id; /** * @var string * - * @Serializer\Groups({"import_post_page"}) + * @JMSS\SerializedName("login") + * @JMSS\Type("string") */ private $login; /** * @var string * - * @Serializer\Groups({"import_post_page"}) + * @JMSS\SerializedName("name") + * @JMSS\Type("string") */ private $name;