Passed
Pull Request — master (#868)
by Diego
14:58 queued 05:01
created

MenuItemHelper::isSidebarMenuSearch()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
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 2
cts 2
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 54
    public static function isSubmenu($item)
42
    {
43 54
        return isset($item['text']) &&
44 54
               isset($item['submenu']) &&
45 54
               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 52
    public static function isLegacySearch($item)
55
    {
56 52
        return isset($item['text']) &&
57 52
               isset($item['search']) &&
58 52
               $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
               $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 56
     */
80
    public static function isSidebarCustomSearch($item)
81 56
    {
82 56
        return isset($item['text']) &&
83 56
               isset($item['type']) &&
84
               $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 56
     */
93
    public static function isSidebarMenuSearch($item)
94 56
    {
95
        return isset($item['text']) &&
96 56
               isset($item['type']) &&
97
               $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 50
     */
106
    public static function isNavbarSearch($item)
107 50
    {
108 50
        return self::isLegacySearch($item) ||
109
               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 18
     */
118
    public static function isSidebarSearch($item)
119 18
    {
120 18
        return self::isLegacySearch($item) ||
121 18
               self::isSidebarMenuSearch($item) ||
122 18
               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 20
    public static function isAllowed($item)
132
    {
133 20
        $isAllowed = ! (isset($item['restricted']) && $item['restricted']);
134 20
135 20
        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
               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 19
     */
158
    public static function isValidNavbarItem($item)
159 19
    {
160 19
        return self::isNavbarSearch($item) ||
161 19
               self::isSubmenu($item) ||
162
               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 19
     */
171
    public static function isNavbarLeftItem($item)
172 19
    {
173 19
        return self::isValidNavbarItem($item) &&
174 19
               isset($item['topnav']) &&
175
               $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 18
     */
184
    public static function isNavbarRightItem($item)
185 18
    {
186 18
        return self::isValidNavbarItem($item) &&
187 18
               isset($item['topnav_right']) &&
188 18
               $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
    public static function isNavbarUserItem($item)
198
    {
199
        return self::isValidNavbarItem($item) &&
200
               isset($item['topnav_user']) &&
201
               $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
    public static function isSidebarItem($item)
211
    {
212
        return self::isValidSidebarItem($item) &&
213
               ! self::isNavbarLeftItem($item) &&
214
               ! self::isNavbarRightItem($item) &&
215
               ! self::isNavbarUserItem($item);
216
    }
217
}
218