|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yiisoft\Yii\Widgets\Helper; |
|
6
|
|
|
|
|
7
|
|
|
use InvalidArgumentException; |
|
8
|
|
|
use Yiisoft\Html\Html; |
|
9
|
|
|
use Yiisoft\Html\Tag\I; |
|
10
|
|
|
use Yiisoft\Html\Tag\Span; |
|
11
|
|
|
|
|
12
|
|
|
final class Normalize |
|
13
|
|
|
{ |
|
14
|
|
|
public static function dropdown(array $items): array |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @psalm-var array[] $items |
|
18
|
|
|
* @psalm-suppress RedundantConditionGivenDocblockType |
|
19
|
|
|
*/ |
|
20
|
|
|
foreach ($items as $i => $child) { |
|
21
|
|
|
if (is_array($child)) { |
|
22
|
|
|
$items[$i]['label'] = self::label($child); |
|
23
|
|
|
/** @var bool */ |
|
24
|
|
|
$items[$i]['active'] = $child['active'] ?? false; |
|
25
|
|
|
/** @var bool */ |
|
26
|
|
|
$items[$i]['disabled'] = $child['disabled'] ?? false; |
|
27
|
|
|
/** @var string */ |
|
28
|
|
|
$items[$i]['link'] = $child['link'] ?? '/'; |
|
29
|
|
|
/** @var string */ |
|
30
|
|
|
$items[$i]['icon'] = $child['icon'] ?? ''; |
|
31
|
|
|
/** @var array */ |
|
32
|
|
|
$items[$i]['iconAttributes'] = $child['iconAttributes'] ?? []; |
|
33
|
|
|
/** @var string */ |
|
34
|
|
|
$items[$i]['iconClass'] = $child['iconClass'] ?? ''; |
|
35
|
|
|
/** @var array */ |
|
36
|
|
|
$items[$i]['iconContainerAttributes'] = $child['iconContainerAttributes'] ?? []; |
|
37
|
|
|
/** @var bool */ |
|
38
|
|
|
$items[$i]['visible'] = $child['visible'] ?? true; |
|
39
|
|
|
|
|
40
|
|
|
if (isset($child['items']) && is_array($child['items'])) { |
|
41
|
|
|
$items[$i]['items'] = self::dropdown($child['items']); |
|
42
|
|
|
} |
|
43
|
|
|
} |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
return $items; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Normalize the given array of items for the menu. |
|
51
|
|
|
* |
|
52
|
|
|
* @param array $items The items to be normalized. |
|
53
|
|
|
* @param string $currentPath The current path. |
|
54
|
|
|
* @param bool $activateItems Whether to activate items. |
|
55
|
|
|
* |
|
56
|
|
|
* @return array The normalized array of items. |
|
57
|
|
|
*/ |
|
58
|
|
|
public static function menu(array $items, string $currentPath, bool $activateItems): array |
|
59
|
|
|
{ |
|
60
|
|
|
/** |
|
61
|
|
|
* @psalm-var array[] $items |
|
62
|
|
|
* @psalm-suppress RedundantConditionGivenDocblockType |
|
63
|
|
|
*/ |
|
64
|
|
|
foreach ($items as $i => $child) { |
|
65
|
|
|
if (is_array($child)) { |
|
66
|
|
|
if (isset($child['items']) && is_array($child['items'])) { |
|
67
|
|
|
$items[$i]['items'] = self::menu($child['items'], $currentPath, $activateItems); |
|
68
|
|
|
} else { |
|
69
|
|
|
$items[$i]['label'] = self::label($child); |
|
70
|
|
|
|
|
71
|
|
|
/** @var string */ |
|
72
|
|
|
$link = $child['link'] ?? '/'; |
|
73
|
|
|
/** @var bool */ |
|
74
|
|
|
$active = $child['active'] ?? false; |
|
75
|
|
|
|
|
76
|
|
|
if ($active === false) { |
|
77
|
|
|
$items[$i]['active'] = self::isItemActive($link, $currentPath, $activateItems); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** @var bool */ |
|
81
|
|
|
$items[$i]['disabled'] = $child['disabled'] ?? false; |
|
82
|
|
|
/** @var string */ |
|
83
|
|
|
$items[$i]['icon'] = $child['icon'] ?? ''; |
|
84
|
|
|
/** @var array */ |
|
85
|
|
|
$items[$i]['iconAttributes'] = $child['iconAttributes'] ?? []; |
|
86
|
|
|
/** @var string */ |
|
87
|
|
|
$items[$i]['iconClass'] = $child['iconClass'] ?? ''; |
|
88
|
|
|
/** @var bool */ |
|
89
|
|
|
$items[$i]['visible'] = $child['visible'] ?? true; |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
return $items; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
public static function renderLabel( |
|
98
|
|
|
string $label, |
|
99
|
|
|
string $icon, |
|
100
|
|
|
array $iconAttributes, |
|
101
|
|
|
string $iconClass, |
|
102
|
|
|
array $iconContainerAttributes |
|
103
|
|
|
): string { |
|
104
|
|
|
$html = ''; |
|
105
|
|
|
|
|
106
|
|
|
if ($iconClass !== '') { |
|
107
|
|
|
Html::addCssClass($iconAttributes, $iconClass); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
if ($icon !== '' || $iconAttributes !== [] || $iconClass !== '') { |
|
111
|
|
|
$i = I::tag()->addAttributes($iconAttributes)->content($icon); |
|
112
|
|
|
$html = Span::tag()->addAttributes($iconContainerAttributes)->content($i)->encode(false)->render(); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
if ($label !== '') { |
|
116
|
|
|
$html .= $label; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
return $html; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Checks whether a menu item is active. |
|
124
|
|
|
* |
|
125
|
|
|
* This is done by checking match that specified in the `url` option of the menu item. |
|
126
|
|
|
* |
|
127
|
|
|
* @param string $link The link of the menu item. |
|
128
|
|
|
* @param string $currentPath The current path. |
|
129
|
|
|
* @param bool $activateItems Whether to activate items having no link. |
|
130
|
|
|
* |
|
131
|
|
|
* @return bool Whether the menu item is active. |
|
132
|
|
|
*/ |
|
133
|
|
|
private static function isItemActive(string $link, string $currentPath, bool $activateItems): bool |
|
134
|
|
|
{ |
|
135
|
|
|
return ($link === $currentPath) && $activateItems; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
private static function label(array $item): string |
|
139
|
|
|
{ |
|
140
|
|
|
if (!isset($item['label'])) { |
|
141
|
|
|
throw new InvalidArgumentException('The "label" option is required.'); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
if (!is_string($item['label'])) { |
|
145
|
|
|
throw new InvalidArgumentException('The "label" option must be a string.'); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
if ($item['label'] === '') { |
|
149
|
|
|
throw new InvalidArgumentException('The "label" cannot be an empty string.'); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** @var bool */ |
|
153
|
|
|
$encode = $item['encode'] ?? true; |
|
154
|
|
|
|
|
155
|
|
|
return $encode ? Html::encode($item['label']) : $item['label']; |
|
156
|
|
|
} |
|
157
|
|
|
} |
|
158
|
|
|
|