1 | <?php |
||
2 | |||
3 | namespace Zenstruck\Foundry\Tests\Fixtures\Document; |
||
4 | |||
5 | use Doctrine\Common\Collections\ArrayCollection; |
||
6 | use Doctrine\Common\Collections\Collection; |
||
7 | use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB; |
||
8 | |||
9 | /** |
||
10 | * @MongoDB\Document(collection="post") |
||
11 | */ |
||
12 | class Post |
||
13 | { |
||
14 | /** |
||
15 | * @MongoDB\Id |
||
16 | */ |
||
17 | private $id; |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
18 | |||
19 | /** |
||
20 | * @MongoDB\Field(type="string") |
||
21 | */ |
||
22 | private $title; |
||
23 | |||
24 | /** |
||
25 | * @MongoDB\Field(type="string") |
||
26 | */ |
||
27 | private $body; |
||
28 | |||
29 | /** |
||
30 | * @MongoDB\Field(type="string", nullable=true) |
||
31 | */ |
||
32 | private $shortDescription; |
||
33 | |||
34 | /** |
||
35 | * @MongoDB\Field(type="int") |
||
36 | */ |
||
37 | private $viewCount = 0; |
||
38 | |||
39 | /** |
||
40 | * @MongoDB\Field(type="date") |
||
41 | */ |
||
42 | private $createdAt; |
||
43 | |||
44 | /** |
||
45 | * @MongoDB\Field(type="date", nullable=true) |
||
46 | */ |
||
47 | private $publishedAt; |
||
48 | |||
49 | /** |
||
50 | * @MongoDB\EmbedMany( |
||
51 | * targetDocument=Comment::class |
||
52 | * ) |
||
53 | */ |
||
54 | private $comments; |
||
55 | |||
56 | /** |
||
57 | * @MongoDB\EmbedOne( |
||
58 | * targetDocument=User::class |
||
59 | * ) |
||
60 | */ |
||
61 | private $user; |
||
62 | |||
63 | public function __construct(string $title, string $body, User $user, ?string $shortDescription = null) |
||
64 | { |
||
65 | $this->title = $title; |
||
66 | $this->body = $body; |
||
67 | $this->shortDescription = $shortDescription; |
||
68 | $this->createdAt = new \DateTime('now'); |
||
69 | $this->comments = new ArrayCollection(); |
||
70 | $this->user = $user; |
||
71 | } |
||
72 | |||
73 | public function __toString(): string |
||
74 | { |
||
75 | return $this->title; |
||
76 | } |
||
77 | |||
78 | public function getTitle(): ?string |
||
79 | { |
||
80 | return $this->title; |
||
81 | } |
||
82 | |||
83 | public function getBody(): ?string |
||
84 | { |
||
85 | return $this->body; |
||
86 | } |
||
87 | |||
88 | public function getUser(): User |
||
89 | { |
||
90 | return $this->user; |
||
91 | } |
||
92 | |||
93 | public function getShortDescription(): ?string |
||
94 | { |
||
95 | return $this->shortDescription; |
||
96 | } |
||
97 | |||
98 | public function getViewCount(): int |
||
99 | { |
||
100 | return $this->viewCount; |
||
101 | } |
||
102 | |||
103 | public function increaseViewCount(int $amount = 1): void |
||
104 | { |
||
105 | $this->viewCount += $amount; |
||
106 | } |
||
107 | |||
108 | public function getCreatedAt(): ?\DateTime |
||
109 | { |
||
110 | return $this->createdAt; |
||
111 | } |
||
112 | |||
113 | public function isPublished(): bool |
||
114 | { |
||
115 | return null !== $this->publishedAt; |
||
116 | } |
||
117 | |||
118 | public function setPublishedAt(\DateTime $timestamp) |
||
119 | { |
||
120 | $this->publishedAt = $timestamp; |
||
121 | } |
||
122 | |||
123 | /** |
||
124 | * @return Collection<Comment> |
||
125 | */ |
||
126 | public function getComments(): Collection |
||
127 | { |
||
128 | return $this->comments; |
||
129 | } |
||
130 | |||
131 | public function addComment(Comment $comment): self |
||
132 | { |
||
133 | if (!$this->comments->contains($comment)) { |
||
134 | $this->comments->add($comment); |
||
135 | } |
||
136 | |||
137 | return $this; |
||
138 | } |
||
139 | |||
140 | public function removeComment(Comment $comment): self |
||
141 | { |
||
142 | if ($this->comments->contains($comment)) { |
||
143 | $this->comments->removeElement($comment); |
||
144 | } |
||
145 | |||
146 | return $this; |
||
147 | } |
||
148 | } |
||
149 |