GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 4a396e...105659 )
by Sebastian
02:06
created

Link::render()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 8
rs 9.4285
cc 1
eloc 5
nc 1
nop 0
1
<?php
2
3
namespace Spatie\Menu;
4
5
use Spatie\HtmlElement\HtmlElement;
6
use Spatie\Menu\Traits\Activatable as ActivatableTrait;
7
use Spatie\Menu\Traits\HtmlAttributes;
8
use Spatie\Menu\Traits\ParentAttributes;
9
10
class Link implements Item, Activatable, HasUrl
11
{
12
    use ActivatableTrait, HtmlAttributes, ParentAttributes;
13
14
    /** @var string */
15
    protected $text;
16
17
    /** @var string */
18
    protected $url;
19
20
    /**
21
     * @param string $url
22
     * @param string $text
23
     */
24
    protected function __construct(string $url, string $text)
25
    {
26
        $this->url = $url;
27
        $this->text = $text;
28
        $this->active = false;
29
30
        $this->initializeHtmlAttributes();
31
        $this->initializeParentAttributes();
32
    }
33
34
    /**
35
     * @param string $url
36
     * @param string $text
37
     *
38
     * @return static
39
     */
40
    public static function to(string $url, string $text)
41
    {
42
        return new static($url, $text);
43
    }
44
45
    /**
46
     * @return string
47
     */
48
    public function getText() : string
49
    {
50
        return $this->text;
51
    }
52
53
    /**
54
     * @return string
55
     */
56
    public function getUrl() : string
57
    {
58
        return $this->url;
59
    }
60
61
    /**
62
     * Return a segment of the link's URL. This function works for both absolute
63
     * and relative URL's. The index is a 1-index based number. Trailing and
64
     * double slashes are ignored.
65
     *
66
     * Example: (new Link('Open Source', 'https://spatie.be/opensource'))->segment(1)
67
     *      => 'opensource'
68
     *
69
     * @param int $index
70
     *
71
     * @return string|null
72
     */
73
    public function segment(int $index)
74
    {
75
        $path = parse_url($this->url)['path'] ?? '';
76
77
        $segments = array_values(
78
            array_filter(
79
                explode('/', $path),
80
                function ($value) {
81
                    return $value !== '';
82
                }
83
            )
84
        );
85
86
        return $segments[$index - 1] ?? null;
87
    }
88
89
    /**
90
     * @param string $prefix
91
     *
92
     * @return $this
93
     */
94
    public function prefix(string $prefix)
95
    {
96
        $this->url = $prefix.'/'.ltrim($this->url, '/');
97
98
        return $this;
99
    }
100
101
    /**
102
     * @return string
103
     */
104
    public function render() : string
105
    {
106
        return HtmlElement::render(
107
            "a[href={$this->url}]",
108
            $this->htmlAttributes->toArray(),
109
            $this->text
110
        );
111
    }
112
113
    /**
114
     * @return string
115
     */
116
    public function __toString() : string
117
    {
118
        return $this->render();
119
    }
120
}
121