|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Hyde\Support\Models; |
|
6
|
|
|
|
|
7
|
|
|
use Hyde\Hyde; |
|
8
|
|
|
use Hyde\Pages\Concerns\HydePage; |
|
9
|
|
|
use Hyde\Support\Concerns\Serializable; |
|
10
|
|
|
use Hyde\Support\Contracts\SerializableContract; |
|
11
|
|
|
use Stringable; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* The Route class bridges the gaps between Hyde pages and their respective compiled static webpages |
|
15
|
|
|
* by providing helper methods and information allowing you to easily access and interact with the |
|
16
|
|
|
* various paths associated with a page, both source and compiled file paths as well as the URL. |
|
17
|
|
|
* |
|
18
|
|
|
* If you visualize a web of this class's properties, you should be able to see how this |
|
19
|
|
|
* class links them all together, and what powerful information you can gain from it. |
|
20
|
|
|
* |
|
21
|
|
|
* @see \Hyde\Framework\Testing\Unit\RouteTest |
|
22
|
|
|
*/ |
|
23
|
|
|
class Route implements Stringable, SerializableContract |
|
24
|
|
|
{ |
|
25
|
|
|
use Serializable; |
|
26
|
|
|
|
|
27
|
|
|
protected HydePage $page; |
|
28
|
|
|
|
|
29
|
|
|
public function __construct(HydePage $page) |
|
30
|
|
|
{ |
|
31
|
|
|
$this->page = $page; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Cast a route object into a string that can be used in a href attribute. |
|
36
|
|
|
*/ |
|
37
|
|
|
public function __toString(): string |
|
38
|
|
|
{ |
|
39
|
|
|
return $this->getLink(); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Generate a link to the route destination, relative to the current route, and supports pretty URLs. |
|
44
|
|
|
*/ |
|
45
|
|
|
public function getLink(): string |
|
46
|
|
|
{ |
|
47
|
|
|
return Hyde::relativeLink($this->page->getLink()); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
public function getPage(): HydePage |
|
51
|
|
|
{ |
|
52
|
|
|
return $this->page; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** @return class-string<HydePage> */ |
|
|
|
|
|
|
56
|
|
|
public function getPageClass(): string |
|
57
|
|
|
{ |
|
58
|
|
|
return $this->page::class; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
public function getPageIdentifier(): string |
|
62
|
|
|
{ |
|
63
|
|
|
return $this->page->getIdentifier(); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
public function getRouteKey(): string |
|
67
|
|
|
{ |
|
68
|
|
|
return $this->page->getRouteKey(); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
public function getSourcePath(): string |
|
72
|
|
|
{ |
|
73
|
|
|
return $this->page->getSourcePath(); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
public function getOutputPath(): string |
|
77
|
|
|
{ |
|
78
|
|
|
return $this->page->getOutputPath(); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Determine if the route instance matches another route or route key. |
|
83
|
|
|
*/ |
|
84
|
|
|
public function is(Route|RouteKey|string $route): bool |
|
85
|
|
|
{ |
|
86
|
|
|
if ($route instanceof Route) { |
|
87
|
|
|
return $this->getRouteKey() === $route->getRouteKey(); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
return $this->getRouteKey() === (string) $route; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @return array{routeKey: string, sourcePath: string, outputPath: string, page: array{class: string, identifier: string}} |
|
95
|
|
|
*/ |
|
96
|
|
|
public function toArray(): array |
|
97
|
|
|
{ |
|
98
|
|
|
return [ |
|
|
|
|
|
|
99
|
|
|
'routeKey' => $this->getRouteKey(), |
|
100
|
|
|
'sourcePath' => $this->getSourcePath(), |
|
101
|
|
|
'outputPath' => $this->getOutputPath(), |
|
102
|
|
|
'page' => [ |
|
103
|
|
|
'class' => $this->getPageClass(), |
|
104
|
|
|
'identifier' => $this->getPageIdentifier(), |
|
105
|
|
|
], |
|
106
|
|
|
]; |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|