Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
21 / 21 |
|
100.00% |
11 / 11 |
CRAP | |
100.00% |
1 / 1 |
| Route | |
100.00% |
21 / 21 |
|
100.00% |
11 / 11 |
12 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| __toString | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getLink | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getPage | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getPageClass | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getPageIdentifier | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getRouteKey | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getSourcePath | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getOutputPath | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| is | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| toArray | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
1 | |||
| 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 | class Route implements Stringable, SerializableContract |
| 22 | { |
| 23 | use Serializable; |
| 24 | |
| 25 | protected HydePage $page; |
| 26 | |
| 27 | public function __construct(HydePage $page) |
| 28 | { |
| 29 | $this->page = $page; |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * Cast a route object into a string that can be used in a href attribute. |
| 34 | */ |
| 35 | public function __toString(): string |
| 36 | { |
| 37 | return $this->getLink(); |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Generate a link to the route destination, relative to the current route, and supports pretty URLs. |
| 42 | */ |
| 43 | public function getLink(): string |
| 44 | { |
| 45 | return Hyde::relativeLink($this->page->getLink()); |
| 46 | } |
| 47 | |
| 48 | public function getPage(): HydePage |
| 49 | { |
| 50 | return $this->page; |
| 51 | } |
| 52 | |
| 53 | /** @return class-string<HydePage> */ |
| 54 | public function getPageClass(): string |
| 55 | { |
| 56 | return $this->page::class; |
| 57 | } |
| 58 | |
| 59 | public function getPageIdentifier(): string |
| 60 | { |
| 61 | return $this->page->getIdentifier(); |
| 62 | } |
| 63 | |
| 64 | public function getRouteKey(): string |
| 65 | { |
| 66 | return $this->page->getRouteKey(); |
| 67 | } |
| 68 | |
| 69 | public function getSourcePath(): string |
| 70 | { |
| 71 | return $this->page->getSourcePath(); |
| 72 | } |
| 73 | |
| 74 | public function getOutputPath(): string |
| 75 | { |
| 76 | return $this->page->getOutputPath(); |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Determine if the route instance matches another route or route key. |
| 81 | */ |
| 82 | public function is(Route|RouteKey|string $route): bool |
| 83 | { |
| 84 | if ($route instanceof Route) { |
| 85 | return $this->getRouteKey() === $route->getRouteKey(); |
| 86 | } |
| 87 | |
| 88 | return $this->getRouteKey() === (string) $route; |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * @return array{routeKey: string, sourcePath: string, outputPath: string, page: array{class: string, identifier: string}} |
| 93 | */ |
| 94 | public function toArray(): array |
| 95 | { |
| 96 | return [ |
| 97 | 'routeKey' => $this->getRouteKey(), |
| 98 | 'sourcePath' => $this->getSourcePath(), |
| 99 | 'outputPath' => $this->getOutputPath(), |
| 100 | 'page' => [ |
| 101 | 'class' => $this->getPageClass(), |
| 102 | 'identifier' => $this->getPageIdentifier(), |
| 103 | ], |
| 104 | ]; |
| 105 | } |
| 106 | } |