Passed
Pull Request — master (#863)
by Diego
03:31
created

MenuItemHelper::isSearchBar()   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 legacy search bar.
37
     *
38
     * @param mixed $item
39
     * @return bool
40
     */
41 54
    public static function isLegacySearch($item)
42
    {
43 54
        return isset($item['text']) &&
44 54
               isset($item['search']) &&
45 54
               $item['search'];
46
    }
47
48
    /**
49
     * Check if a menu item is a navbar custom search bar.
50
     *
51
     * @param mixed $item
52
     * @return bool
53
     */
54 52
    public static function isNavbarCustomSearch($item)
55
    {
56 52
        return isset($item['type']) && $item['type'] === 'navbar-search';
57
    }
58
59
    /**
60
     * Check if a menu item is a navbar search item (legacy or new).
61
     *
62
     * @param mixed $item
63
     * @return bool
64
     */
65 52
    public static function isNavbarSearch($item)
66
    {
67 52
        return self::isLegacySearch($item) ||
68 52
               self::isNavbarCustomSearch($item);
69
    }
70
71
    /**
72
     * Check if a menu item is a submenu.
73
     *
74
     * @param mixed $item
75
     * @return bool
76
     */
77 56
    public static function isSubmenu($item)
78
    {
79 56
        return isset($item['text']) &&
80 56
               isset($item['submenu']) &&
81 56
               is_array($item['submenu']);
82
    }
83
84
    /**
85
     * Check if a menu item is allowed to be shown.
86
     *
87
     * @param mixed $item
88
     * @return bool
89
     */
90 56
    public static function isAllowed($item)
91
    {
92 56
        $isAllowed = ! (isset($item['restricted']) && $item['restricted']);
93
94 56
        return $item && $isAllowed;
95
    }
96
97
    /**
98
     * Check if a menu item is a search item (for sidebar or navbar).
99
     *
100
     * @param mixed $item
101
     * @return bool
102
     */
103 50
    public static function isSearchItem($item)
104
    {
105 50
        return self::isLegacySearch($item) ||
106 50
               self::isNavbarSearch($item);
107
    }
108
109
    /**
110
     * Check if a menu item is valid for the sidebar section.
111
     *
112
     * @param mixed $item
113
     * @return bool
114
     */
115 18
    public static function isValidSidebarItem($item)
116
    {
117 18
        return self::isHeader($item) ||
118 18
               self::isLegacySearch($item) ||
119 18
               self::isSubmenu($item) ||
120 18
               self::isLink($item);
121
    }
122
123
    /**
124
     * Check if a menu item is valid for the navbar section.
125
     *
126
     * @param mixed $item
127
     * @return bool
128
     */
129 20
    public static function isValidNavbarItem($item)
130
    {
131 20
        return self::isNavbarSearch($item) ||
132 20
               self::isSubmenu($item) ||
133 20
               self::isLink($item);
134
    }
135
136
    /**
137
     * Check if a menu item belongs to the left section of the navbar.
138
     *
139
     * @param mixed $item
140
     * @return bool
141
     */
142 18
    public static function isNavbarLeftItem($item)
143
    {
144 18
        return self::isValidNavbarItem($item) &&
145 18
               isset($item['topnav']) &&
146 18
               $item['topnav'];
147
    }
148
149
    /**
150
     * Check if a menu item belongs to the right section of the navbar.
151
     *
152
     * @param mixed $item
153
     * @return bool
154
     */
155 19
    public static function isNavbarRightItem($item)
156
    {
157 19
        return self::isValidNavbarItem($item) &&
158 19
               isset($item['topnav_right']) &&
159 19
               $item['topnav_right'];
160
    }
161
162
    /**
163
     * Check if a menu item belongs to the user menu section of the navbar.
164
     *
165
     * @param mixed $item
166
     * @return bool
167
     */
168 19
    public static function isNavbarUserItem($item)
169
    {
170 19
        return self::isValidNavbarItem($item) &&
171 19
               isset($item['topnav_user']) &&
172 19
               $item['topnav_user'];
173
    }
174
175
    /**
176
     * Check if a menu item belongs to the sidebar.
177
     *
178
     * @param mixed $item
179
     * @return bool
180
     */
181 18
    public static function isSidebarItem($item)
182
    {
183 18
        return self::isValidSidebarItem($item) &&
184 18
               ! self::isNavbarLeftItem($item) &&
185 18
               ! self::isNavbarRightItem($item) &&
186 18
               ! self::isNavbarUserItem($item);
187
    }
188
}
189