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->chidlren = 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 chidlren of Item |
142
|
|
|
* |
143
|
|
|
* @return null\ArrayCollection (Item) |
144
|
|
|
*/ |
145
|
|
|
public function getChidlren() |
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 = null) |
158
|
|
|
{ |
159
|
|
|
foreach ($collection as $item) { |
|
|
|
|
160
|
|
|
if($this->chidlren->contains($item) === false) { |
161
|
|
|
$this->setChidlren($item); |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
return $this; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Set chidlren of Item |
170
|
|
|
* |
171
|
|
|
* @param Item $item |
172
|
|
|
* |
173
|
|
|
* @return self |
174
|
|
|
*/ |
175
|
|
|
public function setChidlren(ItemInterface $item) |
176
|
|
|
{ |
177
|
|
|
$this->chidlren->add($item); |
178
|
|
|
|
179
|
|
|
return $this; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Get name displayed in Item |
184
|
|
|
* |
185
|
|
|
* @return string |
186
|
|
|
*/ |
187
|
|
|
public function getName() |
188
|
|
|
{ |
189
|
|
|
return $this->name; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* Set name displayed in Item |
194
|
|
|
* |
195
|
|
|
* @param string |
196
|
|
|
* |
197
|
|
|
* @return self |
198
|
|
|
*/ |
199
|
|
|
public function setName($name) |
200
|
|
|
{ |
201
|
|
|
$this->name = $name; |
202
|
|
|
|
203
|
|
|
return $this; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* Get URL |
208
|
|
|
* |
209
|
|
|
* @return string |
210
|
|
|
*/ |
211
|
|
|
public function getUri() |
212
|
|
|
{ |
213
|
|
|
return $this->uri; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* Set URL |
218
|
|
|
* |
219
|
|
|
* @param string $url |
|
|
|
|
220
|
|
|
* |
221
|
|
|
* @return self |
222
|
|
|
*/ |
223
|
|
|
public function setUri($uri) |
224
|
|
|
{ |
225
|
|
|
$this->uri = $uri; |
226
|
|
|
|
227
|
|
|
return $this; |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* Get position of Item |
232
|
|
|
* |
233
|
|
|
* @return integer |
234
|
|
|
*/ |
235
|
|
|
public function getPosition() |
236
|
|
|
{ |
237
|
|
|
return $this->position; |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* Set position |
243
|
|
|
* |
244
|
|
|
* @param int $position |
245
|
|
|
* |
246
|
|
|
* @return self |
247
|
|
|
*/ |
248
|
|
|
public function setPosition($position) |
249
|
|
|
{ |
250
|
|
|
$this->position = $position; |
251
|
|
|
|
252
|
|
|
return $this; |
253
|
|
|
} |
254
|
|
|
} |
255
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: