Menu::regex()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
/**
4
 * SideNav Menu class
5
 *
6
 * @author Alireza Josheghani <[email protected]>
7
 * @version 1.0
8
 * @package SideNav
9
 * @since 20 Sep 2016
10
 */
11
12
namespace Anetwork\SideNav;
13
14
class Menu
15
{
16
    /**
17
     * the route name
18
     *
19
     * @var string
20
     */
21
    private $route;
22
23
    /**
24
     * the title option
25
     *
26
     * @var string
27
     */
28
    private $title;
29
30
    /**
31
     * the url option
32
     *
33
     * @var array
34
     */
35
    private $link = [
36
        'new_tab' => false,
37
        'value' => null,
38
        'regex' => []
39
    ];
40
41
    /**
42
     * the class-name option
43
     *
44
     * @var string
45
     */
46
    private $className;
47
48
    /**
49
     * the icon option
50
     *
51
     * @var array
52
     */
53
    private $icon = [ 'tag' => 'i', 'value' => null ];
54
55
    /**
56
     * is_new icon option
57
     *
58
     * @var boolean
59
     */
60
    protected $isNew = false;
61
62
    /**
63
     * selected option
64
     *
65
     * @var boolean
66
     */
67
    protected $selected = false;
68
69
    /**
70
     * open child when item is active
71
     *
72
     * @var boolean
73
     */
74
    protected $openChildOnClick = true;
75
76
    /**
77
     * Set parent class attribute when child open
78
     *
79
     * @var boolean
80
     */
81
    protected $parentClassWhenChildOpen;
82
83
    /**
84
     * the sub menu
85
     *
86
     * @var array
87
     */
88
    protected $submenu = [];
89
90
    /**
91
     * Attach sub menu array
92
     *
93
     * @author Alireza Josheghani <[email protected]>
94
     * @since  20 Sep 2016
95
     * @param  $route
96
     * @param  $callback
97
     */
98
    public function sub($route, $callback)
99
    {
100
        $sub = SideNav::addSub($route, $callback);
101
        array_push($this->submenu, $sub);
102
    }
103
104
    /**
105
     * Attach sub menu array with checking status of item
106
     *
107
     * @author Alireza Josheghani <[email protected]>
108
     * @since  20 Sep 2016
109
     * @param  $route
110
     * @param  $callback
111
     * @param  $checkCallback
112
     */
113
    public function subWithCheck($route, $callback , $checkCallback)
114
    {
115
        if($checkCallback()) {
116
            $this->sub($route,$callback);
117
        }
118
    }
119
120
    /**
121
     * Set the icon option
122
     *
123
     * @author Alireza Josheghani <[email protected]>
124
     * @since  20 Sep 2016
125
     * @param  $icon
126
     */
127
    public function icon($icon)
128
    {
129
        $this->icon['value'] = $icon;
130
131
        return $this;
132
    }
133
134
    /**
135
     * Set the icon tag option
136
     *
137
     * @author Alireza Josheghani <[email protected]>
138
     * @since  20 Sep 2016
139
     * @param  $icon
140
     */
141
    public function tag($tag)
142
    {
143
        $this->icon['tag'] = $tag;
144
145
        return $this;
146
    }
147
148
    /**
149
     * Set the title option
150
     *
151
     * @author Alireza Josheghani <[email protected]>
152
     * @since  20 Sep 2016
153
     * @param  $title
154
     */
155
    public function title($title)
156
    {
157
        $this->title = $title;
158
    }
159
160
    /**
161
     * Set parent class name when child open
162
     *
163
     * @author Alireza Josheghani <[email protected]>
164
     * @since  29 Oct 2016
165
     * @param $class
166
     */
167
    public function parentClassWhenChildOpen($class)
168
    {
169
        $this->parentClassWhenChildOpen = $class;
170
    }
171
172
    /**
173
     * Set the class-name option
174
     *
175
     * @author Alireza Josheghani <[email protected]>
176
     * @since  20 Sep 2016
177
     * @param  $class_name
178
     */
179
    public function className($className)
180
    {
181
        $this->className = $className;
182
    }
183
184
    /**
185
     * Set the newTab option
186
     *
187
     * @author Alireza Josheghani <[email protected]>
188
     * @since  20 Sep 2016
189
     * @param  $newtab : boolean
190
     */
191
    public function newTab($newtab)
192
    {
193
        $this->link['new_tab'] = $newtab;
194
    }
195
196
    /**
197
     * Set route name
198
     *
199
     * @author Alireza Josheghani <[email protected]>
200
     * @since  20 Sep 2016
201
     * @param  $route
202
     */
203
    public function routeName($route)
204
    {
205
        $this->route = $route;
206
    }
207
208
    /**
209
     * Set the link of menu
210
     *
211
     * @author Alireza Josheghani <[email protected]>
212
     * @since  20 Sep 2016
213
     * @param  $link
214
     */
215
    public function link($link)
216
    {
217
        $this->link['value'] = $link;
218
    }
219
220
    /**
221
     * Set the regex link of menu
222
     *
223
     * @author Alireza Josheghani <[email protected]>
224
     * @since  16 Oxt 2016
225
     * @param  array $regex
226
     */
227
    public function regex(array $regex)
228
    {
229
        $this->link['regex'] = $regex;
230
    }
231
232
    /**
233
     * Set target link of item
234
     *
235
     * @author Alireza Josheghani <[email protected]>
236
     * @since  20 Sep 2016
237
     * @param  $isNew
238
     */
239
    public function isNew($isNew)
240
    {
241
        $this->isNew = $isNew;
242
    }
243
244
    /**
245
     * Set item is selected
246
     *
247
     * @author Alireza Josheghani <[email protected]>
248
     * @since  20 Sep 2016
249
     * @param  $type
250
     */
251
    public function selected($type)
252
    {
253
        $this->selected = $type;
254
    }
255
256
    /**
257
     * Set open child when item is active
258
     *
259
     * @author Alireza Josheghani <[email protected]>
260
     * @since  20 Sep 2016
261
     * @param  $type
262
     */
263
    public function openChildOnClick($type)
264
    {
265
        $this->openChildOnClick = $type;
266
    }
267
268
    /**
269
     * make the return item array
270
     *
271
     * @author Alireza Josheghani <[email protected]>
272
     * @since  20 Sep 2016
273
     * @return array
274
     */
275
    public function make($route)
276
    {
277
        // set route name of menu
278
        $this->routeName($route);
279
280
        $submenu = [];
281
282
        if(! empty($this->submenu)) {
283
            $submenu = $this->submenu;
284
        }
285
286
        $result = [
287
            'name' => $this->route,
288
            'title' => $this->title,
289
            'link' => $this->link,
290
            'icon' => $this->icon,
291
            'is_new' => $this->isNew,
292
            'class' => $this->className,
293
            'selected' => $this->selected,
294
            'open_child_on_click' => $this->openChildOnClick,
295
            'parent_class_when_open_child' => $this->parentClassWhenChildOpen,
296
            'sub' => $submenu
297
        ];
298
299
        if(empty($this->submenu)) {
300
            unset($result['sub']);
301
        }
302
303
        return $result;
304
    }
305
306
}
307