1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the OrbitaleCmsBundle package. |
5
|
|
|
* |
6
|
|
|
* (c) Alexandre Rock Ancelet <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Orbitale\Bundle\CmsBundle\Entity; |
13
|
|
|
|
14
|
|
|
use Behat\Transliterator\Transliterator; |
15
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
16
|
|
|
use Doctrine\ORM\Event\LifecycleEventArgs; |
17
|
|
|
use Doctrine\ORM\Mapping as ORM; |
18
|
|
|
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @UniqueEntity("slug") |
22
|
|
|
* @ORM\HasLifecycleCallbacks() |
23
|
|
|
* @ORM\MappedSuperclass(repositoryClass="Orbitale\Bundle\CmsBundle\Repository\CategoryRepository") |
24
|
|
|
*/ |
25
|
|
|
abstract class Category |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @return int|string |
29
|
|
|
*/ |
30
|
|
|
abstract public function getId(); |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var string |
34
|
|
|
* @ORM\Column(name="name", type="string", length=255) |
35
|
|
|
*/ |
36
|
|
|
protected $name; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var string |
40
|
|
|
* @ORM\Column(name="slug", type="string", length=255, unique=true) |
41
|
|
|
*/ |
42
|
|
|
protected $slug; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var string |
46
|
|
|
* @ORM\Column(name="description", type="text", nullable=true) |
47
|
|
|
*/ |
48
|
|
|
protected $description; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @var bool |
52
|
|
|
* @ORM\Column(name="enabled", type="boolean") |
53
|
|
|
*/ |
54
|
|
|
protected $enabled = false; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @var \DateTime |
58
|
|
|
* |
59
|
|
|
* @ORM\Column(name="created_at", type="datetime") |
60
|
|
|
*/ |
61
|
|
|
protected $createdAt; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @var Category |
65
|
|
|
*/ |
66
|
|
|
protected $parent; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @var Category[]|ArrayCollection |
70
|
|
|
*/ |
71
|
|
|
protected $children; |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @var Page[]|ArrayCollection |
75
|
|
|
*/ |
76
|
|
|
protected $pages; |
77
|
|
|
|
78
|
|
|
public function __toString() |
79
|
|
|
{ |
80
|
|
|
return $this->name; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function __construct() |
84
|
|
|
{ |
85
|
|
|
$this->createdAt = new \DateTime(); |
86
|
|
|
$this->children = new ArrayCollection(); |
87
|
|
|
$this->pages = new ArrayCollection(); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @return string |
92
|
|
|
*/ |
93
|
|
|
public function getName() |
94
|
|
|
{ |
95
|
|
|
return $this->name; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param string $name |
100
|
|
|
* |
101
|
|
|
* @return Category |
102
|
|
|
*/ |
103
|
|
|
public function setName(string $name): Category |
104
|
|
|
{ |
105
|
|
|
$this->name = $name; |
106
|
|
|
|
107
|
|
|
return $this; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @return string |
112
|
|
|
*/ |
113
|
|
|
public function getDescription() |
114
|
|
|
{ |
115
|
|
|
return $this->description; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @param string $description |
|
|
|
|
120
|
|
|
* |
121
|
|
|
* @return Category |
122
|
|
|
*/ |
123
|
|
|
public function setDescription(string $description = null): Category |
124
|
|
|
{ |
125
|
|
|
$this->description = $description; |
126
|
|
|
|
127
|
|
|
return $this; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @return string |
132
|
|
|
*/ |
133
|
|
|
public function getSlug() |
134
|
|
|
{ |
135
|
|
|
return $this->slug; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @param string $slug |
|
|
|
|
140
|
|
|
* |
141
|
|
|
* @return Category |
142
|
|
|
*/ |
143
|
|
|
public function setSlug(string $slug = null): Category |
144
|
|
|
{ |
145
|
|
|
$this->slug = $slug; |
146
|
|
|
|
147
|
|
|
return $this; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @return bool |
152
|
|
|
*/ |
153
|
|
|
public function isEnabled() |
154
|
|
|
{ |
155
|
|
|
return $this->enabled; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @param bool $enabled |
|
|
|
|
160
|
|
|
* |
161
|
|
|
* @return Category |
162
|
|
|
*/ |
163
|
|
|
public function setEnabled(bool $enabled = null): Category |
164
|
|
|
{ |
165
|
|
|
$this->enabled = $enabled; |
166
|
|
|
|
167
|
|
|
return $this; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* @return Category |
172
|
|
|
*/ |
173
|
|
|
public function getParent() |
174
|
|
|
{ |
175
|
|
|
return $this->parent; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* @param Category|null $parent |
180
|
|
|
* |
181
|
|
|
* @return Category |
182
|
|
|
*/ |
183
|
|
View Code Duplication |
public function setParent(Category $parent = null): Category |
|
|
|
|
184
|
|
|
{ |
185
|
|
|
if ($parent === $this) { |
186
|
|
|
// Refuse the category to have itself as parent. |
187
|
|
|
$this->parent = null; |
188
|
|
|
|
189
|
|
|
return $this; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
$this->parent = $parent; |
193
|
|
|
|
194
|
|
|
// Ensure bidirectional relation is respected. |
195
|
|
|
if ($parent && false === $parent->getChildren()->indexOf($this)) { |
196
|
|
|
$parent->addChild($this); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
return $this; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* @return \DateTime |
204
|
|
|
*/ |
205
|
|
|
public function getCreatedAt(): \DateTime |
206
|
|
|
{ |
207
|
|
|
return $this->createdAt; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* @return Category[]|ArrayCollection |
212
|
|
|
*/ |
213
|
|
|
public function getChildren() |
214
|
|
|
{ |
215
|
|
|
return $this->children; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* @param Category $category |
220
|
|
|
* |
221
|
|
|
* @return Category |
222
|
|
|
*/ |
223
|
|
|
public function addChild(Category $category): Category |
224
|
|
|
{ |
225
|
|
|
$this->children->add($category); |
226
|
|
|
|
227
|
|
|
if ($category->getParent() !== $this) { |
228
|
|
|
$category->setParent($this); |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
return $this; |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* @param Category $child |
236
|
|
|
* |
237
|
|
|
* @return Category |
238
|
|
|
*/ |
239
|
|
|
public function removeChild(Category $child): Category |
240
|
|
|
{ |
241
|
|
|
$this->children->removeElement($child); |
242
|
|
|
|
243
|
|
|
return $this; |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* @param string $separator |
248
|
|
|
* |
249
|
|
|
* @return string |
250
|
|
|
*/ |
251
|
|
View Code Duplication |
public function getTree(string $separator = '/'): string |
|
|
|
|
252
|
|
|
{ |
253
|
|
|
$tree = ''; |
254
|
|
|
|
255
|
|
|
$current = $this; |
256
|
|
|
do { |
257
|
|
|
$tree = $current->getSlug().$separator.$tree; |
258
|
|
|
$current = $current->getParent(); |
259
|
|
|
} while ($current); |
260
|
|
|
|
261
|
|
|
return trim($tree, $separator); |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
/** |
265
|
|
|
* @ORM\PrePersist() |
266
|
|
|
* @ORM\PreUpdate() |
267
|
|
|
*/ |
268
|
|
|
public function updateSlug() |
269
|
|
|
{ |
270
|
|
|
if (!$this->slug) { |
271
|
|
|
$this->slug = Transliterator::transliterate($this->name); |
272
|
|
|
} |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
/** |
276
|
|
|
* @ORM\PreRemove() |
277
|
|
|
* |
278
|
|
|
* @param LifecycleEventArgs $event |
279
|
|
|
*/ |
280
|
|
View Code Duplication |
public function onRemove(LifecycleEventArgs $event) |
|
|
|
|
281
|
|
|
{ |
282
|
|
|
$em = $event->getEntityManager(); |
283
|
|
|
if (count($this->children)) { |
284
|
|
|
foreach ($this->children as $child) { |
285
|
|
|
$child->setParent(null); |
286
|
|
|
$em->persist($child); |
287
|
|
|
} |
288
|
|
|
} |
289
|
|
|
$this->enabled = false; |
290
|
|
|
$this->parent = null; |
291
|
|
|
$this->name .= '-'.$this->getId().'-deleted'; |
292
|
|
|
$this->slug .= '-'.$this->getId().'-deleted'; |
293
|
|
|
} |
294
|
|
|
} |
295
|
|
|
|
This check looks for
@param
annotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.