Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
5 / 5 |
|
100.00% |
5 / 5 |
CRAP | |
100.00% |
1 / 1 |
RouteKey | |
100.00% |
5 / 5 |
|
100.00% |
5 / 5 |
5 | |
100.00% |
1 / 1 |
make | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
__construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
__toString | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
get | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
fromPage | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | |
3 | declare(strict_types=1); |
4 | |
5 | namespace Hyde\Support\Models; |
6 | |
7 | use Stringable; |
8 | |
9 | use function Hyde\unslash; |
10 | |
11 | /** |
12 | * Route keys provide the core bindings of the HydePHP routing system as they are what canonically identifies a page. |
13 | * This class both provides a data object for normalized type-hintable values, and general related helper methods. |
14 | * |
15 | * In short, the route key is the URL path relative to the site webroot, without the file extension. |
16 | * |
17 | * For example, `_pages/index.blade.php` would be compiled to `_site/index.html` and thus has the route key of `index`. |
18 | * As another example, `_posts/welcome.md` would be compiled to `_site/posts/welcome.html` and thus has the route key of `posts/welcome`. |
19 | * |
20 | * Note that if the source page's output directory is changed, the route key will change accordingly. |
21 | * This can potentially cause links to break when changing the output directory for a page class. |
22 | */ |
23 | final class RouteKey implements Stringable |
24 | { |
25 | protected readonly string $key; |
26 | |
27 | public static function make(string $key): self |
28 | { |
29 | return new self($key); |
30 | } |
31 | |
32 | public function __construct(string $key) |
33 | { |
34 | $this->key = $key; |
35 | } |
36 | |
37 | public function __toString(): string |
38 | { |
39 | return $this->key; |
40 | } |
41 | |
42 | public function get(): string |
43 | { |
44 | return $this->key; |
45 | } |
46 | |
47 | /** @param class-string<\Hyde\Pages\Concerns\HydePage> $pageClass */ |
48 | public static function fromPage(string $pageClass, string $identifier): self |
49 | { |
50 | return new self(unslash("{$pageClass::baseRouteKey()}/$identifier")); |
51 | } |
52 | } |