|
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
|
|
|
|