Passed
Pull Request — master (#863)
by Diego
02:41
created

MenuItemHelper::isValidSidebarItem()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 4
c 1
b 0
f 0
nc 4
nop 1
dl 0
loc 6
ccs 5
cts 5
cp 1
crap 4
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 new navbar search bar.
50
     *
51
     * @param mixed $item
52
     * @return bool
53
     */
54 52
    public static function isNewNavbarSearch($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) || self::isNewNavbarSearch($item);
68
    }
69
70
    /**
71
     * Check if a menu item is a submenu.
72
     *
73
     * @param mixed $item
74
     * @return bool
75
     */
76 56
    public static function isSubmenu($item)
77
    {
78 56
        return isset($item['text']) &&
79 56
               isset($item['submenu']) &&
80 56
               is_array($item['submenu']);
81
    }
82
83
    /**
84
     * Check if a menu item is allowed to be shown.
85
     *
86
     * @param mixed $item
87
     * @return bool
88
     */
89 56
    public static function isAllowed($item)
90
    {
91 56
        $isAllowed = ! (isset($item['restricted']) && $item['restricted']);
92
93 56
        return $item && $isAllowed;
94
    }
95
96
    /**
97
     * Check if a menu item is a search item (for sidebar or navbar).
98
     *
99
     * @param mixed $item
100
     * @return bool
101
     */
102 50
    public static function isSearchItem($item)
103
    {
104 50
        return self::isLegacySearch($item) ||
105 50
               self::isNavbarSearch($item);
106
    }
107
108
    /**
109
     * Check if a menu item is valid for the sidebar section.
110
     *
111
     * @param mixed $item
112
     * @return bool
113
     */
114 18
    public static function isValidSidebarItem($item)
115
    {
116 18
        return self::isHeader($item) ||
117 18
               self::isLegacySearch($item) ||
118 18
               self::isSubmenu($item) ||
119 18
               self::isLink($item);
120
    }
121
122
    /**
123
     * Check if a menu item is valid for the navbar section.
124
     *
125
     * @param mixed $item
126
     * @return bool
127
     */
128 20
    public static function isValidNavbarItem($item)
129
    {
130 20
        return self::isNavbarSearch($item) ||
131 20
               self::isSubmenu($item) ||
132 20
               self::isLink($item);
133
    }
134
135
    /**
136
     * Check if a menu item belongs to the left section of the navbar.
137
     *
138
     * @param mixed $item
139
     * @return bool
140
     */
141 18
    public static function isNavbarLeftItem($item)
142
    {
143 18
        return self::isValidNavbarItem($item) &&
144 18
               isset($item['topnav']) &&
145 18
               $item['topnav'];
146
    }
147
148
    /**
149
     * Check if a menu item belongs to the right section of the navbar.
150
     *
151
     * @param mixed $item
152
     * @return bool
153
     */
154 19
    public static function isNavbarRightItem($item)
155
    {
156 19
        return self::isValidNavbarItem($item) &&
157 19
               isset($item['topnav_right']) &&
158 19
               $item['topnav_right'];
159
    }
160
161
    /**
162
     * Check if a menu item belongs to the user menu section of the navbar.
163
     *
164
     * @param mixed $item
165
     * @return bool
166
     */
167 19
    public static function isNavbarUserItem($item)
168
    {
169 19
        return self::isValidNavbarItem($item) &&
170 19
               isset($item['topnav_user']) &&
171 19
               $item['topnav_user'];
172
    }
173
174
    /**
175
     * Check if a menu item belongs to the sidebar.
176
     *
177
     * @param mixed $item
178
     * @return bool
179
     */
180 18
    public static function isSidebarItem($item)
181
    {
182 18
        return self::isValidSidebarItem($item) &&
183 18
               ! self::isNavbarLeftItem($item) &&
184 18
               ! self::isNavbarRightItem($item) &&
185 18
               ! self::isNavbarUserItem($item);
186
    }
187
}
188