|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* YAWIK |
|
4
|
|
|
* |
|
5
|
|
|
* @filesource |
|
6
|
|
|
* @license MIT |
|
7
|
|
|
* @copyright 2013 - 2016 Cross Solution <http://cross-solution.de> |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
/** */ |
|
11
|
|
|
namespace Core\Entity\Tree; |
|
12
|
|
|
|
|
13
|
|
|
use Core\Entity\Collection\ArrayCollection; |
|
14
|
|
|
use Core\Entity\EntityTrait; |
|
15
|
|
|
use Core\Entity\IdentifiableEntityTrait; |
|
16
|
|
|
use Doctrine\Common\Collections\Collection; |
|
17
|
|
|
use \Doctrine\ODM\MongoDB\Mapping\Annotations as ODM; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* base class for trees. |
|
21
|
|
|
* |
|
22
|
|
|
* @ODM\MappedSuperclass |
|
23
|
|
|
* @author Mathias Gelhausen <[email protected]> |
|
24
|
|
|
* @since 0.29 |
|
25
|
|
|
*/ |
|
26
|
|
|
class Node implements NodeInterface |
|
27
|
|
|
{ |
|
28
|
|
|
use EntityTrait, IdentifiableEntityTrait; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Name of this item. |
|
32
|
|
|
* |
|
33
|
|
|
* @ODM\Field(type="string") |
|
34
|
|
|
* @var string |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $name; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Value of this item. |
|
40
|
|
|
* |
|
41
|
|
|
* Used in select form elements. |
|
42
|
|
|
* |
|
43
|
|
|
* @ODM\Field(type="string") |
|
44
|
|
|
* @var string |
|
45
|
|
|
*/ |
|
46
|
|
|
protected $value; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Order priority. |
|
50
|
|
|
* |
|
51
|
|
|
* @ODM\Field(type="int") |
|
52
|
|
|
* @var int |
|
53
|
|
|
*/ |
|
54
|
|
|
protected $priority = 0; |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Child nodes. |
|
58
|
|
|
* |
|
59
|
|
|
* @ODM\ReferenceMany(discriminatorField="_entity", storeAs="dbRef", strategy="set", sort={"priority"="asc"}, cascade="all", orphanRemoval="true") |
|
60
|
|
|
* @var Collection |
|
61
|
|
|
*/ |
|
62
|
|
|
protected $children; |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Parent node. |
|
66
|
|
|
* |
|
67
|
|
|
* @ODM\ReferenceOne(discriminatorField="_entity", storeAs="dbRef", nullable="true") |
|
68
|
|
|
* @var |
|
69
|
|
|
*/ |
|
70
|
|
|
protected $parent; |
|
71
|
|
|
|
|
72
|
|
|
final public static function filterValue($value) |
|
73
|
|
|
{ |
|
74
|
|
|
$value = mb_strtolower($value); |
|
75
|
|
|
$value = str_replace(['ä', 'ö', 'ü', 'ß'], ['ae', 'oe', 'ue', 'ss'], $value); |
|
76
|
|
|
$value = preg_replace(['~[^a-z0-9]~', '~__+~'], '_', $value); |
|
77
|
|
|
|
|
78
|
|
|
return $value; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Creates a new Tree item. |
|
83
|
|
|
* |
|
84
|
|
|
* @param null|string $name |
|
85
|
|
|
* @param null|string $value |
|
86
|
|
|
* @param int $priority |
|
87
|
|
|
*/ |
|
88
|
|
|
public function __construct($name = null, $value = null, $priority = 0) |
|
89
|
|
|
{ |
|
90
|
|
|
if (null !== $name) { |
|
91
|
|
|
$this->setName($name); |
|
92
|
|
|
$this->setValue($value); |
|
93
|
|
|
$this->setPriority($priority); |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Set the name. |
|
99
|
|
|
* |
|
100
|
|
|
* @param string $name |
|
101
|
|
|
* |
|
102
|
|
|
* @return self |
|
103
|
|
|
* @throws \InvalidArgumentException if $name is empty. |
|
104
|
|
|
*/ |
|
105
|
|
|
public function setName($name) |
|
106
|
|
|
{ |
|
107
|
|
|
if (!$name) { |
|
108
|
|
|
throw new \InvalidArgumentException('Name must not be empty.'); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
$this->name = (string) $name; |
|
112
|
|
|
|
|
113
|
|
|
return $this; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
public function getName() |
|
117
|
|
|
{ |
|
118
|
|
|
return $this->name; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* Set the value. |
|
123
|
|
|
* |
|
124
|
|
|
* Used in form selects. |
|
125
|
|
|
* |
|
126
|
|
|
* @param string $value |
|
127
|
|
|
* |
|
128
|
|
|
* @return self |
|
129
|
|
|
* @throws \InvalidArgumentException if $value AND {@link name} are empty. |
|
130
|
|
|
*/ |
|
131
|
|
|
public function setValue($value) |
|
132
|
|
|
{ |
|
133
|
|
|
if (!$value) { |
|
134
|
|
|
if (!$this->getName()) { |
|
135
|
|
|
throw new \InvalidArgumentException('Value must not be empty.'); |
|
136
|
|
|
} |
|
137
|
|
|
$value = self::filterValue($this->getName()); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
$this->value = (string) $value; |
|
141
|
|
|
|
|
142
|
|
|
return $this; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
public function getValue() |
|
146
|
|
|
{ |
|
147
|
|
|
if (!$this->value) { |
|
148
|
|
|
$this->setValue(null); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
return $this->value; |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
public function getValueWithParents($withRoot = false, $useNames = false) |
|
155
|
|
|
{ |
|
156
|
|
|
$parts = [ ($useNames ? $this->getName() : $this->getValue()) ]; |
|
157
|
|
|
$item = $this; |
|
158
|
|
|
|
|
159
|
|
|
while ($item = $item->getParent()) { |
|
160
|
|
|
$parts[] = $useNames ? $item->getName() : $item->getValue(); |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
if (!$withRoot) { |
|
164
|
|
|
array_pop($parts); // No root node. |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
$parts = array_reverse($parts); |
|
168
|
|
|
$value = join(($useNames ? ' | ' : '-'), $parts); |
|
169
|
|
|
|
|
170
|
|
|
return $value; |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
public function getNameWithParents($withRoot = false) |
|
174
|
|
|
{ |
|
175
|
|
|
return $this->getValueWithParents($withRoot, true); |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
public function setPriority($priority) |
|
179
|
|
|
{ |
|
180
|
|
|
$this->priority = (int) $priority; |
|
181
|
|
|
|
|
182
|
|
|
return $this; |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
public function getPriority() |
|
186
|
|
|
{ |
|
187
|
|
|
return $this->priority; |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
public function setChildren(Collection $children) |
|
191
|
|
|
{ |
|
192
|
|
|
$this->children = $children; |
|
193
|
|
|
|
|
194
|
|
|
/* @var NodeInterface $child */ |
|
195
|
|
|
foreach ($children as $child) { |
|
196
|
|
|
$child->setParent($this); |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
return $this; |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
public function getChildren() |
|
203
|
|
|
{ |
|
204
|
|
|
if (!$this->children) { |
|
205
|
|
|
$this->setChildren(new ArrayCollection()); |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
return $this->children; |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
public function hasChildren() |
|
212
|
|
|
{ |
|
213
|
|
|
return (bool) $this->getChildren()->count(); |
|
|
|
|
|
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
public function addChild(NodeInterface $child) |
|
217
|
|
|
{ |
|
218
|
|
|
$this->getChildren()->add($child); |
|
|
|
|
|
|
219
|
|
|
$child->setParent($this); |
|
220
|
|
|
|
|
221
|
|
|
return $this; |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
public function removeChild(NodeInterface $child) |
|
225
|
|
|
{ |
|
226
|
|
|
$this->getChildren()->removeElement($child); |
|
|
|
|
|
|
227
|
|
|
|
|
228
|
|
|
return $this; |
|
229
|
|
|
} |
|
230
|
|
|
|
|
231
|
|
|
public function clearChildren() |
|
232
|
|
|
{ |
|
233
|
|
|
$this->getChildren()->clear(); |
|
|
|
|
|
|
234
|
|
|
|
|
235
|
|
|
return $this; |
|
236
|
|
|
} |
|
237
|
|
|
|
|
238
|
|
|
public function setParent(NodeInterface $parent) |
|
239
|
|
|
{ |
|
240
|
|
|
$this->parent = $parent; |
|
241
|
|
|
|
|
242
|
|
|
return $this; |
|
243
|
|
|
} |
|
244
|
|
|
|
|
245
|
|
|
/** |
|
246
|
|
|
* |
|
247
|
|
|
* |
|
248
|
|
|
* @return NodeInterface |
|
249
|
|
|
*/ |
|
250
|
|
|
public function getParent() |
|
251
|
|
|
{ |
|
252
|
|
|
return $this->parent; |
|
253
|
|
|
} |
|
254
|
|
|
} |
|
255
|
|
|
|
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: