1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Odiseo\BlogBundle\Model; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
6
|
|
|
use Doctrine\Common\Collections\Collection; |
7
|
|
|
use Sylius\Component\Resource\Model\ArchivableTrait; |
8
|
|
|
use Sylius\Component\Resource\Model\TimestampableTrait; |
9
|
|
|
use Sylius\Component\Resource\Model\ToggleableTrait; |
10
|
|
|
use Sylius\Component\Resource\Model\TranslatableTrait; |
11
|
|
|
use Sylius\Component\Resource\Model\TranslationInterface; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @author Diego D'amico <[email protected]> |
15
|
|
|
*/ |
16
|
|
|
class Article implements ArticleInterface |
17
|
|
|
{ |
18
|
|
|
use TimestampableTrait; |
19
|
|
|
use ToggleableTrait; |
20
|
|
|
use ArchivableTrait; |
21
|
|
|
|
22
|
|
|
use TranslatableTrait { |
23
|
|
|
__construct as private initializeTranslationsCollection; |
24
|
|
|
getTranslation as private doGetTranslation; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var mixed |
29
|
|
|
*/ |
30
|
|
|
protected $id; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var string|null |
34
|
|
|
*/ |
35
|
|
|
protected $code; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var Collection|ArticleCategoryInterface[] |
39
|
|
|
*/ |
40
|
|
|
protected $categories; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var Collection|ImageInterface[] |
44
|
|
|
*/ |
45
|
|
|
protected $images; |
46
|
|
|
|
47
|
|
|
public function __construct() |
48
|
|
|
{ |
49
|
|
|
$this->initializeTranslationsCollection(); |
50
|
|
|
$this->categories = new ArrayCollection(); |
51
|
|
|
$this->images = new ArrayCollection(); |
52
|
|
|
$this->createdAt = new \DateTime(); |
53
|
|
|
$this->enabled = true; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* {@inheritdoc} |
58
|
|
|
*/ |
59
|
|
|
public function getId() |
60
|
|
|
{ |
61
|
|
|
return $this->id; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* {@inheritdoc} |
66
|
|
|
*/ |
67
|
|
|
public function getCode(): ?string |
68
|
|
|
{ |
69
|
|
|
return $this->code; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* {@inheritdoc} |
74
|
|
|
*/ |
75
|
|
|
public function setCode(?string $code): void |
76
|
|
|
{ |
77
|
|
|
$this->code = $code; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* {@inheritdoc} |
82
|
|
|
*/ |
83
|
|
|
public function getSlug() |
84
|
|
|
{ |
85
|
|
|
return $this->getTranslation()->getSlug(); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* {@inheritdoc} |
90
|
|
|
*/ |
91
|
|
|
public function getTitle() |
92
|
|
|
{ |
93
|
|
|
return $this->getTranslation()->getTitle(); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* {@inheritdoc} |
98
|
|
|
*/ |
99
|
|
|
public function getContent() |
100
|
|
|
{ |
101
|
|
|
return $this->getTranslation()->getContent(); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* {@inheritdoc} |
106
|
|
|
*/ |
107
|
|
|
public function getMetaKeywords() |
108
|
|
|
{ |
109
|
|
|
return $this->getTranslation()->getMetaKeywords(); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* {@inheritdoc} |
114
|
|
|
*/ |
115
|
|
|
public function getMetaDescription() |
116
|
|
|
{ |
117
|
|
|
return $this->getTranslation()->getMetaDescription(); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* {@inheritdoc} |
122
|
|
|
*/ |
123
|
|
|
public function addCategory(ArticleCategoryInterface $category) |
124
|
|
|
{ |
125
|
|
|
if (!$this->hasCategory($category)) { |
126
|
|
|
$category->addArticle($this); |
127
|
|
|
$this->categories->add($category); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* {@inheritdoc} |
133
|
|
|
*/ |
134
|
|
|
public function removeCategory(ArticleCategoryInterface $category) |
135
|
|
|
{ |
136
|
|
|
if ($this->hasCategory($category)) { |
137
|
|
|
$this->categories->removeElement($category); |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* {@inheritdoc} |
143
|
|
|
*/ |
144
|
|
|
public function hasCategory(ArticleCategoryInterface $category) |
145
|
|
|
{ |
146
|
|
|
return $this->categories->contains($category); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* {@inheritdoc} |
151
|
|
|
*/ |
152
|
|
|
public function getCategories() |
153
|
|
|
{ |
154
|
|
|
return $this->categories; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* {@inheritdoc} |
159
|
|
|
*/ |
160
|
|
|
public function getImages(): Collection |
161
|
|
|
{ |
162
|
|
|
return $this->images; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* {@inheritdoc} |
167
|
|
|
*/ |
168
|
|
|
public function getImagesByType(string $type): Collection |
169
|
|
|
{ |
170
|
|
|
return $this->images->filter(function (ImageInterface $image) use ($type): bool { |
171
|
|
|
return $type === $image->getType(); |
172
|
|
|
}); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* {@inheritdoc} |
177
|
|
|
*/ |
178
|
|
|
public function hasImages(): bool |
179
|
|
|
{ |
180
|
|
|
return !$this->images->isEmpty(); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* {@inheritdoc} |
185
|
|
|
*/ |
186
|
|
|
public function hasImage(ImageInterface $image): bool |
187
|
|
|
{ |
188
|
|
|
return $this->images->contains($image); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* {@inheritdoc} |
193
|
|
|
*/ |
194
|
|
|
public function addImage(ImageInterface $image): void |
195
|
|
|
{ |
196
|
|
|
$image->setOwner($this); |
197
|
|
|
$this->images->add($image); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* {@inheritdoc} |
202
|
|
|
*/ |
203
|
|
|
public function removeImage(ImageInterface $image): void |
204
|
|
|
{ |
205
|
|
|
if ($this->hasImage($image)) { |
206
|
|
|
$image->setOwner(null); |
207
|
|
|
$this->images->removeElement($image); |
208
|
|
|
} |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* {@inheritdoc} |
213
|
|
|
*/ |
214
|
|
|
public function getTranslation(?string $locale = null): TranslationInterface |
215
|
|
|
{ |
216
|
|
|
/** @var ArticleTranslation $translation */ |
217
|
|
|
$translation = $this->doGetTranslation($locale); |
218
|
|
|
|
219
|
|
|
return $translation; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* {@inheritdoc} |
224
|
|
|
*/ |
225
|
|
|
protected function createTranslation(): ArticleTranslation |
226
|
|
|
{ |
227
|
|
|
return new ArticleTranslation(); |
228
|
|
|
} |
229
|
|
|
} |
230
|
|
|
|