1<?php
2
3declare(strict_types=1);
4
5namespace JMS\Serializer\Tests\Fixtures;
6
7use Doctrine\Common\Collections\ArrayCollection;
8use JMS\Serializer\Annotation\Groups;
9use JMS\Serializer\Annotation\SerializedName;
10use JMS\Serializer\Annotation\Type;
11use JMS\Serializer\Annotation\XmlAttribute;
12use JMS\Serializer\Annotation\XmlElement;
13use JMS\Serializer\Annotation\XmlList;
14use JMS\Serializer\Annotation\XmlMap;
15use JMS\Serializer\Annotation\XmlNamespace;
16use JMS\Serializer\Annotation\XmlRoot;
17
18/**
19 * @XmlRoot("blog-post")
20 * @XmlNamespace(uri="http://example.com/namespace")
21 * @XmlNamespace(uri="http://schemas.google.com/g/2005", prefix="gd")
22 * @XmlNamespace(uri="http://www.w3.org/2005/Atom", prefix="atom")
23 * @XmlNamespace(uri="http://purl.org/dc/elements/1.1/", prefix="dc")
24 */
25class BlogPost
26{
27    /**
28     * @Type("string")
29     * @XmlElement(cdata=false)
30     * @Groups({"comments","post"})
31     */
32    private $id = 'what_a_nice_id';
33
34    /**
35     * @Type("string")
36     * @Groups({"comments","post"})
37     * @XmlElement(namespace="http://purl.org/dc/elements/1.1/");
38     */
39    private $title;
40
41    /**
42     * @Type("DateTime")
43     * @XmlAttribute
44     */
45    private $createdAt;
46
47    /**
48     * @Type("boolean")
49     * @SerializedName("is_published")
50     * @XmlAttribute
51     * @Groups({"post"})
52     */
53    private $published;
54
55    /**
56     * @Type("bool")
57     * @SerializedName("is_reviewed")
58     * @XmlAttribute
59     * @Groups({"post"})
60     */
61    private $reviewed;
62
63    /**
64     * @Type("string")
65     * @XmlAttribute(namespace="http://schemas.google.com/g/2005")
66     * @Groups({"post"})
67     */
68    private $etag;
69
70    /**
71     * @Type("ArrayCollection<JMS\Serializer\Tests\Fixtures\Comment>")
72     * @XmlList(inline=true, entry="comment")
73     * @Groups({"comments"})
74     */
75    private $comments;
76
77    /**
78     * @Type("array<JMS\Serializer\Tests\Fixtures\Comment>")
79     * @XmlList(inline=true, entry="comment2")
80     * @Groups({"comments"})
81     */
82    private $comments2;
83
84    /**
85     * @Type("array<string,string>")
86     * @XmlMap(keyAttribute = "key")
87     */
88    private $metadata;
89
90    /**
91     * @Type("JMS\Serializer\Tests\Fixtures\Author")
92     * @Groups({"post"})
93     * @XmlElement(namespace="http://www.w3.org/2005/Atom")
94     */
95    private $author;
96
97    /**
98     * @Type("JMS\Serializer\Tests\Fixtures\Publisher")
99     */
100    private $publisher;
101
102    /**
103     * @Type("array<JMS\Serializer\Tests\Fixtures\Tag>")
104     * @XmlList(inline=true, entry="tag", namespace="http://purl.org/dc/elements/1.1/");
105     */
106    private $tag;
107
108    public function __construct($title, Author $author, \DateTime $createdAt, Publisher $publisher)
109    {
110        $this->title = $title;
111        $this->author = $author;
112        $this->publisher = $publisher;
113        $this->published = false;
114        $this->reviewed = false;
115        $this->comments = new ArrayCollection();
116        $this->comments2 = [];
117        $this->metadata = ['foo' => 'bar'];
118        $this->createdAt = $createdAt;
119        $this->etag = sha1($this->createdAt->format(\DateTime::ATOM));
120    }
121
122    public function setPublished()
123    {
124        $this->published = true;
125    }
126
127    public function getMetadata()
128    {
129        return $this->metadata;
130    }
131
132    public function addComment(Comment $comment)
133    {
134        $this->comments->add($comment);
135        $this->comments2[] = $comment;
136    }
137
138    public function addTag(Tag $tag)
139    {
140        $this->tag[] = $tag;
141    }
142}
143