-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMenuItem.php
More file actions
107 lines (83 loc) · 2.24 KB
/
MenuItem.php
File metadata and controls
107 lines (83 loc) · 2.24 KB
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
<?php declare(strict_types=1);
namespace DynamicComponents;
use BadMethodCallException;
use Webmozart\Assert\Assert;
use function get_class;
class MenuItem extends \UI\MenuItem
{
private $enabled = true;
/** @var string|null */
private $name;
/** @var callable|null */
private $onClick;
/** @var Menu|null */
private $parent;
public function disable(): void
{
$this->enabled = false;
parent::disable();
}
public function enable(): void
{
$this->enabled = true;
parent::enable();
}
public function getName(): ?string
{
return $this->name;
}
public function getParent(): Menu
{
if (!$this->parent) {
$class = static::class;
throw new BadMethodCallException(
"MenuItem ({$class}: {$this->name}) has no parent," .
" it's probably created by a \\UI\\Menu instead of a \\DynamicComponents\\Menu."
);
}
return $this->parent;
}
public function isEnabled(): bool
{
return $this->enabled;
}
/**
* @internal
*/
public function setName(string $name): void
{
$class = static::class;
Assert::notEmpty($name, "Cannot set an empty name on a {$class}.");
if ($this->name) {
throw new BadMethodCallException(
"Cannot set a name ({$name}) on an already named MenuItem ({$class}: {$this->name})."
);
}
$this->name = $name;
}
public function setOnClick(callable $onClick): void
{
$this->onClick = $onClick;
}
/**
* @internal
*/
public function setParent(Menu $menu): void
{
if (!$menu->hasMenuItem($this)) {
$parent = get_class($menu);
$child = static::class;
throw new BadMethodCallException(
"Trying to set Menu ({$parent}: {$menu->getName()}) as parent of MenuItem ({$child}: {$this->name})." .
' Can only set actual parent as parent.'
);
}
$this->parent = $menu;
}
protected function onClick(): void
{
if ($onClick = $this->onClick) {
$onClick($this);
}
}
}