PostsPage deserialization via JMS Serializer.

This commit is contained in:
Alexey Skobkin 2016-03-22 22:57:18 +03:00
parent 3af5fd230e
commit 8f44a04989
6 changed files with 76 additions and 34 deletions

View file

@ -11,12 +11,12 @@ class CrawlerController extends AbstractApiController
$token = $request->request->get('token'); $token = $request->request->get('token');
$json = $request->request->get('json'); $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([ return $this->createSuccessResponse([
'continue' => true, 'continue' => false,
]); ]);
} }
} }

View file

@ -2,21 +2,29 @@
namespace Skobkin\Bundle\PointToolsBundle\DTO\Api\Crawler; 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 class MetaPost
{ {
/** /**
* @var Recommendation * @var Recommendation
* *
* @Serializer\Groups({"import_post_page"}) * @JMSS\SerializedName("rec")
* @JMSS\Type("Skobkin\Bundle\PointToolsBundle\DTO\Api\Crawler\Recommendation")
* @JMSS\MaxDepth(2)
*/ */
private $rec; private $rec;
/** /**
* @var Post * @var Post
* *
* @Serializer\Groups({"import_post_page"}) * @JMSS\SerializedName("post")
* @JMSS\Type("Skobkin\Bundle\PointToolsBundle\DTO\Api\Crawler\Post")
* @JMSS\MaxDepth(2)
*/ */
private $post; private $post;

View file

@ -2,56 +2,68 @@
namespace Skobkin\Bundle\PointToolsBundle\DTO\Api\Crawler; 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 class Post
{ {
/** /**
* @var string * @var string
* *
* @Serializer\Groups({"import_post_page"}) * @JMSS\SerializedName("id")
* @JMSS\Type("string")
*/ */
private $id; private $id;
/** /**
* @var string[] * @var string[]
* *
* @Serializer\Groups({"import_post_page"}) * @JMSS\SerializedName("tags")
* @JMSS\Type("array<string>")
*/ */
private $tags; private $tags;
/** /**
* @var User * @var User
* *
* @Serializer\Groups({"import_post_page"}) * @JMSS\SerializedName("author")
* @JMSS\Type("Skobkin\Bundle\PointToolsBundle\DTO\Api\Crawler\User")
* @JMSS\MaxDepth(1)
*/ */
private $author; private $author;
/** /**
* @var string * @var string
* *
* @Serializer\Groups({"import_post_page"}) * @JMSS\SerializedName("text")
* @JMSS\Type("string")
*/ */
private $text; private $text;
/** /**
* @var string * @var string
* *
* @Serializer\Groups({"import_post_page"}) * @JMSS\SerializedName("created")
* @JMSS\Type("string")
*/ */
private $created; private $created;
/** /**
* @var string * @var string
* *
* @Serializer\Groups({"import_post_page"}) * @JMSS\SerializedName("type")
* @JMSS\Type("string")
*/ */
private $type; private $type;
/** /**
* @var bool * @var bool
* *
* @Serializer\Groups({"import_post_page"}) * @JMSS\SerializedName("private")
* @JMSS\Type("boolean")
*/ */
private $private; private $private;
@ -86,7 +98,7 @@ class Post
* @param string[] $tags * @param string[] $tags
* @return Post * @return Post
*/ */
public function setTags(array $tags) public function setTags($tags)
{ {
$this->tags = $tags; $this->tags = $tags;
return $this; return $this;

View file

@ -2,19 +2,26 @@
namespace Skobkin\Bundle\PointToolsBundle\DTO\Api\Crawler; 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 class PostsPage
{ {
/** /**
* @var MetaPost[] * @var MetaPost[]
* *
* @Serializer\Groups({"import_post_page"}) * @JMSS\SerializedName("posts")
* @JMSS\Type("array<Skobkin\Bundle\PointToolsBundle\DTO\Api\Crawler\MetaPost>")
* @JMSS\MaxDepth(3)
*/ */
private $posts; private $posts;
/** /**
* @return MetaPost[] * @return MetaPost[]|ArrayCollection
*/ */
public function getPosts() public function getPosts()
{ {
@ -22,10 +29,10 @@ class PostsPage
} }
/** /**
* @param MetaPost[] $posts * @param MetaPost[]|ArrayCollection $posts
* @return PostsPage * @return PostsPage
*/ */
public function setPosts(array $posts) public function setPosts($posts)
{ {
$this->posts = $posts; $this->posts = $posts;
return $this; return $this;

View file

@ -2,28 +2,36 @@
namespace Skobkin\Bundle\PointToolsBundle\DTO\Api\Crawler; 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 class Recommendation
{ {
/** /**
* @var string * @var string
* *
* @Serializer\Groups({"import_post_page"}) * @JMSS\SerializedName("text")
* @JMSS\Type("string")
*/ */
private $text; private $text;
/** /**
* @var int|null * @var int|null
* *
* @Serializer\Groups({"import_post_page"}) * @JMSS\SerializedName("comment_id")
* @JMSS\Type("integer")
*/ */
private $comment_id; private $commentId;
/** /**
* @var User * @var User
* *
* @Serializer\Groups({"import_post_page"}) * @JMSS\SerializedName("author")
* @JMSS\Type("Skobkin\Bundle\PointToolsBundle\DTO\Api\Crawler\User")
* @JMSS\MaxDepth(1)
*/ */
private $author; private $author;
@ -49,18 +57,18 @@ class Recommendation
/** /**
* @return int|null * @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 * @return Recommendation
*/ */
public function setComment_id($comment_id) public function setCommentId($commentId)
{ {
$this->comment_id = $comment_id; $this->commentId = $commentId;
return $this; return $this;
} }

View file

@ -2,28 +2,35 @@
namespace Skobkin\Bundle\PointToolsBundle\DTO\Api\Crawler; 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 class User
{ {
/** /**
* @var string * @var string
* *
* @Serializer\Groups({"import_post_page"}) * @JMSS\SerializedName("id")
* @JMSS\Type("integer")
*/ */
private $id; private $id;
/** /**
* @var string * @var string
* *
* @Serializer\Groups({"import_post_page"}) * @JMSS\SerializedName("login")
* @JMSS\Type("string")
*/ */
private $login; private $login;
/** /**
* @var string * @var string
* *
* @Serializer\Groups({"import_post_page"}) * @JMSS\SerializedName("name")
* @JMSS\Type("string")
*/ */
private $name; private $name;