1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Odiseo\BlogBundle\Model; |
4
|
|
|
|
5
|
|
|
use DateTime; |
6
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
7
|
|
|
use Doctrine\Common\Collections\Collection; |
8
|
|
|
use Sylius\Component\Resource\Model\ArchivableTrait; |
9
|
|
|
use Sylius\Component\Resource\Model\TimestampableTrait; |
10
|
|
|
use Sylius\Component\Resource\Model\ToggleableTrait; |
11
|
|
|
use Sylius\Component\Resource\Model\TranslatableTrait; |
12
|
|
|
use Sylius\Component\Resource\Model\TranslationInterface; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @author Diego D'amico <[email protected]> |
16
|
|
|
*/ |
17
|
|
|
class Article implements ArticleInterface |
18
|
|
|
{ |
19
|
|
|
use TimestampableTrait; |
20
|
|
|
use ToggleableTrait; |
21
|
|
|
use ArchivableTrait; |
22
|
|
|
|
23
|
|
|
use TranslatableTrait { |
24
|
|
|
__construct as private initializeTranslationsCollection; |
25
|
|
|
getTranslation as private doGetTranslation; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** @var mixed */ |
29
|
|
|
protected $id; |
30
|
|
|
|
31
|
|
|
/** @var string|null */ |
32
|
|
|
protected $code; |
33
|
|
|
|
34
|
|
|
/** @var Collection|ArticleCategoryInterface[] */ |
35
|
|
|
protected $categories; |
36
|
|
|
|
37
|
|
|
/** @var Collection|ImageInterface[] */ |
38
|
|
|
protected $images; |
39
|
|
|
|
40
|
|
|
/** @var Collection|ArticleCommentInterface[] */ |
41
|
|
|
protected $comments; |
42
|
|
|
|
43
|
|
|
public function __construct() |
44
|
|
|
{ |
45
|
|
|
$this->initializeTranslationsCollection(); |
46
|
|
|
$this->categories = new ArrayCollection(); |
47
|
|
|
$this->images = new ArrayCollection(); |
48
|
|
|
$this->comments = new ArrayCollection(); |
49
|
|
|
$this->createdAt = new DateTime(); |
50
|
|
|
$this->enabled = true; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* {@inheritdoc} |
55
|
|
|
*/ |
56
|
|
|
public function getId() |
57
|
|
|
{ |
58
|
|
|
return $this->id; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* {@inheritdoc} |
63
|
|
|
*/ |
64
|
|
|
public function getCode(): ?string |
65
|
|
|
{ |
66
|
|
|
return $this->code; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* {@inheritdoc} |
71
|
|
|
*/ |
72
|
|
|
public function setCode(?string $code): void |
73
|
|
|
{ |
74
|
|
|
$this->code = $code; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* {@inheritdoc} |
79
|
|
|
*/ |
80
|
|
|
public function setSlug(string $slug): void |
81
|
|
|
{ |
82
|
|
|
/** @var ArticleTranslationInterface $articleTranslation */ |
83
|
|
|
$articleTranslation = $this->getTranslation(); |
84
|
|
|
|
85
|
|
|
$articleTranslation->setSlug($slug); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* {@inheritdoc} |
90
|
|
|
*/ |
91
|
|
|
public function getSlug() |
92
|
|
|
{ |
93
|
|
|
return $this->getTranslation()->getSlug(); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* {@inheritdoc} |
98
|
|
|
*/ |
99
|
|
|
public function setTitle(string $title): void |
100
|
|
|
{ |
101
|
|
|
/** @var ArticleTranslationInterface $articleTranslation */ |
102
|
|
|
$articleTranslation = $this->getTranslation(); |
103
|
|
|
|
104
|
|
|
$articleTranslation->setTitle($title); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* {@inheritdoc} |
109
|
|
|
*/ |
110
|
|
|
public function getTitle() |
111
|
|
|
{ |
112
|
|
|
return $this->getTranslation()->getTitle(); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* {@inheritdoc} |
117
|
|
|
*/ |
118
|
|
|
public function setContent(string $content): void |
119
|
|
|
{ |
120
|
|
|
/** @var ArticleTranslationInterface $articleTranslation */ |
121
|
|
|
$articleTranslation = $this->getTranslation(); |
122
|
|
|
|
123
|
|
|
$articleTranslation->setContent($content); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* {@inheritdoc} |
128
|
|
|
*/ |
129
|
|
|
public function getContent() |
130
|
|
|
{ |
131
|
|
|
return $this->getTranslation()->getContent(); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* {@inheritdoc} |
136
|
|
|
*/ |
137
|
|
|
public function setMetaKeywords(string $metaKeywords): void |
138
|
|
|
{ |
139
|
|
|
/** @var ArticleTranslationInterface $articleTranslation */ |
140
|
|
|
$articleTranslation = $this->getTranslation(); |
141
|
|
|
|
142
|
|
|
$articleTranslation->setMetaKeywords($metaKeywords); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* {@inheritdoc} |
147
|
|
|
*/ |
148
|
|
|
public function getMetaKeywords() |
149
|
|
|
{ |
150
|
|
|
return $this->getTranslation()->getMetaKeywords(); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* {@inheritdoc} |
155
|
|
|
*/ |
156
|
|
|
public function setMetaDescription(string $metaDescription): void |
157
|
|
|
{ |
158
|
|
|
/** @var ArticleTranslationInterface $articleTranslation */ |
159
|
|
|
$articleTranslation = $this->getTranslation(); |
160
|
|
|
|
161
|
|
|
$articleTranslation->setMetaDescription($metaDescription); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* {@inheritdoc} |
166
|
|
|
*/ |
167
|
|
|
public function getMetaDescription() |
168
|
|
|
{ |
169
|
|
|
return $this->getTranslation()->getMetaDescription(); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* {@inheritdoc} |
174
|
|
|
*/ |
175
|
|
|
public function addCategory(ArticleCategoryInterface $category) |
176
|
|
|
{ |
177
|
|
|
if (!$this->hasCategory($category)) { |
178
|
|
|
$category->addArticle($this); |
179
|
|
|
$this->categories->add($category); |
180
|
|
|
} |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* {@inheritdoc} |
185
|
|
|
*/ |
186
|
|
|
public function removeCategory(ArticleCategoryInterface $category) |
187
|
|
|
{ |
188
|
|
|
if ($this->hasCategory($category)) { |
189
|
|
|
$this->categories->removeElement($category); |
190
|
|
|
} |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* {@inheritdoc} |
195
|
|
|
*/ |
196
|
|
|
public function hasCategory(ArticleCategoryInterface $category) |
197
|
|
|
{ |
198
|
|
|
return $this->categories->contains($category); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* {@inheritdoc} |
203
|
|
|
*/ |
204
|
|
|
public function getCategories() |
205
|
|
|
{ |
206
|
|
|
return $this->categories; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* {@inheritdoc} |
211
|
|
|
*/ |
212
|
|
|
public function getImages(): Collection |
213
|
|
|
{ |
214
|
|
|
return $this->images; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* {@inheritdoc} |
219
|
|
|
*/ |
220
|
|
|
public function getImagesByType(string $type): Collection |
221
|
|
|
{ |
222
|
|
|
return $this->images->filter(function (ImageInterface $image) use ($type): bool { |
223
|
|
|
return $type === $image->getType(); |
224
|
|
|
}); |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* {@inheritdoc} |
229
|
|
|
*/ |
230
|
|
|
public function hasImages(): bool |
231
|
|
|
{ |
232
|
|
|
return !$this->images->isEmpty(); |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* {@inheritdoc} |
237
|
|
|
*/ |
238
|
|
|
public function hasImage(ImageInterface $image): bool |
239
|
|
|
{ |
240
|
|
|
return $this->images->contains($image); |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* {@inheritdoc} |
245
|
|
|
*/ |
246
|
|
|
public function addImage(ImageInterface $image): void |
247
|
|
|
{ |
248
|
|
|
$image->setOwner($this); |
249
|
|
|
$this->images->add($image); |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* {@inheritdoc} |
254
|
|
|
*/ |
255
|
|
|
public function removeImage(ImageInterface $image): void |
256
|
|
|
{ |
257
|
|
|
if ($this->hasImage($image)) { |
258
|
|
|
$image->setOwner(null); |
259
|
|
|
$this->images->removeElement($image); |
260
|
|
|
} |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
/** |
264
|
|
|
* {@inheritdoc} |
265
|
|
|
*/ |
266
|
|
|
public function getComments(): Collection |
267
|
|
|
{ |
268
|
|
|
return $this->comments; |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
/** |
272
|
|
|
* {@inheritdoc} |
273
|
|
|
*/ |
274
|
|
|
public function getEnabledComments(): Collection |
275
|
|
|
{ |
276
|
|
|
return $this->comments->filter(function(ArticleCommentInterface $comment) { |
277
|
|
|
return $comment->isEnabled() && !$comment->getParent(); |
278
|
|
|
}); |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
/** |
282
|
|
|
* {@inheritdoc} |
283
|
|
|
*/ |
284
|
|
|
public function setComments(Collection $comments): void |
285
|
|
|
{ |
286
|
|
|
$this->comments = $comments; |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
/** |
290
|
|
|
* {@inheritdoc} |
291
|
|
|
*/ |
292
|
|
|
public function hasComment(ArticleCommentInterface $comment): bool |
293
|
|
|
{ |
294
|
|
|
return $this->comments->contains($comment); |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
/** |
298
|
|
|
* {@inheritdoc} |
299
|
|
|
*/ |
300
|
|
|
public function addComment(ArticleCommentInterface $comment): void |
301
|
|
|
{ |
302
|
|
|
if (!$this->hasComment($comment)) { |
303
|
|
|
$comment->setArticle($this); |
304
|
|
|
$this->comments->add($comment); |
305
|
|
|
} |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
/** |
309
|
|
|
* {@inheritdoc} |
310
|
|
|
*/ |
311
|
|
|
public function removeComment(ArticleCommentInterface $comment): void |
312
|
|
|
{ |
313
|
|
|
if ($this->hasComment($comment)) { |
314
|
|
|
$this->comments->removeElement($comment); |
315
|
|
|
} |
316
|
|
|
} |
317
|
|
|
|
318
|
|
|
/** |
319
|
|
|
* {@inheritdoc} |
320
|
|
|
* @return ArticleTranslation |
321
|
|
|
*/ |
322
|
|
|
public function getTranslation(?string $locale = null): TranslationInterface |
323
|
|
|
{ |
324
|
|
|
/** @var ArticleTranslation $translation */ |
325
|
|
|
$translation = $this->doGetTranslation($locale); |
326
|
|
|
|
327
|
|
|
return $translation; |
328
|
|
|
} |
329
|
|
|
|
330
|
|
|
/** |
331
|
|
|
* {@inheritdoc} |
332
|
|
|
*/ |
333
|
|
|
protected function createTranslation(): ArticleTranslation |
334
|
|
|
{ |
335
|
|
|
return new ArticleTranslation(); |
336
|
|
|
} |
337
|
|
|
} |
338
|
|
|
|