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