Completed
Push — master ( 605113...c21a7a )
by
unknown
87:53
created

SideNav::checkStatus()   B

Complexity

Conditions 5
Paths 7

Size

Total Lines 25
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 25
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 $type;
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
    // set result render type
41
    public static $resultType = 'array';
42
43
    /**
44
     * Define checkstatus object
45
     * @author Alireza Josheghani <[email protected]>
46
     * @since 19 Sep 2016
47
     * @param $class
48
     */
49
    public static function checkStatusObject($class,$method)
50
    {
51
        self::$checkStatusObject = [
52
            'object' => $class,
53
            'method' => $method
54
        ];
55
    }
56
57
    /**
58
     * Make the SideNav group
59
     * @author Alireza Josheghani <[email protected]>
60
     * @since 19 Sep 2016
61
     * @param $type
62
     * @param $callback
63
     */
64
    public static function group($type, $callback)
65
    {
66
        // set group menu name
67
        self::$type = $type;
68
69
        // set group menu
70
        self::$menu[$type] = [];
71
72
        // run callback function
73
        $callback();
74
    }
75
76
    /**
77
     * Register a new menu item
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::$type))
95
            // add to the group render array
96
            array_push(self::$menu[self::$type], $array);
97
98
        // add to the single render array
99
        array_push(self::$menu, $array);
100
    }
101
102
    /**
103
     * Register a new menu item
104
     * @author Alireza Josheghani <[email protected]>
105
     * @since 19 Sep 2016
106
     * @param $route
107
     * @param $callback
108
     * @param $checkCallback
109
     */
110
    public static function registerWithCheckStatus($route , $callback , $checkCallback = null)
111
    {
112
        // check status of route
113
        if (self::checkStatus($route,$checkCallback)){
114
            self::register($route, $callback);
115
        }
116
    }
117
118
    /**
119
     * Add the submenu to item
120
     * @author Alireza Josheghani <[email protected]>
121
     * @since 19 Sep 2016
122
     * @param $route
123
     * @param $callback
124
     * @return array
125
     */
126
    public static function addSub($route, $callback)
127
    {
128
        // register route name
129
        array_push(self::$routes[self::$currentRoute],$route);
130
131
        // return submenu array
132
        return self::add($route,$callback);
133
    }
134
135
    /**
136
     * Add the menu item
137
     * @author Alireza Josheghani <[email protected]>
138
     * @since 19 Sep 2016
139
     * @param $route
140
     * @param $callback
141
     * @return array
142
     */
143
    private static function add($route,$callback)
144
    {
145
        // instance of menu class
146
        $menu = new Menu;
147
148
        // run callback function
149
        $callback($menu);
150
151
        // Make the menu object array
152
        return $menu->make($route);
153
    }
154
155
    /**
156
     * Get all routes
157
     * @author Alireza Josheghani <[email protected]>
158
     * @since 19 Sep 2016
159
     * @return array
160
     */
161
    public static function routes()
162
    {
163
        // return json array of all routes-name
164
        if(self::$resultType === 'json')
165
            return json_encode(self::$routes);
166
167
        // return array of all routes-name
168
        return self::$routes;
169
    }
170
171
    /**
172
     * set render type
173
     * @author Alireza Josheghani <[email protected]>
174
     * @since 20 Sep 2016
175
     * @param $type
176
     * @return SideNav
177
     */
178
    public static function type($type)
179
    {
180
        self::$resultType = $type;
181
        return new self;
182
    }
183
184
    /**
185
     * Render the menu items
186
     * @author Alireza Josheghani <[email protected]>
187
     * @since 19 Sep 2016
188
     * @return mixed
189
     */
190
    public static function render($type = null)
191
    {
192
        if(self::checkGroupId($type))
193
        {
194
195
            // render the menu json array
196
            if(self::$resultType === 'json')
197
                return json_encode(self::$menu[$type]);
198
199
            // render the menu array
200
            return self::$menu[$type];
201
        }
202
203
        // return single menu json array
204
        if(self::$resultType === 'json')
205
            return json_encode(self::$menu);
206
207
        // return single menu array
208
        return self::$menu[0];
209
    }
210
211
    /**
212
     * Check group id
213
     * @author Alireza Josheghani <[email protected]>
214
     * @since 19 Sep 2016
215
     * @return bool
216
     */
217
    public static function checkGroupId($type)
218
    {
219
        if($type !== null && isset(self::$type))
220
            return true;
221
222
        return false;
223
    }
224
225
    /**
226
     * Check user status
227
     * @author Alireza Josheghani <[email protected]>
228
     * @since 19 Sep 2016
229
     * @param $route
230
     * @param $callback
231
     * @return bool
232
     */
233
    public static function checkStatus($route,$callback = false)
234
    {
235
        if ($callback instanceof \Closure) {
236
            if ($callback())
237
                return true;
238
        }
239
240
        // check status class name
241
        $obj = self::$checkStatusObject['object'];
242
243
        // check status method name
244
        $method = self::$checkStatusObject['method'];
245
246
        if(class_exists($obj)) {
247
            // instance of class
248
            $class = new $obj;
249
250
            // call method of class
251
            if ($class->$method($route))
252
                return true;
253
            return false;
254
        } else {
255
            throw new \Exception("The CheckStatus class not found !");
256
        }
257
    }
258
259
}
260