|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Aminetiyal\LaravelTemplate\Components\Lte; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\View\Component; |
|
6
|
|
|
|
|
7
|
|
|
class MenuItem extends Component |
|
8
|
|
|
{ |
|
9
|
|
|
public $menu = []; |
|
10
|
|
|
public $hasTreeView = false; |
|
11
|
|
|
public $submenus = []; |
|
12
|
|
|
public $link = ''; |
|
13
|
|
|
public $active = false; |
|
14
|
|
|
|
|
15
|
|
|
public function __construct($menu = []) |
|
16
|
|
|
{ |
|
17
|
|
|
$this->setMenu($menu); |
|
18
|
|
|
|
|
19
|
|
|
$this->renderLink(); |
|
20
|
|
|
|
|
21
|
|
|
$this->renderTreeView(); |
|
22
|
|
|
|
|
23
|
|
|
$this->isActive(); |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
public function setMenu(array $menu): void |
|
27
|
|
|
{ |
|
28
|
|
|
if (!array_key_exists('link', $menu)) { |
|
29
|
|
|
$menu['link'] = [ |
|
30
|
|
|
'type' => 'url', |
|
31
|
|
|
'value' => '' |
|
32
|
|
|
]; |
|
33
|
|
|
} else { |
|
34
|
|
|
if (!array_key_exists('type', $menu['link'])) { |
|
35
|
|
|
$menu['link']['type'] = 'url'; |
|
36
|
|
|
} |
|
37
|
|
|
if (!array_key_exists('value', $menu['link'])) { |
|
38
|
|
|
$menu['link']['value'] = ''; |
|
39
|
|
|
} |
|
40
|
|
|
} |
|
41
|
|
|
$this->menu = $menu; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public function renderLink(array $menu = null) |
|
45
|
|
|
{ |
|
46
|
|
|
$external = !is_null($menu); |
|
47
|
|
|
|
|
48
|
|
|
$menu = $menu ?? $this->menu; |
|
49
|
|
|
|
|
50
|
|
|
if ($menu['link']['type'] == 'route') { |
|
51
|
|
|
if (is_array($menu['link']['value'])) { |
|
52
|
|
|
$link = route(current($menu['link']['value']), end($menu['link']['value'])); |
|
53
|
|
|
} else { |
|
54
|
|
|
$link = route($menu['link']['value']); |
|
55
|
|
|
} |
|
56
|
|
|
} elseif ($menu['link']['type'] == 'url') { |
|
57
|
|
|
$link = url($menu['link']['value']); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
if ($external) { |
|
61
|
|
|
return $link; |
|
|
|
|
|
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
$this->link = $link; |
|
|
|
|
|
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
public function renderTreeView() |
|
68
|
|
|
{ |
|
69
|
|
|
$this->hasTreeView = (array_key_exists('submenus', $this->menu) && count($this->menu['submenus']) > 0); |
|
70
|
|
|
|
|
71
|
|
|
if ($this->hasTreeView) { |
|
72
|
|
|
$this->submenus = $this->menu['submenus']; |
|
73
|
|
|
$this->link = ''; |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
public function isActive() |
|
78
|
|
|
{ |
|
79
|
|
|
$this->active = request()->fullUrl() == $this->link; |
|
80
|
|
|
|
|
81
|
|
|
$this->hasActiveSubmenu($this->menu); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
public function hasActiveSubmenu($menu) |
|
85
|
|
|
{ |
|
86
|
|
|
if (array_key_exists('submenus', $menu) && count($menu['submenus']) > 0) { |
|
87
|
|
|
foreach ($menu['submenus'] as $submenu) { |
|
88
|
|
|
if (request()->fullUrl() == $this->getLink($submenu)) { |
|
89
|
|
|
$this->active = true; |
|
90
|
|
|
break; |
|
91
|
|
|
} |
|
92
|
|
|
$this->hasActiveSubmenu($submenu); |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
public function getLink($menu) |
|
98
|
|
|
{ |
|
99
|
|
|
if ( |
|
100
|
|
|
array_key_exists('link', $menu) && |
|
101
|
|
|
array_key_exists('value', $menu['link']) && |
|
102
|
|
|
array_key_exists('type', $menu['link']) && |
|
103
|
|
|
in_array($menu['link']['type'], ['url', 'route']) |
|
104
|
|
|
) { |
|
105
|
|
|
$link = $this->renderLink($menu); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
return $link ?? null; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
public function render() |
|
112
|
|
|
{ |
|
113
|
|
|
return view('template::lte.components._layouts.menu-item'); |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: