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