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