|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Alpixel\Bundle\MenuBundle\Entity; |
|
4
|
|
|
|
|
5
|
|
|
use Alpixel\Bundle\MenuBundle\Model\ItemInterface; |
|
6
|
|
|
use Alpixel\Bundle\MenuBundle\Model\MenuInterface; |
|
7
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
|
8
|
|
|
use Doctrine\ORM\Mapping as ORM; |
|
9
|
|
|
use Gedmo\Mapping\Annotation as Gedmo; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* @ORM\Table(name="alpixel_item") |
|
13
|
|
|
* @ORM\Entity |
|
14
|
|
|
*/ |
|
15
|
|
|
class Item implements ItemInterface |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @var integer |
|
19
|
|
|
* |
|
20
|
|
|
* @ORM\Column(name="item_id", type="integer", nullable=false) |
|
21
|
|
|
* @ORM\Id |
|
22
|
|
|
* @ORM\GeneratedValue(strategy="IDENTITY") |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $id; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @Gedmo\SortableGroup |
|
28
|
|
|
* |
|
29
|
|
|
* @ORM\ManyToOne(targetEntity="Alpixel\Bundle\MenuBundle\Entity\Item", inversedBy="children") |
|
30
|
|
|
* @ORM\JoinColumn(name="parent_id", referencedColumnName="item_id") |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $parent; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @ORM\OneToMany(targetEntity="Alpixel\Bundle\MenuBundle\Entity\Item", mappedBy="parent") |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $children; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @ORM\ManyToOne(targetEntity="Alpixel\Bundle\MenuBundle\Entity\Menu", inversedBy="items") |
|
41
|
|
|
* @ORM\JoinColumn(name="menu_id", referencedColumnName="menu_id") |
|
42
|
|
|
*/ |
|
43
|
|
|
protected $menu; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @var string |
|
47
|
|
|
* |
|
48
|
|
|
* @ORM\Column(name="name", type="string", length=255, nullable=false) |
|
49
|
|
|
*/ |
|
50
|
|
|
protected $name; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @var string |
|
54
|
|
|
* |
|
55
|
|
|
* @ORM\Column(name="uri", type="text", nullable=false) |
|
56
|
|
|
*/ |
|
57
|
|
|
protected $uri; |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @Gedmo\SortablePosition |
|
61
|
|
|
* |
|
62
|
|
|
* @ORM\Column(name="position", type="integer", nullable=false) |
|
63
|
|
|
*/ |
|
64
|
|
|
protected $position; |
|
65
|
|
|
|
|
66
|
|
|
public function __construct() |
|
67
|
|
|
{ |
|
68
|
|
|
$this->children = new ArrayCollection(); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Get string defined |
|
73
|
|
|
* |
|
74
|
|
|
* @return string |
|
75
|
|
|
*/ |
|
76
|
|
|
public function __toString() |
|
77
|
|
|
{ |
|
78
|
|
|
return $this->name; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Get Id |
|
83
|
|
|
* |
|
84
|
|
|
* @return integer |
|
85
|
|
|
*/ |
|
86
|
|
|
public function getId() |
|
87
|
|
|
{ |
|
88
|
|
|
return $this->id; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Get menu |
|
93
|
|
|
* |
|
94
|
|
|
* @return Menu |
|
95
|
|
|
*/ |
|
96
|
|
|
public function getMenu() |
|
97
|
|
|
{ |
|
98
|
|
|
return $this->menu; |
|
|
|
|
|
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Set menu |
|
103
|
|
|
* |
|
104
|
|
|
* @param Menu $menu |
|
105
|
|
|
* |
|
106
|
|
|
* @return self |
|
107
|
|
|
*/ |
|
108
|
|
|
public function setMenu(MenuInterface $menu) |
|
109
|
|
|
{ |
|
110
|
|
|
$this->menu = $menu; |
|
111
|
|
|
|
|
112
|
|
|
return $this; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Get parent Item |
|
117
|
|
|
* |
|
118
|
|
|
* @return null\Item |
|
119
|
|
|
*/ |
|
120
|
|
|
public function getParent() |
|
121
|
|
|
{ |
|
122
|
|
|
return $this->parent; |
|
|
|
|
|
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Set parent Item |
|
127
|
|
|
* |
|
128
|
|
|
* @param ItemInterface $menu |
|
|
|
|
|
|
129
|
|
|
* |
|
130
|
|
|
* @return self |
|
131
|
|
|
*/ |
|
132
|
|
|
public function setParent(ItemInterface $item = null) |
|
133
|
|
|
{ |
|
134
|
|
|
$this->parent = $item; |
|
135
|
|
|
|
|
136
|
|
|
return $this; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* Get children of Item |
|
142
|
|
|
* |
|
143
|
|
|
* @return ArrayCollection (Item) |
|
144
|
|
|
*/ |
|
145
|
|
|
public function getChildren() |
|
146
|
|
|
{ |
|
147
|
|
|
return $this->children; |
|
|
|
|
|
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* Set Item from ArrayCollection |
|
152
|
|
|
* |
|
153
|
|
|
* @param null\ArrayCollection |
|
154
|
|
|
* |
|
155
|
|
|
* @return self |
|
156
|
|
|
*/ |
|
157
|
|
|
public function addChildren(ArrayCollection $collection) |
|
158
|
|
|
{ |
|
159
|
|
|
foreach ($collection as $item) { |
|
160
|
|
|
$this->setChildren($item); |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
return $this; |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* Set children of Item |
|
168
|
|
|
* |
|
169
|
|
|
* @param Item $item |
|
170
|
|
|
* |
|
171
|
|
|
* @return self |
|
172
|
|
|
*/ |
|
173
|
|
|
public function setChildren(ItemInterface $item) |
|
174
|
|
|
{ |
|
175
|
|
|
if ($this->children->contains($item) === false) { |
|
176
|
|
|
$this->children->add($item); |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
return $this; |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
/** |
|
183
|
|
|
* Remove children |
|
184
|
|
|
* |
|
185
|
|
|
* @param ItemInterface $item |
|
186
|
|
|
* |
|
187
|
|
|
* @return $this |
|
188
|
|
|
*/ |
|
189
|
|
|
public function removeChildren(ItemInterface $item) |
|
190
|
|
|
{ |
|
191
|
|
|
if ($this->children->contains($item) === true) { |
|
192
|
|
|
$this->children->removeElement($item); |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
return $this; |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
/** |
|
199
|
|
|
* Get name displayed in Item |
|
200
|
|
|
* |
|
201
|
|
|
* @return string |
|
202
|
|
|
*/ |
|
203
|
|
|
public function getName() |
|
204
|
|
|
{ |
|
205
|
|
|
return $this->name; |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
/** |
|
209
|
|
|
* Set name displayed in Item |
|
210
|
|
|
* |
|
211
|
|
|
* @param string |
|
212
|
|
|
* |
|
213
|
|
|
* @return self |
|
214
|
|
|
*/ |
|
215
|
|
|
public function setName($name) |
|
216
|
|
|
{ |
|
217
|
|
|
$this->name = $name; |
|
218
|
|
|
|
|
219
|
|
|
return $this; |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
|
/** |
|
223
|
|
|
* Get URL |
|
224
|
|
|
* |
|
225
|
|
|
* @return string |
|
226
|
|
|
*/ |
|
227
|
|
|
public function getUri() |
|
228
|
|
|
{ |
|
229
|
|
|
return $this->uri; |
|
230
|
|
|
} |
|
231
|
|
|
|
|
232
|
|
|
/** |
|
233
|
|
|
* Set URL |
|
234
|
|
|
* |
|
235
|
|
|
* @param string $url |
|
|
|
|
|
|
236
|
|
|
* |
|
237
|
|
|
* @return self |
|
238
|
|
|
*/ |
|
239
|
|
|
public function setUri($uri) |
|
240
|
|
|
{ |
|
241
|
|
|
$this->uri = $uri; |
|
242
|
|
|
|
|
243
|
|
|
return $this; |
|
244
|
|
|
} |
|
245
|
|
|
|
|
246
|
|
|
/** |
|
247
|
|
|
* Get position of Item |
|
248
|
|
|
* |
|
249
|
|
|
* @return integer |
|
250
|
|
|
*/ |
|
251
|
|
|
public function getPosition() |
|
252
|
|
|
{ |
|
253
|
|
|
return $this->position; |
|
254
|
|
|
} |
|
255
|
|
|
|
|
256
|
|
|
|
|
257
|
|
|
/** |
|
258
|
|
|
* Set position |
|
259
|
|
|
* |
|
260
|
|
|
* @param int $position |
|
261
|
|
|
* |
|
262
|
|
|
* @return self |
|
263
|
|
|
*/ |
|
264
|
|
|
public function setPosition($position) |
|
265
|
|
|
{ |
|
266
|
|
|
$this->position = $position; |
|
267
|
|
|
|
|
268
|
|
|
return $this; |
|
269
|
|
|
} |
|
270
|
|
|
} |
|
271
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.