-
Notifications
You must be signed in to change notification settings - Fork 0
/
MenuItem.php
89 lines (73 loc) · 2.89 KB
/
MenuItem.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
<?php
namespace CyberPunkCodes\MenuHelper\View\Components;
use Illuminate\Support\Str;
use Illuminate\View\Component;
use CyberPunkCodes\MenuHelper\Exceptions\MenuHelperException;
use CyberPunkCodes\MenuHelper\Traits\MenuHelperItem;
class MenuItem extends Component
{
use MenuHelperItem;
/**
* Create a new component instance.
*
* @return void
*/
public function __construct($menu, $item, $parentId, $loop = null)
{
$this->menu = $menu;
$this->item = $item;
$this->parentId = $parentId;
$this->loop = $loop;
// if it's a collection, convert to array
if ( $this->item instanceof \Illuminate\Support\Collection ) {
$this->item = $this->item->toArray();
}
$this->validate();
if (isset($item['show']) && ($item['show'] === false)) {
$this->showItem = false;
}
// expanded and collapsed should always be the opposite of eachother
if ( isset($item['expanded'], $item['collapsed']) ) {
$this->expanded = filter_var($item['expanded'], FILTER_VALIDATE_BOOL);
$this->collapsed = filter_var($item['collapsed'], FILTER_VALIDATE_BOOL);
} elseif ( isset($item['expanded']) ) {
$this->expanded = filter_var($item['expanded'], FILTER_VALIDATE_BOOL);
$this->collapsed = ! $this->expanded;
} elseif ( isset($item['collapsed']) ) {
$this->collapsed = filter_var($item['collapsed'], FILTER_VALIDATE_BOOL);
$this->expanded = ! $this->collapsed;
}
// fail safes
if ( ($this->expanded === true) && ($this->collapsed === true) ) {
throw new MenuHelperException("'collapsed' and 'expanded' both can't be true.");
}
if ( ($this->expanded === false) && ($this->collapsed === false) ) {
throw new MenuHelperException("'collapsed' and 'expanded' both can't be false.");
}
// randomly generate a target if none specified
if ( ! isset($this->item['target']) || empty($this->item['target']) ) {
$this->item['target'] = 'mh_' . Str::random(12);
}
}
private function validate()
{
if ( ! isset($this->menu, $this->item) ) {
throw new MenuHelperException("'menu' and 'item' attributes are required for menu item component");
}
if ( ! is_string($this->menu) || empty($this->menu) ) {
throw new MenuHelperException("'menu' must be a string and not empty.");
}
if ( ! is_array($this->item) ) {
throw new MenuHelperException("'item' must be a collection or an array.");
}
}
/**
* Get the view / contents that represent the component.
*
* @return \Illuminate\Contracts\View\View|\Closure|string
*/
public function render()
{
return view('menuhelper::' . $this->menu . '.menu-item');
}
}