Passed
Push — master ( a9a196...3e2eac )
by Diego
03:10
created

MenuItemHelper::isSidebarSearch()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 3
c 0
b 0
f 0
nc 3
nop 1
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 3
rs 10
1
<?php
2
3
namespace JeroenNoten\LaravelAdminLte\Helpers;
4
5
/**
6
 * TODO: On the future, all menu items should have a type property. We can use
7
 * the type property to easy distinguish the item type and avoid guessing it by
8
 * they properties.
9
 */
10
class MenuItemHelper
11
{
12
    /**
13
     * Check if a menu item is a header.
14
     *
15
     * @param mixed $item
16
     * @return bool
17
     */
18 52
    public static function isHeader($item)
19
    {
20 52
        return is_string($item) || isset($item['header']);
21
    }
22
23
    /**
24
     * Check if a menu item is a link.
25
     *
26
     * @param mixed $item
27
     * @return bool
28
     */
29 4
    public static function isLink($item)
30
    {
31 4
        return isset($item['text']) &&
32 4
               (isset($item['url']) || isset($item['route']));
33
    }
34
35
    /**
36
     * Check if a menu item is a submenu.
37
     *
38
     * @param mixed $item
39
     * @return bool
40
     */
41 56
    public static function isSubmenu($item)
42
    {
43 56
        return isset($item['text']) &&
44 56
               isset($item['submenu']) &&
45 56
               is_array($item['submenu']);
46
    }
47
48
    /**
49
     * Check if a menu item is a legacy search bar.
50
     *
51
     * @param mixed $item
52
     * @return bool
53
     */
54 54
    public static function isLegacySearch($item)
55
    {
56 54
        return isset($item['text']) &&
57 54
               isset($item['search']) &&
58 54
               $item['search'];
59
    }
60
61
    /**
62
     * Check if a menu item is a navbar custom search bar.
63
     *
64
     * @param mixed $item
65
     * @return bool
66
     */
67 52
    public static function isNavbarCustomSearch($item)
68
    {
69 52
        return isset($item['text']) &&
70 52
               isset($item['type']) &&
71 52
               $item['type'] === 'navbar-search';
72
    }
73
74
    /**
75
     * Check if a menu item is a sidebar custom search bar.
76
     *
77
     * @param mixed $item
78
     * @return bool
79
     */
80 50
    public static function isSidebarCustomSearch($item)
81
    {
82 50
        return isset($item['text']) &&
83 50
               isset($item['type']) &&
84 50
               $item['type'] === 'sidebar-custom-search';
85
    }
86
87
    /**
88
     * Check if a menu item is a sidebar menu search bar.
89
     *
90
     * @param mixed $item
91
     * @return bool
92
     */
93 50
    public static function isSidebarMenuSearch($item)
94
    {
95 50
        return isset($item['text']) &&
96 50
               isset($item['type']) &&
97 50
               $item['type'] === 'sidebar-menu-search';
98
    }
99
100
    /**
101
     * Check if a menu item is a navbar search item (legacy or new).
102
     *
103
     * @param mixed $item
104
     * @return bool
105
     */
106 54
    public static function isNavbarSearch($item)
107
    {
108 54
        return self::isLegacySearch($item) ||
109 54
               self::isNavbarCustomSearch($item);
110
    }
111
112
    /**
113
     * Check if a menu item is a sidebar search item (legacy or new).
114
     *
115
     * @param mixed $item
116
     * @return bool
117
     */
118 50
    public static function isSidebarSearch($item)
119
    {
120 50
        return self::isLegacySearch($item) ||
121 50
               self::isSidebarMenuSearch($item) ||
122 50
               self::isSidebarCustomSearch($item);
123
    }
124
125
    /**
126
     * Check if a menu item is allowed to be shown.
127
     *
128
     * @param mixed $item
129
     * @return bool
130
     */
131 56
    public static function isAllowed($item)
132
    {
133 56
        $isAllowed = ! (isset($item['restricted']) && $item['restricted']);
134
135 56
        return $item && $isAllowed;
136
    }
137
138
    /**
139
     * Check if a menu item is valid for the sidebar section.
140
     *
141
     * @param mixed $item
142
     * @return bool
143
     */
144 18
    public static function isValidSidebarItem($item)
145
    {
146 18
        return self::isHeader($item) ||
147 18
               self::isSidebarSearch($item) ||
148 18
               self::isSubmenu($item) ||
149 18
               self::isLink($item);
150
    }
151
152
    /**
153
     * Check if a menu item is valid for the navbar section.
154
     *
155
     * @param mixed $item
156
     * @return bool
157
     */
158 20
    public static function isValidNavbarItem($item)
159
    {
160 20
        return self::isNavbarSearch($item) ||
161 20
               self::isSubmenu($item) ||
162 20
               self::isLink($item);
163
    }
164
165
    /**
166
     * Check if a menu item belongs to the left section of the navbar.
167
     *
168
     * @param mixed $item
169
     * @return bool
170
     */
171 18
    public static function isNavbarLeftItem($item)
172
    {
173 18
        return self::isValidNavbarItem($item) &&
174 18
               isset($item['topnav']) &&
175 18
               $item['topnav'];
176
    }
177
178
    /**
179
     * Check if a menu item belongs to the right section of the navbar.
180
     *
181
     * @param mixed $item
182
     * @return bool
183
     */
184 19
    public static function isNavbarRightItem($item)
185
    {
186 19
        return self::isValidNavbarItem($item) &&
187 19
               isset($item['topnav_right']) &&
188 19
               $item['topnav_right'];
189
    }
190
191
    /**
192
     * Check if a menu item belongs to the user menu section of the navbar.
193
     *
194
     * @param mixed $item
195
     * @return bool
196
     */
197 19
    public static function isNavbarUserItem($item)
198
    {
199 19
        return self::isValidNavbarItem($item) &&
200 19
               isset($item['topnav_user']) &&
201 19
               $item['topnav_user'];
202
    }
203
204
    /**
205
     * Check if a menu item belongs to the sidebar.
206
     *
207
     * @param mixed $item
208
     * @return bool
209
     */
210 18
    public static function isSidebarItem($item)
211
    {
212 18
        return self::isValidSidebarItem($item) &&
213 18
               ! self::isNavbarLeftItem($item) &&
214 18
               ! self::isNavbarRightItem($item) &&
215 18
               ! self::isNavbarUserItem($item);
216
    }
217
}
218