1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\Menu\Traits; |
4
|
|
|
|
5
|
|
|
use Spatie\Menu\ActiveUrlChecker; |
6
|
|
|
use Spatie\Menu\ExactUrlChecker; |
7
|
|
|
use Spatie\Url\Url; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Expects an `$active` property on the class. |
11
|
|
|
* |
12
|
|
|
* @property string $url |
13
|
|
|
*/ |
14
|
|
|
trait Activatable |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var bool |
18
|
|
|
*/ |
19
|
|
|
protected $exactActive = false; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @return bool |
23
|
|
|
*/ |
24
|
|
|
public function isActive(): bool |
25
|
|
|
{ |
26
|
|
|
return $this->active; |
|
|
|
|
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param bool|callable $active |
31
|
|
|
* |
32
|
|
|
* @return $this |
33
|
|
|
*/ |
34
|
|
|
public function setActive($active = true) |
35
|
|
|
{ |
36
|
|
|
if (is_callable($active)) { |
37
|
|
|
$this->active = $active($this); |
|
|
|
|
38
|
|
|
|
39
|
|
|
return $this; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
$this->active = (bool) $active; |
|
|
|
|
43
|
|
|
|
44
|
|
|
return $this; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @return $this |
49
|
|
|
*/ |
50
|
|
|
public function setInactive() |
51
|
|
|
{ |
52
|
|
|
$this->active = false; |
|
|
|
|
53
|
|
|
|
54
|
|
|
return $this; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @return string|null |
59
|
|
|
*/ |
60
|
|
|
public function url() |
61
|
|
|
{ |
62
|
|
|
return $this->url; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @return bool |
67
|
|
|
*/ |
68
|
|
|
public function hasUrl(): bool |
69
|
|
|
{ |
70
|
|
|
return ! is_null($this->url); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param string|null $url |
75
|
|
|
* |
76
|
|
|
* @return $this |
77
|
|
|
*/ |
78
|
|
|
public function setUrl($url) |
79
|
|
|
{ |
80
|
|
|
$this->url = $url; |
81
|
|
|
|
82
|
|
|
return $this; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param string $url |
87
|
|
|
* @param string $root |
88
|
|
|
* |
89
|
|
|
* @return $this |
90
|
|
|
*/ |
91
|
|
|
public function determineActiveForUrl(string $url, string $root = '/') |
92
|
|
|
{ |
93
|
|
|
if (! $this->hasUrl()) { |
94
|
|
|
return; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
ActiveUrlChecker::check($this->url, $url, $root) |
98
|
|
|
? $this->setActive() |
99
|
|
|
: $this->setInactive(); |
100
|
|
|
|
101
|
|
|
ExactUrlChecker::check($this->url, $url, $root) |
102
|
|
|
? $this->setExactActive() |
103
|
|
|
: $this->setExactActive(false); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Set if current Activatable should be marked as an exact url match. |
108
|
|
|
* |
109
|
|
|
* @param bool $exactActive |
110
|
|
|
* |
111
|
|
|
* @return $this |
112
|
|
|
*/ |
113
|
|
|
public function setExactActive(bool $exactActive = true) |
114
|
|
|
{ |
115
|
|
|
$this->exactActive = $exactActive; |
116
|
|
|
|
117
|
|
|
return $this; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Check if current Activatable is marked as an exact url match. |
122
|
|
|
* |
123
|
|
|
* @return bool |
124
|
|
|
*/ |
125
|
|
|
public function isExactActive(): bool |
126
|
|
|
{ |
127
|
|
|
return $this->exactActive; |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.
If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.