Completed
Push — master ( 4aa783...02edd9 )
by Craig
05:41
created

MenuItemEntity::getAttribute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Zikula package.
7
 *
8
 * Copyright Zikula - https://ziku.la/
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Zikula\MenuModule\Entity;
15
16
use Doctrine\Common\Collections\ArrayCollection;
17
use Doctrine\ORM\Mapping as ORM;
18
use Gedmo\Mapping\Annotation as Gedmo;
19
use Knp\Menu\NodeInterface;
20
use Symfony\Component\Validator\Constraints as Assert;
21
use Zikula\Bundle\CoreBundle\Doctrine\EntityAccess;
22
23
/**
24
 * Represents one menu item in a nested set.
25
 * Extends NodeInterface in order to integrate with KnpMenu.
26
 *
27
 * @ORM\Entity(repositoryClass="Zikula\MenuModule\Entity\Repository\MenuItemRepository")
28
 * @ORM\Table(name="menu_items")
29
 * @Gedmo\Tree(type="nested")
30
 */
31
class MenuItemEntity extends EntityAccess implements NodeInterface
32
{
33
    /**
34
     * @ORM\Column(type="integer")
35
     * @ORM\Id
36
     * @ORM\GeneratedValue
37
     * @var int
38
     */
39
    private $id;
40
41
    /**
42
     * @ORM\Column(length=64)
43
     * @Assert\Length(min="0", max="64", allowEmptyString="false")
44
     * @var string
45
     */
46
    private $title;
47
48
    /**
49
     * @Gedmo\TreeLeft
50
     * @ORM\Column(type="integer")
51
     * @var int
52
     */
53
    private $lft;
54
55
    /**
56
     * @Gedmo\TreeLevel
57
     * @ORM\Column(type="integer")
58
     * @var int
59
     */
60
    private $lvl;
61
62
    /**
63
     * @Gedmo\TreeRight
64
     * @ORM\Column(type="integer")
65
     * @var int
66
     */
67
    private $rgt;
68
69
    /**
70
     * @Gedmo\TreeRoot
71
     * @ORM\ManyToOne(targetEntity="MenuItemEntity")
72
     * @ORM\JoinColumn(referencedColumnName="id", onDelete="CASCADE")
73
     * @var self
74
     */
75
    private $root;
76
77
    /**
78
     * @Gedmo\TreeParent
79
     * @ORM\ManyToOne(targetEntity="MenuItemEntity", inversedBy="children")
80
     * @ORM\JoinColumn(referencedColumnName="id", onDelete="CASCADE")
81
     * @var self
82
     */
83
    private $parent;
84
85
    /**
86
     * @ORM\OneToMany(targetEntity="MenuItemEntity", mappedBy="parent")
87
     * @ORM\OrderBy({"lft" = "ASC"})
88
     */
89
    private $children;
90
91
    /**
92
     * @ORM\Column(type="array")
93
     * possible keys
94
     * [
95
     *  'route' => null,
96
     *  'routeParameters' => [],
97
     *  'icon' => null,
98
     *  'uri' => null,
99
     *  'label' => null,
100
     *  'attributes' => [],
101
     *  'linkAttributes' => [],
102
     *  'childrenAttributes' => [],
103
     *  'labelAttributes' => [],
104
     *  'extras' => [],
105
     *  'current' => null,
106
     *  'display' => true,
107
     *  'displayChildren' => true,
108
     * ]
109
     */
110
    private $options;
111
112
    /**
113
     * MenuItemEntity constructor.
114
     */
115
    public function __construct()
116
    {
117
        $this->title = '';
118
        $this->options = []; /*new ArrayCollection();
119
        $this->options = [
120
            'routeParameters' => [],
121
            'attributes' => [],
122
            'linkAttributes' => [],
123
            'childrenAttributes' => [],
124
            'labelAttributes' => [],
125
            'extras' => [],
126
            'display' => true,
127
            'displayChildren' => true,
128
        ];*/
129
        $this->children = new ArrayCollection();
130
    }
131
132
    public function getId(): ?int
133
    {
134
        return $this->id;
135
    }
136
137
    public function setTitle(string $title): void
138
    {
139
        $this->title = $title;
140
    }
141
142
    public function getTitle(): string
143
    {
144
        return $this->title;
145
    }
146
147
    public function getLft(): int
148
    {
149
        return $this->lft;
150
    }
151
152
    public function getLvl(): int
153
    {
154
        return $this->lvl;
155
    }
156
157
    public function getRgt(): int
158
    {
159
        return $this->rgt;
160
    }
161
162
    public function setRoot(self $root): void
163
    {
164
        $this->root = $root;
165
    }
166
167
    public function getRoot(): self
168
    {
169
        return $this->root;
170
    }
171
172
    public function setParent(self $parent = null): void
173
    {
174
        $this->parent = $parent;
175
    }
176
177
    public function getParent(): ?self
178
    {
179
        return $this->parent;
180
    }
181
182
    public function getName(): string
183
    {
184
        return $this->title;
185
    }
186
187
    public function getOptions(): array
188
    {
189
        return $this->options;
190
    }
191
192
    public function getOption(string $name, $default = null)
193
    {
194
        return $this->options[$name] ?? $default;
195
    }
196
197
    public function setOptions(array $options = []): void
198
    {
199
        $this->options = $options;
200
    }
201
202
    public function setOption(string $name, $value): void
203
    {
204
        $this->options[$name] = $value;
205
    }
206
207
    public function removeOption(string $name): void
208
    {
209
        unset($this->options[$name]);
210
    }
211
212
    public function hasOption(string $name): bool
213
    {
214
        return isset($this->options[$name]);
215
    }
216
217
    public function hasOptions(): bool
218
    {
219
        return !empty($this->options);
220
    }
221
222
    public function getChildren(): \Traversable
223
    {
224
        return $this->children;
225
    }
226
227
    public function getAttributes(): array
228
    {
229
        return $this->hasAttributes() ? $this->options['attributes'] : [];
230
    }
231
232
    public function getAttribute(string $name, $default = null)
233
    {
234
        return $this->options['attributes'][$name] ?? $default;
235
    }
236
237
    public function setAttributes(array $attributes = []): void
238
    {
239
        $this->options['attributes'] = $attributes;
240
    }
241
242
    public function setAttribute(string $name, $value): void
243
    {
244
        $this->options['attributes'][$name] = $value;
245
    }
246
247
    public function removeAttribute(string $name): void
248
    {
249
        unset($this->options['attributes'][$name]);
250
    }
251
252
    public function hasAttributes(): bool
253
    {
254
        return !empty($this->options['attributes']);
255
    }
256
257
    public function getLinkAttributes(): array
258
    {
259
        return $this->hasLinkAttributes() ? $this->options['linkAttributes'] : [];
260
    }
261
262
    public function getLinkAttribute(string $name, $default = null)
263
    {
264
        return $this->options['linkAttributes'][$name] ?? $default;
265
    }
266
267
    public function setLinkAttributes(array $attributes = []): void
268
    {
269
        $this->options['linkAttributes'] = $attributes;
270
    }
271
272
    public function setLinkAttribute(string $name, $value): void
273
    {
274
        $this->options['linkAttributes'][$name] = $value;
275
    }
276
277
    public function removeLinkAttribute(string $name): void
278
    {
279
        unset($this->options['linkAttributes'][$name]);
280
    }
281
282
    public function hasLinkAttributes(): bool
283
    {
284
        return !empty($this->options['linkAttributes']);
285
    }
286
287
    public function getChildrenAttributes(): array
288
    {
289
        return $this->hasChildrenAttributes() ? $this->options['childrenAttributes'] : [];
290
    }
291
292
    public function getChildrenAttribute(string $name, $default = null)
293
    {
294
        return $this->options['childrenAttributes'][$name] ?? $default;
295
    }
296
297
    public function setChildrenAttributes(array $attributes = []): void
298
    {
299
        $this->options['childrenAttributes'] = $attributes;
300
    }
301
302
    public function setChildrenAttribute(string $name, $value): void
303
    {
304
        $this->options['childrenAttributes'][$name] = $value;
305
    }
306
307
    public function removeChildrenAttribute(string $name): void
308
    {
309
        unset($this->options['childrenAttributes'][$name]);
310
    }
311
312
    public function hasChildrenAttributes(): bool
313
    {
314
        return !empty($this->options['childrenAttributes']);
315
    }
316
317
    public function getLabelAttributes(): array
318
    {
319
        return $this->hasLabelAttributes() ? $this->options['labelAttributes'] : [];
320
    }
321
322
    public function getLabelAttribute(string $name, $default = null)
323
    {
324
        return $this->options['labelAttributes'][$name] ?? $default;
325
    }
326
327
    public function setLabelAttributes(array $attributes = []): void
328
    {
329
        $this->options['labelAttributes'] = $attributes;
330
    }
331
332
    public function setLabelAttribute(string $name, $value): void
333
    {
334
        $this->options['labelAttributes'][$name] = $value;
335
    }
336
337
    public function removeLabelAttribute(string $name): void
338
    {
339
        unset($this->options['labelAttributes'][$name]);
340
    }
341
342
    public function hasLabelAttributes(): bool
343
    {
344
        return !empty($this->options['labelAttributes']);
345
    }
346
347
    public function getExtras(): array
348
    {
349
        return $this->hasExtras() ? $this->options['extras'] : [];
350
    }
351
352
    public function getExtra(string $name, $default = null)
353
    {
354
        return $this->options['extras'][$name] ?? $default;
355
    }
356
357
    public function setExtras(array $attributes = []): void
358
    {
359
        $this->options['extras'] = $attributes;
360
    }
361
362
    public function setExtra(string $name, $value): void
363
    {
364
        $this->options['extras'][$name] = $value;
365
    }
366
367
    public function removeExtra(string $name): void
368
    {
369
        unset($this->options['extras'][$name]);
370
    }
371
372
    public function hasExtras(): bool
373
    {
374
        return !empty($this->options['extras']);
375
    }
376
377
    public function toJson($prefix = ''): string
378
    {
379
        return json_encode([
380
            'id' => $prefix . $this->id,
381
            'title' => $this->title,
382
            'text' => $this->title,
383
            'options' => $this->options,
384
            'parent' => null !== $this->getParent() ? $this->getParent()->getId() : null,
385
            'root' => null !== $this->getRoot() ? $this->getRoot()->getId() : null
386
        ]);
387
    }
388
}
389