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

MenuItemHelper::isNavbarSearch()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

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