Completed
Push — master ( 27524b...d8cc6e )
by
unknown
02:16
created

Menu::parentClassWhenChildOpen()   A

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