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 getSlug() |
81
|
|
|
{ |
82
|
|
|
return $this->getTranslation()->getSlug(); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* {@inheritdoc} |
87
|
|
|
*/ |
88
|
|
|
public function getTitle() |
89
|
|
|
{ |
90
|
|
|
return $this->getTranslation()->getTitle(); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* {@inheritdoc} |
95
|
|
|
*/ |
96
|
|
|
public function getContent() |
97
|
|
|
{ |
98
|
|
|
return $this->getTranslation()->getContent(); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* {@inheritdoc} |
103
|
|
|
*/ |
104
|
|
|
public function getMetaKeywords() |
105
|
|
|
{ |
106
|
|
|
return $this->getTranslation()->getMetaKeywords(); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* {@inheritdoc} |
111
|
|
|
*/ |
112
|
|
|
public function getMetaDescription() |
113
|
|
|
{ |
114
|
|
|
return $this->getTranslation()->getMetaDescription(); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* {@inheritdoc} |
119
|
|
|
*/ |
120
|
|
|
public function addCategory(ArticleCategoryInterface $category) |
121
|
|
|
{ |
122
|
|
|
if (!$this->hasCategory($category)) { |
123
|
|
|
$category->addArticle($this); |
124
|
|
|
$this->categories->add($category); |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* {@inheritdoc} |
130
|
|
|
*/ |
131
|
|
|
public function removeCategory(ArticleCategoryInterface $category) |
132
|
|
|
{ |
133
|
|
|
if ($this->hasCategory($category)) { |
134
|
|
|
$this->categories->removeElement($category); |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* {@inheritdoc} |
140
|
|
|
*/ |
141
|
|
|
public function hasCategory(ArticleCategoryInterface $category) |
142
|
|
|
{ |
143
|
|
|
return $this->categories->contains($category); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* {@inheritdoc} |
148
|
|
|
*/ |
149
|
|
|
public function getCategories() |
150
|
|
|
{ |
151
|
|
|
return $this->categories; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* {@inheritdoc} |
156
|
|
|
*/ |
157
|
|
|
public function getImages(): Collection |
158
|
|
|
{ |
159
|
|
|
return $this->images; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* {@inheritdoc} |
164
|
|
|
*/ |
165
|
|
|
public function getImagesByType(string $type): Collection |
166
|
|
|
{ |
167
|
|
|
return $this->images->filter(function (ImageInterface $image) use ($type): bool { |
168
|
|
|
return $type === $image->getType(); |
169
|
|
|
}); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* {@inheritdoc} |
174
|
|
|
*/ |
175
|
|
|
public function hasImages(): bool |
176
|
|
|
{ |
177
|
|
|
return !$this->images->isEmpty(); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* {@inheritdoc} |
182
|
|
|
*/ |
183
|
|
|
public function hasImage(ImageInterface $image): bool |
184
|
|
|
{ |
185
|
|
|
return $this->images->contains($image); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* {@inheritdoc} |
190
|
|
|
*/ |
191
|
|
|
public function addImage(ImageInterface $image): void |
192
|
|
|
{ |
193
|
|
|
$image->setOwner($this); |
194
|
|
|
$this->images->add($image); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* {@inheritdoc} |
199
|
|
|
*/ |
200
|
|
|
public function removeImage(ImageInterface $image): void |
201
|
|
|
{ |
202
|
|
|
if ($this->hasImage($image)) { |
203
|
|
|
$image->setOwner(null); |
204
|
|
|
$this->images->removeElement($image); |
205
|
|
|
} |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* {@inheritdoc} |
210
|
|
|
*/ |
211
|
|
|
public function getComments(): Collection |
212
|
|
|
{ |
213
|
|
|
return $this->comments; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* {@inheritdoc} |
218
|
|
|
*/ |
219
|
|
|
public function getEnabledComments(): Collection |
220
|
|
|
{ |
221
|
|
|
return $this->comments->filter(function(ArticleCommentInterface $comment) { |
222
|
|
|
return $comment->isEnabled() && !$comment->getParent(); |
223
|
|
|
}); |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* {@inheritdoc} |
228
|
|
|
*/ |
229
|
|
|
public function setComments(Collection $comments): void |
230
|
|
|
{ |
231
|
|
|
$this->comments = $comments; |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* {@inheritdoc} |
236
|
|
|
*/ |
237
|
|
|
public function hasComment(ArticleCommentInterface $comment): bool |
238
|
|
|
{ |
239
|
|
|
return $this->comments->contains($comment); |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* {@inheritdoc} |
244
|
|
|
*/ |
245
|
|
|
public function addComment(ArticleCommentInterface $comment): void |
246
|
|
|
{ |
247
|
|
|
if (!$this->hasComment($comment)) { |
248
|
|
|
$this->comments->add($comment); |
249
|
|
|
} |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* {@inheritdoc} |
254
|
|
|
*/ |
255
|
|
|
public function removeComment(ArticleCommentInterface $comment): void |
256
|
|
|
{ |
257
|
|
|
if ($this->hasComment($comment)) { |
258
|
|
|
$this->comments->removeElement($comment); |
259
|
|
|
} |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
/** |
263
|
|
|
* {@inheritdoc} |
264
|
|
|
* @return ArticleTranslation |
265
|
|
|
*/ |
266
|
|
|
public function getTranslation(?string $locale = null): TranslationInterface |
267
|
|
|
{ |
268
|
|
|
/** @var ArticleTranslation $translation */ |
269
|
|
|
$translation = $this->doGetTranslation($locale); |
270
|
|
|
|
271
|
|
|
return $translation; |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
/** |
275
|
|
|
* {@inheritdoc} |
276
|
|
|
*/ |
277
|
|
|
protected function createTranslation(): ArticleTranslation |
278
|
|
|
{ |
279
|
|
|
return new ArticleTranslation(); |
280
|
|
|
} |
281
|
|
|
} |
282
|
|
|
|