Passed
Pull Request — master (#868)
by Diego
02:40
created

MenuItemHelper::isSidebarMenuSearch()   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 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 3
c 1
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 a search item (for sidebar or navbar).
127
     *
128
     * @param mixed $item
129
     * @return bool
130
     */
131 50
    public static function isSearchItem($item)
132
    {
133 50
        return self::isNavbarSearch($item) ||
134 50
               self::isSidebarSearch($item);
135
    }
136
137
    /**
138
     * Check if a menu item is allowed to be shown.
139
     *
140
     * @param mixed $item
141
     * @return bool
142
     */
143 56
    public static function isAllowed($item)
144
    {
145 56
        $isAllowed = ! (isset($item['restricted']) && $item['restricted']);
146
147 56
        return $item && $isAllowed;
148
    }
149
150
    /**
151
     * Check if a menu item is valid for the sidebar section.
152
     *
153
     * @param mixed $item
154
     * @return bool
155
     */
156 18
    public static function isValidSidebarItem($item)
157
    {
158 18
        return self::isHeader($item) ||
159 18
               self::isSidebarSearch($item) ||
160 18
               self::isSubmenu($item) ||
161 18
               self::isLink($item);
162
    }
163
164
    /**
165
     * Check if a menu item is valid for the navbar section.
166
     *
167
     * @param mixed $item
168
     * @return bool
169
     */
170 20
    public static function isValidNavbarItem($item)
171
    {
172 20
        return self::isNavbarSearch($item) ||
173 20
               self::isSubmenu($item) ||
174 20
               self::isLink($item);
175
    }
176
177
    /**
178
     * Check if a menu item belongs to the left section of the navbar.
179
     *
180
     * @param mixed $item
181
     * @return bool
182
     */
183 18
    public static function isNavbarLeftItem($item)
184
    {
185 18
        return self::isValidNavbarItem($item) &&
186 18
               isset($item['topnav']) &&
187 18
               $item['topnav'];
188
    }
189
190
    /**
191
     * Check if a menu item belongs to the right section of the navbar.
192
     *
193
     * @param mixed $item
194
     * @return bool
195
     */
196 19
    public static function isNavbarRightItem($item)
197
    {
198 19
        return self::isValidNavbarItem($item) &&
199 19
               isset($item['topnav_right']) &&
200 19
               $item['topnav_right'];
201
    }
202
203
    /**
204
     * Check if a menu item belongs to the user menu section of the navbar.
205
     *
206
     * @param mixed $item
207
     * @return bool
208
     */
209 19
    public static function isNavbarUserItem($item)
210
    {
211 19
        return self::isValidNavbarItem($item) &&
212 19
               isset($item['topnav_user']) &&
213 19
               $item['topnav_user'];
214
    }
215
216
    /**
217
     * Check if a menu item belongs to the sidebar.
218
     *
219
     * @param mixed $item
220
     * @return bool
221
     */
222 18
    public static function isSidebarItem($item)
223
    {
224 18
        return self::isValidSidebarItem($item) &&
225 18
               ! self::isNavbarLeftItem($item) &&
226 18
               ! self::isNavbarRightItem($item) &&
227 18
               ! self::isNavbarUserItem($item);
228
    }
229
}
230