|
13 | 13 | use function is_string; |
14 | 14 |
|
15 | 15 | /** |
16 | | - * Abstraction for a navigation menu item. Used by the MainNavigationMenu and DocumentationSidebar classes. |
| 16 | + * Abstraction for a navigation menu item containing useful information like the destination, label, and priority. |
| 17 | + * |
| 18 | + * It is used by the MainNavigationMenu and DocumentationSidebar classes. |
17 | 19 | */ |
18 | 20 | class NavigationItem implements Stringable |
19 | 21 | { |
20 | 22 | protected string|Route $destination; |
21 | 23 | protected string $label; |
22 | 24 | protected int $priority; |
23 | 25 |
|
24 | | - public function __construct(Route|string $destination, string $label, int $priority = NavigationMenu::DEFAULT) |
| 26 | + /** |
| 27 | + * Create a new navigation menu item, automatically filling in the properties from a Route instance if provided. |
| 28 | + * |
| 29 | + * @param \Hyde\Support\Models\Route|string<\Hyde\Support\Models\RouteKey>|string $destination Route instance or route key, or an external URI. |
| 30 | + * @param string|null $label If not provided, Hyde will try to get it from the route's connected page, or from the URL. |
| 31 | + * @param int|null $priority If not provided, Hyde will try to get it from the route or the default priority of 500. |
| 32 | + */ |
| 33 | + public function __construct(Route|string $destination, ?string $label = null, ?int $priority = null) |
25 | 34 | { |
26 | 35 | [$this->destination, $this->label, $this->priority] = self::make($destination, $label, $priority); |
27 | 36 | } |
28 | 37 |
|
29 | 38 | /** |
30 | 39 | * Create a new navigation menu item, automatically filling in the properties from a Route instance if provided. |
31 | 40 | * |
32 | | - * @param \Hyde\Support\Models\Route|string<\Hyde\Support\Models\RouteKey>|string $destination Route instance or route key, or external URI. |
33 | | - * @param string|null $label Leave blank to use the label of the route's corresponding page, if there is one tied to the route. |
34 | | - * @param int|null $priority Leave blank to use the priority of the route's corresponding page, if there is one tied to the route. |
| 41 | + * @param \Hyde\Support\Models\Route|string<\Hyde\Support\Models\RouteKey>|string $destination Route instance or route key, or an external URI. |
| 42 | + * @param string|null $label If not provided, Hyde will try to get it from the route's connected page, or from the URL. |
| 43 | + * @param int|null $priority If not provided, Hyde will try to get it from the route or the default priority of 500. |
35 | 44 | */ |
36 | 45 | public static function create(Route|string $destination, ?string $label = null, ?int $priority = null): static |
37 | 46 | { |
38 | 47 | return new static(...self::make($destination, $label, $priority)); |
39 | 48 | } |
40 | 49 |
|
41 | 50 | /** |
42 | | - * Resolve a link to the navigation item. |
| 51 | + * Resolve a link to the navigation item. See `getLink()` for more information. |
43 | 52 | */ |
44 | 53 | public function __toString(): string |
45 | 54 | { |
46 | 55 | return $this->getLink(); |
47 | 56 | } |
48 | 57 |
|
49 | 58 | /** |
50 | | - * Resolve the destination link of the navigation item. |
| 59 | + * Resolve the destination link of the navigation item. This can then be used in the `href` attribute of an anchor tag. |
| 60 | + * |
| 61 | + * If the destination is a Route, it will be resolved using the Route's link. |
| 62 | + * Otherwise, it will be returned as is for external links using URLs. |
51 | 63 | */ |
52 | 64 | public function getLink(): string |
53 | 65 | { |
@@ -88,11 +100,13 @@ public function isActive(): bool |
88 | 100 |
|
89 | 101 | protected static function make(Route|string $destination, ?string $label = null, ?int $priority = null): array |
90 | 102 | { |
| 103 | + // Automatically resolve the destination if it's a route key. |
91 | 104 | if (is_string($destination) && Routes::has($destination)) { |
92 | 105 | $destination = Routes::get($destination); |
93 | 106 | } |
94 | 107 |
|
95 | 108 | if ($destination instanceof Route) { |
| 109 | + // Try to fill in missing properties from the route's connected page. |
96 | 110 | $label ??= $destination->getPage()->navigationMenuLabel(); |
97 | 111 | $priority ??= $destination->getPage()->navigationMenuPriority(); |
98 | 112 | } |
|
0 commit comments