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