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

RouteKey   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 6
c 1
b 0
f 0
dl 0
loc 28
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __toString() 0 3 1
A get() 0 3 1
A make() 0 3 1
A __construct() 0 3 1
A fromPage() 0 3 1
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