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