Passed
Push — master ( c38f14...5b0ffd )
by Caen
03:39 queued 15s
created

Route::getOrFail()   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 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
 * @see \Hyde\Framework\Testing\Unit\RouteTest
22
 */
23
class Route implements Stringable, SerializableContract
24
{
25
    use Serializable;
26
27
    protected HydePage $page;
28
29
    public function __construct(HydePage $page)
30
    {
31
        $this->page = $page;
32
    }
33
34
    /**
35
     * Cast a route object into a string that can be used in a href attribute.
36
     */
37
    public function __toString(): string
38
    {
39
        return $this->getLink();
40
    }
41
42
    /**
43
     * Generate a link to the route destination, relative to the current route, and supports pretty URLs.
44
     */
45
    public function getLink(): string
46
    {
47
        return Hyde::relativeLink($this->page->getLink());
48
    }
49
50
    public function getPage(): HydePage
51
    {
52
        return $this->page;
53
    }
54
55
    /** @return class-string<HydePage> */
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<HydePage> at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<HydePage>.
Loading history...
56
    public function getPageClass(): string
57
    {
58
        return $this->page::class;
59
    }
60
61
    public function getPageIdentifier(): string
62
    {
63
        return $this->page->getIdentifier();
64
    }
65
66
    public function getRouteKey(): string
67
    {
68
        return $this->page->getRouteKey();
69
    }
70
71
    public function getSourcePath(): string
72
    {
73
        return $this->page->getSourcePath();
74
    }
75
76
    public function getOutputPath(): string
77
    {
78
        return $this->page->getOutputPath();
79
    }
80
81
    /**
82
     * Determine if the route instance matches another route or route key.
83
     */
84
    public function is(Route|RouteKey|string $route): bool
85
    {
86
        if ($route instanceof Route) {
87
            return $this->getRouteKey() === $route->getRouteKey();
88
        }
89
90
        return $this->getRouteKey() === (string) $route;
91
    }
92
93
    /**
94
     * @return array{routeKey: string, sourcePath: string, outputPath: string, page: array{class: string, identifier: string}}
95
     */
96
    public function toArray(): array
97
    {
98
        return [
0 ignored issues
show
Bug Best Practice introduced by
The expression return array('routeKey' ...->getPageIdentifier())) returns the type array<string,array<string,string>|string> which is incompatible with the return type mandated by Hyde\Support\Contracts\S...ableContract::toArray() of Hyde\Support\Contracts\TValue[].

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
99
            'routeKey' => $this->getRouteKey(),
100
            'sourcePath' => $this->getSourcePath(),
101
            'outputPath' => $this->getOutputPath(),
102
            'page' => [
103
                'class' => $this->getPageClass(),
104
                'identifier' => $this->getPageIdentifier(),
105
            ],
106
        ];
107
    }
108
}
109