Completed
Push — master ( cbbc28...ae46c4 )
by
unknown
01:53
created

SideNav::checkStatus()   B

Complexity

Conditions 5
Paths 7

Size

Total Lines 27
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

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