-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMenu.php
71 lines (54 loc) · 1.64 KB
/
Menu.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
<?php declare(strict_types=1);
namespace DynamicComponents;
use InvalidArgumentException;
use SplObjectStorage;
use UI\Menu as UiMenu;
use UI\MenuItem as UiMenuItem;
use Webmozart\Assert\Assert;
class Menu extends UiMenu
{
/** @var string */
private $name;
/** @var SplObjectStorage */
private $items;
public function __construct(string $name)
{
parent::__construct($name);
$this->name = $name;
$this->items = new SplObjectStorage();
}
public function add(string $name): MenuItem
{
return $this->append($name, MenuItem::class);
}
public function addAs(string $type, string $name): UiMenuItem
{
return $this->append($name, $type);
}
public function append(string $name = '', string $type = MenuItem::class): UiMenuItem
{
Assert::notEmpty($name, "Provide a \$name to append to Menu '{$this->name}'.");
$menuItem = parent::append($name, $type);
$this->items->attach($menuItem, $name);
if ($menuItem instanceof MenuItem) {
$menuItem->setName($name);
$menuItem->setParent($this);
}
return $menuItem;
}
public function hasMenuItem(UiMenuItem $menuItem): bool
{
return $this->items->contains($menuItem);
}
public function getName(): string
{
return $this->name;
}
public function getMenuItemName(UiMenuItem $menuItem): string
{
if (!$this->hasMenuItem($menuItem)) {
throw new InvalidArgumentException("MenuItem is not a child of Menu '{$this->name}'.");
}
return $this->items->offsetGet($menuItem);
}
}