Passed
Push — master ( 890a19...10d014 )
by Caen
03:15 queued 12s
created

RouteKey::normalize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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