|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Spatie\Menu\Traits; |
|
4
|
|
|
|
|
5
|
|
|
use Spatie\Menu\Helpers\Str; |
|
6
|
|
|
use Spatie\Url\Url; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Expects an `$active` property on the class. |
|
10
|
|
|
* |
|
11
|
|
|
* @property string $url |
|
12
|
|
|
*/ |
|
13
|
|
|
trait Activatable |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @return bool |
|
17
|
|
|
*/ |
|
18
|
|
|
public function isActive(): bool |
|
19
|
|
|
{ |
|
20
|
|
|
return $this->active; |
|
|
|
|
|
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @param bool|callable $active |
|
25
|
|
|
* |
|
26
|
|
|
* @return $this |
|
27
|
|
|
*/ |
|
28
|
|
|
public function setActive($active = true) |
|
29
|
|
|
{ |
|
30
|
|
|
if (is_callable($active)) { |
|
31
|
|
|
$this->active = $active($this); |
|
32
|
|
|
|
|
33
|
|
|
return $this; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
$this->active = (bool) $active; |
|
37
|
|
|
|
|
38
|
|
|
return $this; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @return $this |
|
43
|
|
|
*/ |
|
44
|
|
|
public function setInactive() |
|
45
|
|
|
{ |
|
46
|
|
|
$this->active = false; |
|
47
|
|
|
|
|
48
|
|
|
return $this; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @return string|null |
|
53
|
|
|
*/ |
|
54
|
|
|
public function url() |
|
55
|
|
|
{ |
|
56
|
|
|
return $this->url; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @return bool |
|
61
|
|
|
*/ |
|
62
|
|
|
public function hasUrl(): bool |
|
63
|
|
|
{ |
|
64
|
|
|
return ! is_null($this->url); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @param string|null $url |
|
69
|
|
|
* |
|
70
|
|
|
* @return $this |
|
71
|
|
|
*/ |
|
72
|
|
|
public function setUrl($url) |
|
73
|
|
|
{ |
|
74
|
|
|
$this->url = $url; |
|
75
|
|
|
|
|
76
|
|
|
return $this; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @param string $url |
|
81
|
|
|
* @param string $root |
|
82
|
|
|
* |
|
83
|
|
|
* @return $this |
|
84
|
|
|
*/ |
|
85
|
|
|
public function determineActiveForUrl(string $url, string $root = '/') |
|
86
|
|
|
{ |
|
87
|
|
|
if (! $this->hasUrl()) { |
|
88
|
|
|
return; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
$itemUrl = Url::fromString($this->url); |
|
92
|
|
|
$matchUrl = Url::fromString($url); |
|
93
|
|
|
|
|
94
|
|
|
// If the hosts don't match, this url isn't active. |
|
95
|
|
|
if ($itemUrl->getHost() !== $matchUrl->getHost()) { |
|
96
|
|
|
return $this->setInactive(); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
// If this url doesn't start with the root, it's inactive. |
|
100
|
|
|
if (! Str::startsWith($itemUrl->getPath(), $root)) { |
|
101
|
|
|
return $this->setInactive(); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
// For the next comparisons we just need the paths, and we'll remove |
|
105
|
|
|
// the root first. |
|
106
|
|
|
$itemPath = Str::removeFromStart($root, $itemUrl->getPath()); |
|
107
|
|
|
$matchPath = Str::removeFromStart($root, $matchUrl->getPath()); |
|
108
|
|
|
|
|
109
|
|
|
// If this url starts with the url we're matching with, it's active. |
|
110
|
|
|
if (Str::startsWith($matchPath, $itemPath)) { |
|
111
|
|
|
return $this->setActive(); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
return $this->setInactive(); |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: