-
Notifications
You must be signed in to change notification settings - Fork 194
/
Copy pathMenuItem.php
620 lines (495 loc) · 13.5 KB
/
MenuItem.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
<?php
namespace Knp\Menu;
/**
* Default implementation of the ItemInterface
*/
class MenuItem implements ItemInterface
{
/**
* Name of this menu item (used for id by parent menu)
*
* @var string
*/
protected $name;
/**
* Label to output, name is used by default
*
* @var string|null
*/
protected $label;
/**
* Attributes for the item link
*
* @var array<string, string|bool|null>
*/
protected $linkAttributes = [];
/**
* Attributes for the children list
*
* @var array<string, string|bool|null>
*/
protected $childrenAttributes = [];
/**
* Attributes for the item text
*
* @var array<string, string|bool|null>
*/
protected $labelAttributes = [];
/**
* Uri to use in the anchor tag
*
* @var string|null
*/
protected $uri;
/**
* Attributes for the item
*
* @var array<string, string|bool|null>
*/
protected $attributes = [];
/**
* Extra stuff associated to the item
*
* @var array<string, mixed>
*/
protected $extras = [];
/**
* Whether the item is displayed
*
* @var bool
*/
protected $display = true;
/**
* Whether the children of the item are displayed
*
* @var bool
*/
protected $displayChildren = true;
/**
* Child items
*
* @var array<string, ItemInterface>
*/
protected $children = [];
/**
* Parent item
*
* @var ItemInterface|null
*/
protected $parent;
/**
* whether the item is current. null means unknown
*
* @var bool|null
*/
protected $isCurrent;
/**
* @var FactoryInterface
*/
protected $factory;
/**
* Class constructor
*
* @param string $name The name of this menu, which is how its parent will
* reference it. Also used as label if label not specified
*/
public function __construct(string $name, FactoryInterface $factory)
{
$this->name = $name;
$this->factory = $factory;
}
public function setFactory(FactoryInterface $factory): ItemInterface
{
$this->factory = $factory;
return $this;
}
public function getName(): string
{
return $this->name;
}
public function setName(string $name): ItemInterface
{
if ($this->name === $name) {
return $this;
}
$parent = $this->getParent();
if (null !== $parent && isset($parent[$name])) {
throw new \InvalidArgumentException('Cannot rename item, name is already used by sibling.');
}
$oldName = $this->name;
$this->name = $name;
if (null !== $parent) {
$names = \array_keys($parent->getChildren());
$items = \array_values($parent->getChildren());
$offset = \array_search($oldName, $names);
$names[$offset] = $name;
if (false === $children = \array_combine($names, $items)) {
throw new \InvalidArgumentException('Number of elements is not matching.');
}
$parent->setChildren($children);
}
return $this;
}
public function getUri(): ?string
{
return $this->uri;
}
public function setUri(?string $uri): ItemInterface
{
$this->uri = $uri;
return $this;
}
public function getLabel(): string
{
return $this->label ?? $this->name;
}
public function setLabel(?string $label): ItemInterface
{
$this->label = $label;
return $this;
}
public function getAttributes(): array
{
return $this->attributes;
}
public function setAttributes(array $attributes): ItemInterface
{
$this->attributes = $attributes;
return $this;
}
public function getAttribute(string $name, $default = null)
{
return $this->attributes[$name] ?? $default;
}
public function setAttribute(string $name, $value): ItemInterface
{
$this->attributes[$name] = $value;
return $this;
}
public function getLinkAttributes(): array
{
return $this->linkAttributes;
}
public function setLinkAttributes(array $linkAttributes): ItemInterface
{
$this->linkAttributes = $linkAttributes;
return $this;
}
public function getLinkAttribute(string $name, $default = null)
{
return $this->linkAttributes[$name] ?? $default;
}
public function setLinkAttribute(string $name, $value): ItemInterface
{
$this->linkAttributes[$name] = $value;
return $this;
}
public function getChildrenAttributes(): array
{
return $this->childrenAttributes;
}
public function setChildrenAttributes(array $childrenAttributes): ItemInterface
{
$this->childrenAttributes = $childrenAttributes;
return $this;
}
public function getChildrenAttribute(string $name, $default = null)
{
return $this->childrenAttributes[$name] ?? $default;
}
public function setChildrenAttribute(string $name, $value): ItemInterface
{
$this->childrenAttributes[$name] = $value;
return $this;
}
public function getLabelAttributes(): array
{
return $this->labelAttributes;
}
public function setLabelAttributes(array $labelAttributes): ItemInterface
{
$this->labelAttributes = $labelAttributes;
return $this;
}
public function getLabelAttribute(string $name, $default = null)
{
return $this->labelAttributes[$name] ?? $default;
}
public function setLabelAttribute(string $name, $value): ItemInterface
{
$this->labelAttributes[$name] = $value;
return $this;
}
public function getExtras(): array
{
return $this->extras;
}
public function setExtras(array $extras): ItemInterface
{
$this->extras = $extras;
return $this;
}
public function getExtra(string $name, $default = null)
{
return $this->extras[$name] ?? $default;
}
public function setExtra(string $name, $value): ItemInterface
{
$this->extras[$name] = $value;
return $this;
}
public function getDisplayChildren(): bool
{
return $this->displayChildren;
}
public function setDisplayChildren(bool $bool): ItemInterface
{
$this->displayChildren = $bool;
return $this;
}
public function isDisplayed(): bool
{
return $this->display;
}
public function setDisplay(bool $bool): ItemInterface
{
$this->display = $bool;
return $this;
}
public function addChild($child, array $options = []): ItemInterface
{
if (!$child instanceof ItemInterface) {
$child = $this->factory->createItem($child, $options);
} elseif (null !== $child->getParent()) {
throw new \InvalidArgumentException('Cannot add menu item as child, it already belongs to another menu (e.g. has a parent).');
}
$child->setParent($this);
$this->children[$child->getName()] = $child;
return $child;
}
public function getChild(string $name): ?ItemInterface
{
return $this->children[$name] ?? null;
}
public function reorderChildren(array $order): ItemInterface
{
if (\count($order) !== $this->count()) {
throw new \InvalidArgumentException('Cannot reorder children, order does not contain all children.');
}
$newChildren = [];
foreach ($order as $name) {
if (!isset($this->children[$name])) {
throw new \InvalidArgumentException('Cannot find children named '.$name);
}
$child = $this->children[$name];
$newChildren[$name] = $child;
}
$this->setChildren($newChildren);
return $this;
}
public function copy(): ItemInterface
{
$newMenu = clone $this;
$newMenu->setChildren([]);
$newMenu->setParent();
foreach ($this->getChildren() as $child) {
$newMenu->addChild($child->copy());
}
return $newMenu;
}
public function getLevel(): int
{
return $this->parent ? $this->parent->getLevel() + 1 : 0;
}
public function getRoot(): ItemInterface
{
$obj = $this;
do {
$found = $obj;
} while ($obj = $obj->getParent());
return $found;
}
public function isRoot(): bool
{
return null === $this->parent;
}
public function getParent(): ?ItemInterface
{
return $this->parent;
}
public function setParent(?ItemInterface $parent = null): ItemInterface
{
if ($parent === $this) {
throw new \InvalidArgumentException('Item cannot be a child of itself');
}
$this->parent = $parent;
return $this;
}
public function getChildren(): array
{
return $this->children;
}
public function setChildren(array $children): ItemInterface
{
$this->children = $children;
return $this;
}
public function removeChild($name): ItemInterface
{
$name = $name instanceof ItemInterface ? $name->getName() : $name;
if (isset($this->children[$name])) {
// unset the child and reset it so it looks independent
$this->children[$name]->setParent(null);
unset($this->children[$name]);
}
return $this;
}
public function getFirstChild(): ItemInterface
{
if (empty($this->children)) {
throw new \LogicException('Cannot get first child: there are no children.');
}
return \reset($this->children);
}
public function getLastChild(): ItemInterface
{
if (empty($this->children)) {
throw new \LogicException('Cannot get last child: there are no children.');
}
return \end($this->children);
}
public function hasChildren(): bool
{
foreach ($this->children as $child) {
if ($child->isDisplayed()) {
return true;
}
}
return false;
}
public function setCurrent(?bool $bool): ItemInterface
{
$this->isCurrent = $bool;
return $this;
}
public function isCurrent(): ?bool
{
return $this->isCurrent;
}
public function isLast(): bool
{
// if this is root, then return false
if (null === $this->parent) {
return false;
}
return $this->parent->getLastChild() === $this;
}
public function isFirst(): bool
{
// if this is root, then return false
if (null === $this->parent) {
return false;
}
return $this->parent->getFirstChild() === $this;
}
public function actsLikeFirst(): bool
{
// root items are never "marked" as first
if (null === $this->parent) {
return false;
}
// A menu acts like first only if it is displayed
if (!$this->isDisplayed()) {
return false;
}
// if we're first and visible, we're first, period.
if ($this->isFirst()) {
return true;
}
$children = $this->parent->getChildren();
foreach ($children as $child) {
// loop until we find a visible menu. If its this menu, we're first
if ($child->isDisplayed()) {
return $child->getName() === $this->getName();
}
}
return false;
}
public function actsLikeLast(): bool
{
// root items are never "marked" as last
if (null === $this->parent) {
return false;
}
// A menu acts like last only if it is displayed
if (!$this->isDisplayed()) {
return false;
}
// if we're last and visible, we're last, period.
if ($this->isLast()) {
return true;
}
$children = \array_reverse($this->parent->getChildren());
foreach ($children as $child) {
// loop until we find a visible menu. If its this menu, we're first
if ($child->isDisplayed()) {
return $child->getName() === $this->getName();
}
}
return false;
}
/**
* Implements Countable
*/
public function count(): int
{
return \count($this->children);
}
/**
* Implements IteratorAggregate
*/
public function getIterator(): \Traversable
{
return new \ArrayIterator($this->children);
}
/**
* Implements ArrayAccess
*
* @param string $offset
*/
public function offsetExists($offset): bool
{
return isset($this->children[$offset]);
}
/**
* Implements ArrayAccess
*
* @param string $offset
*
* @return ItemInterface|null
*/
#[\ReturnTypeWillChange]
public function offsetGet($offset)
{
return $this->getChild($offset);
}
/**
* Implements ArrayAccess
*
* @param string $offset
* @param string|null $value
*/
public function offsetSet($offset, $value): void
{
$this->addChild($offset)->setLabel($value);
}
/**
* Implements ArrayAccess
*
* @param string $offset
*/
public function offsetUnset($offset): void
{
$this->removeChild($offset);
}
}