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
|
|
|
/** |
73
|
|
|
* Creates a new Tree item. |
74
|
|
|
* |
75
|
|
|
* @param null|string $name |
76
|
|
|
* @param null|string $value |
77
|
|
|
* @param int $priority |
78
|
|
|
*/ |
79
|
|
|
public function __construct($name = null, $value = null, $priority = 0) |
80
|
|
|
{ |
81
|
|
|
if (null !== $name) { |
82
|
|
|
$this->setName($name); |
83
|
|
|
$this->setValue($value); |
84
|
|
|
$this->setPriority($priority); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Set the name. |
90
|
|
|
* |
91
|
|
|
* @param string $name |
92
|
|
|
* |
93
|
|
|
* @return self |
94
|
|
|
* @throws \InvalidArgumentException if $name is empty. |
95
|
|
|
*/ |
96
|
|
|
public function setName($name) |
97
|
|
|
{ |
98
|
|
|
if (!$name) { |
99
|
|
|
throw new \InvalidArgumentException('Name must not be empty.'); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
$this->name = (string) $name; |
103
|
|
|
|
104
|
|
|
return $this; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
public function getName() |
108
|
|
|
{ |
109
|
|
|
return $this->name; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Set the value. |
114
|
|
|
* |
115
|
|
|
* Used in form selects. |
116
|
|
|
* |
117
|
|
|
* @param string $value |
118
|
|
|
* |
119
|
|
|
* @return self |
120
|
|
|
* @throws \InvalidArgumentException if $value AND {@link name} are empty. |
121
|
|
|
*/ |
122
|
|
|
public function setValue($value) |
123
|
|
|
{ |
124
|
|
|
if (!$value) { |
125
|
|
|
if (!$this->getName()) { |
126
|
|
|
throw new \InvalidArgumentException('Value must not be empty.'); |
127
|
|
|
} |
128
|
|
|
$value = strtolower(str_replace(' ', '-', $this->getName())); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
$this->value = (string) $value; |
132
|
|
|
|
133
|
|
|
return $this; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
public function getValue() |
137
|
|
|
{ |
138
|
|
|
if (!$this->value) { |
139
|
|
|
$this->setValue(null); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
return $this->value; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
public function setPriority($priority) |
146
|
|
|
{ |
147
|
|
|
$this->priority = (int) $priority; |
148
|
|
|
|
149
|
|
|
return $this; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
public function getPriority() |
153
|
|
|
{ |
154
|
|
|
return $this->priority; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
public function setChildren(Collection $children) |
158
|
|
|
{ |
159
|
|
|
$this->children = $children; |
160
|
|
|
|
161
|
|
|
/* @var NodeInterface $child */ |
162
|
|
|
foreach ($children as $child) { |
163
|
|
|
$child->setParent($this); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
return $this; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
public function getChildren() |
170
|
|
|
{ |
171
|
|
|
if (!$this->children) { |
172
|
|
|
$this->setChildren(new ArrayCollection()); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
return $this->children; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
public function hasChildren() |
179
|
|
|
{ |
180
|
|
|
return (bool) $this->getChildren()->count(); |
|
|
|
|
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
public function addChild(NodeInterface $child) |
184
|
|
|
{ |
185
|
|
|
$this->getChildren()->add($child); |
|
|
|
|
186
|
|
|
$child->setParent($this); |
187
|
|
|
|
188
|
|
|
return $this; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
public function removeChild(NodeInterface $child) |
192
|
|
|
{ |
193
|
|
|
$this->getChildren()->removeElement($child); |
|
|
|
|
194
|
|
|
|
195
|
|
|
return $this; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
public function clearChildren() |
199
|
|
|
{ |
200
|
|
|
$this->getChildren()->clear(); |
|
|
|
|
201
|
|
|
|
202
|
|
|
return $this; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
public function setParent(NodeInterface $parent) |
206
|
|
|
{ |
207
|
|
|
$this->parent = $parent; |
208
|
|
|
|
209
|
|
|
return $this; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
public function getParent() |
213
|
|
|
{ |
214
|
|
|
return $this->parent; |
215
|
|
|
} |
216
|
|
|
} |
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: