BlogPost.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. namespace JMS\Serializer\Tests\Fixtures\Doctrine;
  3. use Doctrine\Common\Collections\ArrayCollection;
  4. use Doctrine\ORM\Mapping as ORM;
  5. use JMS\Serializer\Annotation as Serializer;
  6. use JMS\Serializer\Annotation\Groups;
  7. use JMS\Serializer\Annotation\SerializedName;
  8. use JMS\Serializer\Annotation\Type;
  9. use JMS\Serializer\Annotation\XmlAttribute;
  10. use JMS\Serializer\Annotation\XmlList;
  11. use JMS\Serializer\Annotation\XmlRoot;
  12. /**
  13. * @ORM\Entity
  14. * @XmlRoot("blog-post")
  15. */
  16. class BlogPost
  17. {
  18. /**
  19. * @ORM\Id @ORM\Column(type="guid") @ORM\GeneratedValue(strategy="UUID")
  20. */
  21. protected $id;
  22. /**
  23. * @ORM\Column(type="string")
  24. * @Groups({"comments","post"})
  25. */
  26. private $title;
  27. /**
  28. * @ORM\Column(type="some_custom_type")
  29. */
  30. protected $slug;
  31. /**
  32. * @ORM\Column(type="datetime")
  33. * @XmlAttribute
  34. */
  35. private $createdAt;
  36. /**
  37. * @ORM\Column(type="boolean")
  38. * @Type("integer")
  39. * This boolean to integer conversion is one of the few changes between this
  40. * and the standard BlogPost class. It's used to test the override behavior
  41. * of the DoctrineTypeDriver so notice it, but please don't change it.
  42. *
  43. * @SerializedName("is_published")
  44. * @Groups({"post"})
  45. * @XmlAttribute
  46. */
  47. private $published;
  48. /**
  49. * @ORM\OneToMany(targetEntity="Comment", mappedBy="blogPost")
  50. * @XmlList(inline=true, entry="comment")
  51. * @Groups({"comments"})
  52. */
  53. private $comments;
  54. /**
  55. * @ORM\OneToOne(targetEntity="Author")
  56. * @Groups({"post"})
  57. */
  58. private $author;
  59. /**
  60. * @ORM\Column(type="integer")
  61. * @Serializer\Exclude()
  62. */
  63. private $ref;
  64. public function __construct($title, Author $author, \DateTime $createdAt)
  65. {
  66. $this->title = $title;
  67. $this->author = $author;
  68. $this->published = false;
  69. $this->comments = new ArrayCollection();
  70. $this->createdAt = $createdAt;
  71. }
  72. public function setPublished()
  73. {
  74. $this->published = true;
  75. }
  76. public function addComment(Comment $comment)
  77. {
  78. $this->comments->add($comment);
  79. }
  80. /**
  81. * @Serializer\VirtualProperty()
  82. */
  83. public function getRef()
  84. {
  85. return $this->ref;
  86. }
  87. }