1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Zenstruck\Foundry\Tests\Fixtures\Entity; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
6
|
|
|
use Doctrine\ORM\Mapping as ORM; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* @ORM\Entity(repositoryClass="Zenstruck\Foundry\Tests\Fixtures\Repository\PostRepository") |
10
|
|
|
* @ORM\Table(name="posts") |
11
|
|
|
*/ |
12
|
|
|
class Post |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @ORM\Id |
16
|
|
|
* @ORM\GeneratedValue |
17
|
|
|
* @ORM\Column(type="integer") |
18
|
|
|
*/ |
19
|
|
|
private $id; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @ORM\Column(type="string", length=255) |
23
|
|
|
*/ |
24
|
|
|
private $title; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @ORM\Column(type="text") |
28
|
|
|
*/ |
29
|
|
|
private $body; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @ORM\Column(type="string", length=255, nullable=true) |
33
|
|
|
*/ |
34
|
|
|
private $shortDescription; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @ORM\Column(type="integer") |
38
|
|
|
*/ |
39
|
|
|
private $viewCount = 0; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @ORM\Column(type="datetime") |
43
|
|
|
*/ |
44
|
|
|
private $createdAt; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @ORM\Column(type="datetime", nullable=true) |
48
|
|
|
*/ |
49
|
|
|
private $publishedAt; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @ORM\ManyToOne(targetEntity=Category::class, inversedBy="posts") |
53
|
|
|
* @ORM\JoinColumn |
54
|
|
|
*/ |
55
|
|
|
private $category; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @ORM\ManyToMany(targetEntity=Tag::class, inversedBy="posts") |
59
|
|
|
*/ |
60
|
|
|
private $tags; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @ORM\OneToMany(targetEntity=Comment::class, mappedBy="post") |
64
|
|
|
*/ |
65
|
|
|
private $comments; |
66
|
|
|
|
67
|
|
|
public function __construct(string $title, string $body, ?string $shortDescription = null) |
68
|
|
|
{ |
69
|
|
|
$this->title = $title; |
70
|
|
|
$this->body = $body; |
71
|
|
|
$this->shortDescription = $shortDescription; |
72
|
|
|
$this->createdAt = new \DateTime('now'); |
73
|
|
|
$this->tags = new ArrayCollection(); |
74
|
|
|
$this->comments = new ArrayCollection(); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function __toString(): string |
78
|
|
|
{ |
79
|
|
|
return $this->title; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function getId() |
83
|
|
|
{ |
84
|
|
|
return $this->id; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function getTitle(): ?string |
88
|
|
|
{ |
89
|
|
|
return $this->title; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function getBody(): ?string |
93
|
|
|
{ |
94
|
|
|
return $this->body; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function getShortDescription(): ?string |
98
|
|
|
{ |
99
|
|
|
return $this->shortDescription; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
public function getViewCount(): int |
103
|
|
|
{ |
104
|
|
|
return $this->viewCount; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
public function increaseViewCount(int $amount = 1): void |
108
|
|
|
{ |
109
|
|
|
$this->viewCount += $amount; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
public function getCreatedAt(): ?\DateTime |
113
|
|
|
{ |
114
|
|
|
return $this->createdAt; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
public function getCategory(): ?Category |
118
|
|
|
{ |
119
|
|
|
return $this->category; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
public function setCategory(?Category $category) |
123
|
|
|
{ |
124
|
|
|
$this->category = $category; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
public function isPublished(): bool |
128
|
|
|
{ |
129
|
|
|
return null !== $this->publishedAt; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
public function setPublishedAt(\DateTime $timestamp) |
133
|
|
|
{ |
134
|
|
|
$this->publishedAt = $timestamp; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
public function getTags() |
138
|
|
|
{ |
139
|
|
|
return $this->tags; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
public function addTag(Tag $tag) |
143
|
|
|
{ |
144
|
|
|
if (!$this->tags->contains($tag)) { |
145
|
|
|
$this->tags[] = $tag; |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
public function removeTag(Tag $tag) |
150
|
|
|
{ |
151
|
|
|
if ($this->tags->contains($tag)) { |
152
|
|
|
$this->tags->removeElement($tag); |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
public function getComments() |
157
|
|
|
{ |
158
|
|
|
return $this->comments; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
public function addComment(Comment $comment): self |
162
|
|
|
{ |
163
|
|
|
if (!$this->comments->contains($comment)) { |
164
|
|
|
$this->comments[] = $comment; |
165
|
|
|
$comment->setPost($this); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
return $this; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
public function removeComment(Comment $comment): self |
172
|
|
|
{ |
173
|
|
|
if ($this->comments->contains($comment)) { |
174
|
|
|
$this->comments->removeElement($comment); |
175
|
|
|
// set the owning side to null (unless already changed) |
176
|
|
|
if ($comment->getPost() === $this) { |
177
|
|
|
$comment->setPost(null); |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
return $this; |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
|