-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAdminMenuItem.php
200 lines (155 loc) · 4.58 KB
/
AdminMenuItem.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
<?php
declare(strict_types=1);
namespace Snicco\Component\HttpRouting\Routing\Admin;
use LogicException;
use Snicco\Component\HttpRouting\Routing\Route\Route;
use Snicco\Component\HttpRouting\Routing\UrlPath;
use Snicco\Component\StrArr\Str;
use Webmozart\Assert\Assert;
use function count;
use function mb_convert_case;
final class AdminMenuItem
{
/**
* @var string
*/
public const PAGE_TITLE = 'page_title';
/**
* @var string
*/
public const MENU_TITLE = 'menu_title';
/**
* @var string
*/
public const ICON = 'icon';
/**
* @var string
*/
public const CAPABILITY = 'capability';
/**
* @var string
*/
public const POSITION = 'position';
private string $page_title;
private string $menu_title;
private string $menu_slug;
private ?string $capability;
private ?string $icon;
private ?int $position;
private ?string $parent_slug;
public function __construct(
string $page_title,
string $menu_title,
string $menu_slug,
?string $capability = null,
?string $icon = null,
?int $position = null,
?string $parent_slug = null
) {
Assert::stringNotEmpty($page_title, '$page_title cant be empty.');
$this->page_title = $page_title;
Assert::stringNotEmpty($menu_title, '$menu_title cant be empty.');
$this->menu_title = $menu_title;
Assert::stringNotEmpty($menu_slug);
$this->menu_slug = $menu_slug;
if (null !== $capability) {
Assert::stringNotEmpty($capability, '$capability has to be null or non empty string.');
}
$this->capability = $capability;
if (null !== $icon) {
Assert::stringNotEmpty($icon, '$icon has to be null or non empty string.');
}
$this->icon = $icon;
$this->position = $position;
if (null !== $parent_slug) {
Assert::stringNotEmpty($parent_slug, '$parent_slug has to be null or non empty string.');
}
$this->parent_slug = $parent_slug;
}
/**
* @interal
*
* @param array{
* menu_title?: string,
* page_title?: string,
* icon?: string,
* capability?: string,
* position?: int
* } $attributes
*/
public static function fromRoute(
Route $route,
array $attributes = [],
?string $parent_slug = null
): AdminMenuItem {
$menu_slug = $route->getPattern();
$page_title = null;
$menu_title = null;
if (isset($attributes[self::MENU_TITLE])) {
$menu_title = $attributes[self::MENU_TITLE];
$page_title = $menu_title;
}
if (isset($attributes[self::PAGE_TITLE])) {
$page_title = $attributes[self::PAGE_TITLE];
$menu_title ??= $page_title;
}
if (null === $page_title) {
$page_title = self::stringToHeadline(Str::afterLast($route->getName(), '.'));
}
if (null === $menu_title) {
$menu_title = $page_title;
}
return new self(
$page_title,
$menu_title,
$menu_slug,
$attributes[self::CAPABILITY] ?? null,
$attributes[self::ICON] ?? null,
$attributes[self::POSITION] ?? null,
$parent_slug
);
}
public function pageTitle(): string
{
return $this->page_title;
}
public function menuTitle(): string
{
return $this->menu_title;
}
public function slug(): UrlPath
{
return UrlPath::fromString($this->menu_slug);
}
public function position(): ?int
{
return $this->position;
}
public function requiredCapability(): ?string
{
return $this->capability;
}
public function icon(): ?string
{
return $this->icon;
}
public function parentSlug(): UrlPath
{
if (! $this->parent_slug) {
throw new LogicException(sprintf('Menu item [%s] does not have a parent item.', $this->menu_slug));
}
return UrlPath::fromString($this->parent_slug);
}
public function isChild(): bool
{
return null !== $this->parent_slug;
}
private static function stringToHeadline(string $route_name): string
{
$parts = explode(' ', str_replace(['.', '_', '-'], ' ', $route_name));
if (count($parts) > 1) {
$parts = array_map(fn (string $part): string => mb_convert_case($part, MB_CASE_TITLE, 'UTF-8'), $parts);
}
return implode(' ', $parts);
}
}