Passed
Push — master ( 349214...99913c )
by Caen
03:13 queued 12s
created

RouteKey::get()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Hyde\Support\Models;
6
7
use Stringable;
8
use function str_replace;
9
use function 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 = self::normalize($key);
0 ignored issues
show
Bug introduced by
The property key is declared read-only in Hyde\Support\Models\RouteKey.
Loading history...
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
    public static function normalize(string $string): string
48
    {
49
        return str_replace('.', '/', $string);
50
    }
51
52
    /** @param class-string<\Hyde\Pages\Concerns\HydePage> $pageClass */
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<\Hyde\Pages\Concerns\HydePage> at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<\Hyde\Pages\Concerns\HydePage>.
Loading history...
53
    public static function fromPage(string $pageClass, string $identifier): self
54
    {
55
        return new self(unslash("{$pageClass::baseRouteKey()}/$identifier"));
56
    }
57
}
58