|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Sylius package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Paweł Jędrzejewski |
|
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
|
|
|
declare(strict_types=1); |
|
13
|
|
|
|
|
14
|
|
|
namespace Sylius\Component\Taxonomy\Model; |
|
15
|
|
|
|
|
16
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
|
17
|
|
|
use Doctrine\Common\Collections\Collection; |
|
18
|
|
|
use Sylius\Component\Resource\Model\TranslatableTrait; |
|
19
|
|
|
use Sylius\Component\Resource\Model\TranslationInterface; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @author Paweł Jędrzejewski <[email protected]> |
|
23
|
|
|
* @author Gonzalo Vilaseca <[email protected]> |
|
24
|
|
|
*/ |
|
25
|
|
|
class Taxon implements TaxonInterface |
|
26
|
|
|
{ |
|
27
|
|
|
use TranslatableTrait { |
|
28
|
|
|
__construct as private initializeTranslationsCollection; |
|
29
|
|
|
getTranslation as private doGetTranslation; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var mixed |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $id; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @var string|null |
|
39
|
|
|
*/ |
|
40
|
|
|
protected $code; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @var TaxonInterface|null |
|
44
|
|
|
*/ |
|
45
|
|
|
protected $root; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @var TaxonInterface|null |
|
49
|
|
|
*/ |
|
50
|
|
|
protected $parent; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @var Collection|TaxonInterface[] |
|
54
|
|
|
*/ |
|
55
|
|
|
protected $children; |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @var int|null |
|
59
|
|
|
*/ |
|
60
|
|
|
protected $left; |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @var int|null |
|
64
|
|
|
*/ |
|
65
|
|
|
protected $right; |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @var int|null |
|
69
|
|
|
*/ |
|
70
|
|
|
protected $level; |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @var int|null |
|
74
|
|
|
*/ |
|
75
|
|
|
protected $position; |
|
76
|
|
|
|
|
77
|
|
|
public function __construct() |
|
78
|
|
|
{ |
|
79
|
|
|
$this->initializeTranslationsCollection(); |
|
80
|
|
|
|
|
81
|
|
|
$this->children = new ArrayCollection(); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @return string |
|
86
|
|
|
*/ |
|
87
|
|
|
public function __toString(): string |
|
88
|
|
|
{ |
|
89
|
|
|
return (string) $this->getName(); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* {@inheritdoc} |
|
94
|
|
|
*/ |
|
95
|
|
|
public function getId() |
|
96
|
|
|
{ |
|
97
|
|
|
return $this->id; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* {@inheritdoc} |
|
102
|
|
|
*/ |
|
103
|
|
|
public function getCode(): ?string |
|
104
|
|
|
{ |
|
105
|
|
|
return $this->code; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* {@inheritdoc} |
|
110
|
|
|
*/ |
|
111
|
|
|
public function setCode(?string $code): void |
|
112
|
|
|
{ |
|
113
|
|
|
$this->code = $code; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* {@inheritdoc} |
|
118
|
|
|
*/ |
|
119
|
|
|
public function isRoot(): bool |
|
120
|
|
|
{ |
|
121
|
|
|
return null === $this->parent; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* {@inheritdoc} |
|
126
|
|
|
*/ |
|
127
|
|
|
public function getRoot(): ?TaxonInterface |
|
128
|
|
|
{ |
|
129
|
|
|
return $this->root; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* {@inheritdoc} |
|
134
|
|
|
*/ |
|
135
|
|
|
public function getParent(): ?TaxonInterface |
|
136
|
|
|
{ |
|
137
|
|
|
return $this->parent; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* {@inheritdoc} |
|
142
|
|
|
*/ |
|
143
|
|
|
public function setParent(?TaxonInterface $parent): void |
|
144
|
|
|
{ |
|
145
|
|
|
$this->parent = $parent; |
|
146
|
|
|
if (null !== $parent) { |
|
147
|
|
|
$parent->addChild($this); |
|
148
|
|
|
} |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* {@inheritdoc} |
|
153
|
|
|
*/ |
|
154
|
|
|
public function getAncestors(): Collection |
|
155
|
|
|
{ |
|
156
|
|
|
$ancestors = []; |
|
157
|
|
|
|
|
158
|
|
|
for ($ancestor = $this->getParent(); null !== $ancestor; $ancestor = $ancestor->getParent()) { |
|
159
|
|
|
$ancestors[] = $ancestor; |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
return new ArrayCollection($ancestors); |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* {@inheritdoc} |
|
167
|
|
|
*/ |
|
168
|
|
|
public function getChildren(): Collection |
|
169
|
|
|
{ |
|
170
|
|
|
return $this->children; |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
/** |
|
174
|
|
|
* {@inheritdoc} |
|
175
|
|
|
*/ |
|
176
|
|
|
public function hasChild(TaxonInterface $taxon): bool |
|
177
|
|
|
{ |
|
178
|
|
|
return $this->children->contains($taxon); |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
/** |
|
182
|
|
|
* {@inheritdoc} |
|
183
|
|
|
*/ |
|
184
|
|
|
public function hasChildren(): bool |
|
185
|
|
|
{ |
|
186
|
|
|
return !$this->children->isEmpty(); |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
/** |
|
190
|
|
|
* {@inheritdoc} |
|
191
|
|
|
*/ |
|
192
|
|
|
public function addChild(TaxonInterface $taxon): void |
|
193
|
|
|
{ |
|
194
|
|
|
if (!$this->hasChild($taxon)) { |
|
195
|
|
|
$this->children->add($taxon); |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
if ($this !== $taxon->getParent()) { |
|
199
|
|
|
$taxon->setParent($this); |
|
200
|
|
|
} |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
/** |
|
204
|
|
|
* {@inheritdoc} |
|
205
|
|
|
*/ |
|
206
|
|
|
public function removeChild(TaxonInterface $taxon): void |
|
207
|
|
|
{ |
|
208
|
|
|
if ($this->hasChild($taxon)) { |
|
209
|
|
|
$taxon->setParent(null); |
|
210
|
|
|
|
|
211
|
|
|
$this->children->removeElement($taxon); |
|
212
|
|
|
} |
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
|
|
/** |
|
216
|
|
|
* {@inheritdoc} |
|
217
|
|
|
*/ |
|
218
|
|
|
public function getName(): ?string |
|
219
|
|
|
{ |
|
220
|
|
|
return $this->getTranslation()->getName(); |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
/** |
|
224
|
|
|
* {@inheritdoc} |
|
225
|
|
|
*/ |
|
226
|
|
|
public function setName(?string $name): void |
|
227
|
|
|
{ |
|
228
|
|
|
$this->getTranslation()->setName($name); |
|
229
|
|
|
} |
|
230
|
|
|
|
|
231
|
|
|
/** |
|
232
|
|
|
* {@inheritdoc} |
|
233
|
|
|
*/ |
|
234
|
|
|
public function getSlug(): ?string |
|
235
|
|
|
{ |
|
236
|
|
|
return $this->getTranslation()->getSlug(); |
|
237
|
|
|
} |
|
238
|
|
|
|
|
239
|
|
|
/** |
|
240
|
|
|
* {@inheritdoc} |
|
241
|
|
|
*/ |
|
242
|
|
|
public function setSlug(?string $slug): void |
|
243
|
|
|
{ |
|
244
|
|
|
$this->getTranslation()->setSlug($slug); |
|
245
|
|
|
} |
|
246
|
|
|
|
|
247
|
|
|
/** |
|
248
|
|
|
* {@inheritdoc} |
|
249
|
|
|
*/ |
|
250
|
|
|
public function getDescription(): ?string |
|
251
|
|
|
{ |
|
252
|
|
|
return $this->getTranslation()->getDescription(); |
|
253
|
|
|
} |
|
254
|
|
|
|
|
255
|
|
|
/** |
|
256
|
|
|
* {@inheritdoc} |
|
257
|
|
|
*/ |
|
258
|
|
|
public function setDescription(?string $description): void |
|
259
|
|
|
{ |
|
260
|
|
|
$this->getTranslation()->setDescription($description); |
|
261
|
|
|
} |
|
262
|
|
|
|
|
263
|
|
|
/** |
|
264
|
|
|
* {@inheritdoc} |
|
265
|
|
|
*/ |
|
266
|
|
|
public function getLeft(): ?int |
|
267
|
|
|
{ |
|
268
|
|
|
return $this->left; |
|
269
|
|
|
} |
|
270
|
|
|
|
|
271
|
|
|
/** |
|
272
|
|
|
* {@inheritdoc} |
|
273
|
|
|
*/ |
|
274
|
|
|
public function setLeft(?int $left): void |
|
275
|
|
|
{ |
|
276
|
|
|
$this->left = $left; |
|
277
|
|
|
} |
|
278
|
|
|
|
|
279
|
|
|
/** |
|
280
|
|
|
* {@inheritdoc} |
|
281
|
|
|
*/ |
|
282
|
|
|
public function getRight(): ?int |
|
283
|
|
|
{ |
|
284
|
|
|
return $this->right; |
|
285
|
|
|
} |
|
286
|
|
|
|
|
287
|
|
|
/** |
|
288
|
|
|
* {@inheritdoc} |
|
289
|
|
|
*/ |
|
290
|
|
|
public function setRight(?int $right): void |
|
291
|
|
|
{ |
|
292
|
|
|
$this->right = $right; |
|
293
|
|
|
} |
|
294
|
|
|
|
|
295
|
|
|
/** |
|
296
|
|
|
* {@inheritdoc} |
|
297
|
|
|
*/ |
|
298
|
|
|
public function getLevel(): ?int |
|
299
|
|
|
{ |
|
300
|
|
|
return $this->level; |
|
301
|
|
|
} |
|
302
|
|
|
|
|
303
|
|
|
/** |
|
304
|
|
|
* {@inheritdoc} |
|
305
|
|
|
*/ |
|
306
|
|
|
public function setLevel(?int $level): void |
|
307
|
|
|
{ |
|
308
|
|
|
$this->level = $level; |
|
309
|
|
|
} |
|
310
|
|
|
|
|
311
|
|
|
/** |
|
312
|
|
|
* {@inheritdoc} |
|
313
|
|
|
*/ |
|
314
|
|
|
public function getPosition(): ?int |
|
315
|
|
|
{ |
|
316
|
|
|
return $this->position; |
|
317
|
|
|
} |
|
318
|
|
|
|
|
319
|
|
|
/** |
|
320
|
|
|
* {@inheritdoc} |
|
321
|
|
|
*/ |
|
322
|
|
|
public function setPosition(?int $position): void |
|
323
|
|
|
{ |
|
324
|
|
|
$this->position = $position; |
|
325
|
|
|
} |
|
326
|
|
|
|
|
327
|
|
|
/** |
|
328
|
|
|
* @param string|null $locale |
|
329
|
|
|
* |
|
330
|
|
|
* @return TaxonTranslationInterface |
|
331
|
|
|
*/ |
|
332
|
|
|
public function getTranslation(?string $locale = null): TranslationInterface |
|
333
|
|
|
{ |
|
334
|
|
|
/** @var TaxonTranslationInterface $translation */ |
|
335
|
|
|
$translation = $this->doGetTranslation($locale); |
|
336
|
|
|
|
|
337
|
|
|
return $translation; |
|
338
|
|
|
} |
|
339
|
|
|
|
|
340
|
|
|
/** |
|
341
|
|
|
* {@inheritdoc} |
|
342
|
|
|
*/ |
|
343
|
|
|
protected function createTranslation(): TaxonTranslation |
|
344
|
|
|
{ |
|
345
|
|
|
return new TaxonTranslation(); |
|
346
|
|
|
} |
|
347
|
|
|
} |
|
348
|
|
|
|