Completed
Push — master ( b3536c...9e814b )
by Andrey
01:08
created

src/widgets/menu/MainMenuItem.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Itstructure\AdminModule\widgets\menu;
4
5
use Yii;
6
use yii\base\{Widget, InvalidConfigException};
7
8
/**
9
 * Class MainMenuItem
10
 * Widget to render menu item for main menu.
11
 *
12
 * @property string $icon Css-class for icon of menu item.
13
 * Example: fa fa-database.
14
 * @property bool $active Is menu item need to be opened (if has subitems) or selected.
15
 * @property bool $display Is menu item need to be displayed.
16
 * @property string $title Title of menu item.
17
 * @property MainMenu[] $subItems Array of subItems menu items.
18
 * @property string $url Url of menu item.
19
 * @property string $class Class for <li> tag.
20
 *
21
 * @package Itstructure\AdminModule\widgets
22
 *
23
 * @author Andrey Girnik <[email protected]>
24
 */
25
class MainMenuItem extends Widget
26
{
27
    /**
28
     * Css-class for icon of menu item.
29
     * Example: fa fa-database.
30
     *
31
     * @var string
32
     */
33
    protected $icon = '';
34
35
    /**
36
     * Is menu item need to be opened (if has subitems) or selected.
37
     *
38
     * @var bool
39
     */
40
    protected $active = false;
41
42
    /**
43
     * Is menu item need to be displayed.
44
     *
45
     * @var bool
46
     */
47
    protected $display = true;
48
49
    /**
50
     * Title of menu item.
51
     *
52
     * @var string
53
     */
54
    protected $title = '';
55
56
    /**
57
     * Array of subItems menu items.
58
     *
59
     * @var MainMenu[]
60
     */
61
    protected $subItems = [];
62
63
    /**
64
     * Url of menu item.
65
     *
66
     * @var string
67
     */
68
    protected $url = '';
69
70
    /**
71
     * Class for <li> tag.
72
     *
73
     * @var string
74
     */
75
    protected $class;
76
77
    /**
78
     * Executes the widget.
79
     *
80
     * @return string the result of widget execution to be outputted.
81
     */
82
    public function run(): string
83
    {
84
        if (false === $this->display) {
85
            return '';
86
        }
87
88
        if ($this->hasSubItems()) {
89
            $this->class = $this->isActive() ? 'active treeview' : 'treeview';
90
        } else {
91
            $this->class = $this->isActive() ? 'active' : '';
92
        }
93
94
        return $this->render('main-menu-items', [
95
            'item' => $this,
96
        ]);
97
    }
98
99
    /**
100
     * Icon getter.
101
     *
102
     * @return string
103
     */
104
    public function getIcon(): string
105
    {
106
        return $this->icon;
107
    }
108
109
    /**
110
     * Icon setter.
111
     *
112
     * @param string $icon Icon of menu item
113
     *
114
     * @return $this
115
     */
116
    public function setIcon($icon)
117
    {
118
        $this->icon = $icon;
119
        return $this;
120
    }
121
122
    /**
123
     * Url getter.
124
     *
125
     * @return string
126
     */
127
    public function getUrl(): string
128
    {
129
        return $this->url;
130
    }
131
132
    /**
133
     * Url setter.
134
     *
135
     * @param string $url Url of menu item
136
     *
137
     * @return $this
138
     */
139
    public function setUrl($url)
140
    {
141
        $this->url = $url;
142
        return $this;
143
    }
144
145
    /**
146
     * Get class for <li> tag.
147
     *
148
     * @return string
149
     */
150
    public function getClass(): string
151
    {
152
        return $this->class;
153
    }
154
155
    /**
156
     * Is menu item need to be opened (if has subitems) or selected.
157
     *
158
     * @return bool
159
     */
160
    public function isActive(): bool
161
    {
162
        return $this->active;
163
    }
164
165
    /**
166
     * Set menu item need to be opened (if has subitems) or selected.
167
     *
168
     * @param bool $active
169
     *
170
     * @return $this
171
     */
172
    public function setActive($active)
173
    {
174
        $this->active = $active;
175
        return $this;
176
    }
177
178
    /**
179
     * Is menu item need to be displayed.
180
     *
181
     * @return bool
182
     */
183
    public function isDisplay()
184
    {
185
        return $this->display;
186
    }
187
188
    /**
189
     * Set menu item need to be displayed.
190
     *
191
     * @param bool $display
192
     *
193
     * @return $this
194
     */
195
    public function setDisplay($display)
196
    {
197
        $this->display = $display;
198
        return $this;
199
    }
200
201
    /**
202
     * Get title of menu item.
203
     *
204
     * @return string
205
     */
206
    public function getTitle(): string
207
    {
208
        return $this->title;
209
    }
210
211
    /**
212
     * Set title of menu item.
213
     *
214
     * @param string $title
215
     *
216
     * @return $this
217
     */
218
    public function setTitle($title)
219
    {
220
        $this->title = $title;
221
        return $this;
222
    }
223
224
    /**
225
     * Get array of subItems menu items.
226
     *
227
     * @return MainMenu[]
228
     */
229
    public function getSubItems()
230
    {
231
        return $this->subItems;
232
    }
233
234
    /**
235
     * Set array of subItems menu items.
236
     *
237
     * @param array|MainMenu[] $subItems
238
     *
239
     * @throws InvalidConfigException
240
     *
241
     * @return $this
242
     */
243
    public function setSubItems(array $subItems)
244
    {
245
        $this->subItems = [];
246
        foreach ($subItems as $menuItem) {
247
            $this->addSubItems($menuItem);
248
        }
249
250
        return $this;
251
    }
252
253
    /**
254
     * Add to array of subItems menu items.
255
     *
256
     * @param array|MainMenu $menuItem
257
     *
258
     * @throws InvalidConfigException
259
     *
260
     * @return $this
261
     */
262 View Code Duplication
    public function addSubItems($menuItem)
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
263
    {
264
        if (is_array($menuItem)) {
265
            if (!array_key_exists('class', $menuItem)) {
266
                $menuItem['class'] = MainMenuItem::class;
267
            }
268
269
            $menuItem = Yii::createObject($menuItem);
270
        }
271
272
        $this->subItems[] = $menuItem;
273
        return $this;
274
    }
275
276
    /**
277
     * Check if item has subitems.
278
     *
279
     * @return bool
280
     */
281
    public function hasSubItems()
282
    {
283
        return count($this->subItems) > 0;
284
    }
285
}
286