1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Blog\Entity; |
4
|
|
|
|
5
|
|
|
use App\Entity\User; |
6
|
|
|
use Cycle\Annotated\Annotation\Column; |
7
|
|
|
use Cycle\Annotated\Annotation\Entity; |
8
|
|
|
use Cycle\Annotated\Annotation\Relation\BelongsTo; |
9
|
|
|
use Cycle\Annotated\Annotation\Relation\HasMany; |
10
|
|
|
use Cycle\Annotated\Annotation\Relation\ManyToMany; |
11
|
|
|
use Cycle\Annotated\Annotation\Table; |
12
|
|
|
use Cycle\Annotated\Annotation\Table\Index; |
13
|
|
|
use Cycle\ORM\Relation\Pivoted\PivotedCollection; |
14
|
|
|
use DateTimeImmutable; |
15
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
16
|
|
|
use Yiisoft\Security\Random; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @Entity( |
20
|
|
|
* repository="App\Blog\Post\PostRepository", |
21
|
|
|
* mapper="App\Blog\Post\PostMapper", |
22
|
|
|
* constrain="App\Blog\Post\Scope\PublicScope" |
23
|
|
|
* ) |
24
|
|
|
* @Table( |
25
|
|
|
* indexes={ |
26
|
|
|
* @Index(columns={"public","publishedAt"}), |
27
|
|
|
* } |
28
|
|
|
* ) |
29
|
|
|
*/ |
30
|
|
|
class Post |
31
|
|
|
{ |
32
|
|
|
/** |
33
|
|
|
* @Column(type="primary") |
34
|
|
|
* @var int |
35
|
|
|
*/ |
36
|
|
|
private $id; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @Column(type="string(128)") |
40
|
|
|
* @var string |
41
|
|
|
*/ |
42
|
|
|
private $slug; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @Column(type="string(255)") |
46
|
|
|
* @var string |
47
|
|
|
*/ |
48
|
|
|
private $title; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @Column(type="bool", default="false") |
52
|
|
|
* @var bool |
53
|
|
|
*/ |
54
|
|
|
private $public; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @Column(type="text") |
58
|
|
|
* @var string |
59
|
|
|
*/ |
60
|
|
|
private $content; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @Column(type="datetime") |
64
|
|
|
* @var DateTimeImmutable |
65
|
|
|
*/ |
66
|
|
|
private $createdAt; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @Column(type="datetime") |
70
|
|
|
* @var DateTimeImmutable |
71
|
|
|
*/ |
72
|
|
|
private $updatedAt; |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @Column(type="datetime", nullable=true) |
76
|
|
|
* @var DateTimeImmutable|null |
77
|
|
|
*/ |
78
|
|
|
private $publishedAt; |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @Column(type="datetime", nullable=true) |
82
|
|
|
* @var DateTimeImmutable|null |
83
|
|
|
*/ |
84
|
|
|
private $deletedAt; |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @BelongsTo(target="App\Entity\User", nullable=false) |
88
|
|
|
* @var User|\Cycle\ORM\Promise\Reference |
89
|
|
|
*/ |
90
|
|
|
private $user; |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @ManyToMany(target="App\Blog\Entity\Tag", though="PostTag", fkAction="CASCADE") |
94
|
|
|
* @var Tag[]|PivotedCollection |
95
|
|
|
*/ |
96
|
|
|
private $tags; |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @HasMany(target="App\Blog\Entity\Comment") |
100
|
|
|
* @var Comment|ArrayCollection |
101
|
|
|
*/ |
102
|
|
|
private $comments; |
103
|
|
|
|
104
|
|
|
public function __construct() |
105
|
|
|
{ |
106
|
|
|
$this->tags = new PivotedCollection(); |
107
|
|
|
$this->comments = new ArrayCollection(); |
108
|
|
|
if (!isset($this->slug)) { |
109
|
|
|
$this->resetSlug(); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
public function getId(): ?string |
114
|
|
|
{ |
115
|
|
|
return $this->id; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public function getSlug(): ?string |
119
|
|
|
{ |
120
|
|
|
return $this->slug; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
public function resetSlug(): void |
124
|
|
|
{ |
125
|
|
|
$this->slug = Random::string(128); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
public function getTitle(): string |
129
|
|
|
{ |
130
|
|
|
return $this->title; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
public function setTitle(string $title): void |
134
|
|
|
{ |
135
|
|
|
$this->title = $title; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
public function getContent(): string |
139
|
|
|
{ |
140
|
|
|
return $this->content; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
public function setContent(string $content): void |
144
|
|
|
{ |
145
|
|
|
$this->content = $content; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
public function isPublic(): bool |
149
|
|
|
{ |
150
|
|
|
return $this->public; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
public function setPublic(bool $public): void |
154
|
|
|
{ |
155
|
|
|
$this->public = $public; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
public function getCreatedAt(): DateTimeImmutable |
159
|
|
|
{ |
160
|
|
|
return $this->createdAt; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
public function getUpdatedAt(): DateTimeImmutable |
164
|
|
|
{ |
165
|
|
|
return $this->updatedAt; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
public function getDeletedAt(): ?DateTimeImmutable |
169
|
|
|
{ |
170
|
|
|
return $this->deletedAt; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
public function setUser(User $user) |
174
|
|
|
{ |
175
|
|
|
$this->user = $user; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
public function getUser(): User |
179
|
|
|
{ |
180
|
|
|
return $this->user; |
|
|
|
|
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
public function getPublishedAt(): ?DateTimeImmutable |
184
|
|
|
{ |
185
|
|
|
return $this->publishedAt; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
public function setPublishedAt(?DateTimeImmutable $publishedAt): void |
189
|
|
|
{ |
190
|
|
|
$this->publishedAt = $publishedAt; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* @return ArrayCollection|Comment[] |
195
|
|
|
*/ |
196
|
|
|
public function getComments() |
197
|
|
|
{ |
198
|
|
|
return $this->comments; |
|
|
|
|
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
public function addComment(Comment $post): void |
202
|
|
|
{ |
203
|
|
|
$this->comments->add($post); |
|
|
|
|
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* @return ArrayCollection|Tag[] |
208
|
|
|
*/ |
209
|
|
|
public function getTags() |
210
|
|
|
{ |
211
|
|
|
return $this->tags; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
public function addTag(Tag $post): void |
215
|
|
|
{ |
216
|
|
|
$this->tags->add($post); |
217
|
|
|
} |
218
|
|
|
} |
219
|
|
|
|