1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Nexendrie\Menu; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Menu item |
8
|
|
|
* |
9
|
|
|
* @author Jakub Konečný |
10
|
|
|
* @property string $link |
11
|
|
|
* @property string $rawLink |
12
|
|
|
* @property string $text |
13
|
|
|
* @property-read bool $allowed |
14
|
|
|
*/ |
15
|
1 |
|
class MenuItem extends Collection { |
16
|
|
|
/** @var string */ |
17
|
|
|
protected $text; |
18
|
|
|
/** @var string */ |
19
|
|
|
protected $link; |
20
|
|
|
/** @var string */ |
21
|
|
|
protected $rawLink; |
22
|
|
|
/** @var array[] of [IMenuItemCondition, string] */ |
23
|
|
|
protected $conditions = []; |
24
|
|
|
/** @var IMenuItemLinkRender[] */ |
25
|
|
|
protected $linkRenders = []; |
26
|
|
|
|
27
|
|
|
public function __construct(string $link, string $text) { |
28
|
1 |
|
parent::__construct(); |
29
|
1 |
|
$this->setRawLink($link); |
30
|
1 |
|
$this->text = $text; |
31
|
1 |
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @deprecated Access the property directly |
35
|
|
|
*/ |
36
|
|
|
public function getLink(): string { |
37
|
1 |
|
$link = $this->link; |
38
|
1 |
|
foreach($this->linkRenders as $render) { |
39
|
1 |
|
if($render->isApplicable($this->link)) { |
40
|
1 |
|
$link = $render->renderLink($this->link); |
41
|
1 |
|
break; |
42
|
|
|
} |
43
|
|
|
} |
44
|
1 |
|
return $link; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @deprecated Access the property directly |
49
|
|
|
*/ |
50
|
|
|
public function setLink(string $link): void { |
51
|
1 |
|
$this->setRawLink($link); |
52
|
1 |
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @deprecated Access the property directly |
56
|
|
|
*/ |
57
|
|
|
public function getRawLink(): string { |
58
|
1 |
|
return $this->link; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
protected function setRawLink(string $rawLink): void { |
62
|
1 |
|
$this->rawLink = $this->link = $rawLink; |
63
|
1 |
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @deprecated Access the property directly |
67
|
|
|
*/ |
68
|
|
|
public function getText(): string { |
69
|
1 |
|
return $this->text; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @deprecated Access the property directly |
74
|
|
|
*/ |
75
|
|
|
public function setText(string $text): void { |
76
|
1 |
|
$this->text = $text; |
77
|
1 |
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param mixed $parameter |
81
|
|
|
*/ |
82
|
|
|
public function addCondition(IMenuItemCondition $condition, $parameter): void { |
83
|
1 |
|
$this->conditions[$condition->getName()] = [$condition, $parameter]; |
84
|
1 |
|
} |
85
|
|
|
|
86
|
|
|
public function addLinkRender(IMenuItemLinkRender $render): void { |
87
|
1 |
|
$this->linkRenders[$render->getName()] = $render; |
88
|
1 |
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @deprecated Access the property directly |
92
|
|
|
*/ |
93
|
|
|
public function isAllowed(): bool { |
94
|
1 |
|
foreach($this->conditions as $condition) { |
95
|
1 |
|
if(!$condition[0]->isAllowed($condition[1])) { |
96
|
1 |
|
return false; |
97
|
|
|
} |
98
|
|
|
} |
99
|
1 |
|
return true; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
?> |