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