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