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