SideNav   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 191
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 8
Bugs 1 Features 0
Metric Value
dl 0
loc 191
rs 10
c 8
b 1
f 0
wmc 14
lcom 1
cbo 2

8 Methods

Rating   Name   Duplication   Size   Complexity  
A group() 0 11 1
A add() 0 11 1
A register() 0 19 2
A registerWithCheck() 0 7 2
A addSub() 0 8 1
A routes() 0 9 2
A render() 0 11 2
A checkGroupName() 0 8 3
1
<?php
2
3
/**
4
 * SideNav main class
5
 *
6
 * @author Alireza Josheghani <[email protected]>
7
 * @version 1.0
8
 * @package SideNav
9
 * @since 19 Sep 2016
10
 */
11
12
namespace Anetwork\SideNav;
13
14
class SideNav
15
{
16
    use SideNavHelpers;
17
18
    /**
19
     * group route
20
     *
21
     * @var string
22
     */
23
    private static $group;
24
25
    /**
26
     * current position of route
27
     *
28
     * @var string
29
     */
30
    private static $currentItem;
31
32
    /**
33
     * All routes registered
34
     *
35
     * @var array
36
     */
37
    private static $routes = [];
38
39
    /**
40
     * render array
41
     *
42
     * @var array
43
     */
44
    private static $menu = [];
45
46
    /**
47
     * Make the SideNav group
48
     *
49
     * @author Alireza Josheghani <[email protected]>
50
     * @since  19 Sep 2016
51
     * @param  $group
52
     * @param  $callback
53
     */
54
    public static function group($group, $callback)
55
    {
56
        // set group menu name
57
        self::$group = $group;
58
59
        // set group menu
60
        self::$menu[$group] = [];
61
62
        // run callback function
63
        $callback();
64
    }
65
66
    /**
67
     * Register a new menu item
68
     *
69
     * @author Alireza Josheghani <[email protected]>
70
     * @since  19 Sep 2016
71
     * @param  $route
72
     * @param  $callback
73
     */
74
    public static function register($route, $callback)
75
    {
76
        // set current route
77
        self::$currentItem = $route;
78
79
        // register route
80
        self::$routes[self::$currentItem] = [];
81
82
        // make menu array
83
        $array = self::add($route, $callback);
84
85
        if (self::checkGroupName(self::$group)) {
86
            // add to the group render array
87
            return array_push(self::$menu[self::$group], $array);
88
        }
89
90
        // add to the single render array
91
        return array_push(self::$menu, $array);
92
    }
93
94
    /**
95
     * Register a new menu item
96
     *
97
     * @author Alireza Josheghani <[email protected]>
98
     * @since  19 Sep 2016
99
     * @param  $route
100
     * @param  $callback
101
     * @param  $checkCallback
102
     */
103
    public static function registerWithCheck($route , $callback , $check)
104
    {
105
        // check status of route
106
        if ($check()) {
107
            self::register($route, $callback);
108
        }
109
    }
110
111
    /**
112
     * Add the submenu to item
113
     *
114
     * @author Alireza Josheghani <[email protected]>
115
     * @since  19 Sep 2016
116
     * @param  $route
117
     * @param  $callback
118
     * @return array
119
     */
120
    public static function addSub($route, $callback)
121
    {
122
        // register route name
123
        array_push(self::$routes[self::$currentItem], $route);
124
125
        // return submenu array
126
        return self::add($route, $callback);
127
    }
128
129
    /**
130
     * Add the menu item
131
     *
132
     * @author Alireza Josheghani <[email protected]>
133
     * @since  19 Sep 2016
134
     * @param  $route
135
     * @param  $callback
136
     * @return array
137
     */
138
    private static function add($route,$callback)
139
    {
140
        // instance of menu class
141
        $menu = new Menu;
142
143
        // run callback function
144
        $callback($menu);
145
146
        // Make the menu object array
147
        return $menu->make($route);
148
    }
149
150
    /**
151
     * Get all routes
152
     *
153
     * @author Alireza Josheghani <[email protected]>
154
     * @since  19 Sep 2016
155
     * @return array
156
     */
157
    public static function routes($index = null)
158
    {
159
        if($index !== null) {
160
            return self::$routes[$index];
161
        }
162
163
        // return array of all routes-name
164
        return self::$routes;
165
    }
166
167
168
    /**
169
     * Render the menu items
170
     *
171
     * @author Alireza Josheghani <[email protected]>
172
     * @since  19 Sep 2016
173
     * @param  $type
174
     * @return mixed
175
     */
176
    public static function render($type = null)
177
    {
178
        // check $type was used
179
        if(isset($type)) {
180
            // return group menu
181
            return self::$menu[$type];
182
        }
183
184
        // return single menu array
185
        return self::$menu;
186
    }
187
188
    /**
189
     * Check group id
190
     *
191
     * @author Alireza Josheghani <[email protected]>
192
     * @since  19 Sep 2016
193
     * @return bool
194
     */
195
    private static function checkGroupName($type)
196
    {
197
        if($type !== null && isset(self::$group)) {
198
            return true;
199
        }
200
201
        return false;
202
    }
203
204
}
205