Completed
Pull Request — master (#3)
by ARCANEDEV
04:19
created

SidebarItem::setTitle()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 8
ccs 5
cts 5
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php namespace Arcanesoft\Sidebar;
2
3
use Illuminate\Contracts\Support\Arrayable;
4
use Illuminate\Contracts\Support\Jsonable;
5
use Illuminate\Support\Arr;
6
use Illuminate\Support\HtmlString;
7
use JsonSerializable;
8
9
/**
10
 * Class     SidebarItem
11
 *
12
 * @package  Arcanesoft\Sidebar
13
 * @author   ARCANEDEV <[email protected]>
14
 */
15
class SidebarItem implements Arrayable, Jsonable, JsonSerializable
16
{
17
    /* -----------------------------------------------------------------
18
     |  Properties
19
     | -----------------------------------------------------------------
20
     */
21
22
    /** @var  string */
23
    protected $name;
24
25
    /** @var  string */
26
    public $title;
27
28
    /** @var  string */
29
    public $url;
30
31
    /** @var  string */
32
    public $icon;
33
34
    /** @var  \Arcanesoft\Sidebar\SidebarCollection */
35
    public $children;
36
37
    /** @var  array */
38
    protected $roles;
39
40
    /** @var  array */
41
    protected $permissions;
42
43
    /** @var boolean */
44
    private $selected;
45
46
    /* -----------------------------------------------------------------
47
     |  Constructor
48
     | -----------------------------------------------------------------
49
     */
50
51
    /**
52
     * SidebarItem constructor.
53
     *
54
     * @param  array  $attributes
55
     */
56 52
    public function __construct(array $attributes)
57
    {
58 52
        $this->name        = Arr::pull($attributes, 'name');
59 52
        $this->setTitle(Arr::pull($attributes, 'title'));
60 52
        $this->icon        = Arr::pull($attributes, 'icon');
61 52
        $this->roles       = Arr::pull($attributes, 'roles', []);
0 ignored issues
show
Documentation Bug introduced by
It seems like \Illuminate\Support\Arr:...utes, 'roles', array()) of type * is incompatible with the declared type array of property $roles.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
62 52
        $this->permissions = Arr::pull($attributes, 'permissions', []);
0 ignored issues
show
Documentation Bug introduced by
It seems like \Illuminate\Support\Arr:...'permissions', array()) of type * is incompatible with the declared type array of property $permissions.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
63 52
        $this->children    = (new SidebarCollection)->pushItems(
64 52
            Arr::pull($attributes, 'children', [])
65
        );
66 52
        $this->selected    = false;
67
68 52
        $this->parseUrl($attributes);
69 52
    }
70
71
    /* -----------------------------------------------------------------
72
     |  Setters & Getters
73
     | -----------------------------------------------------------------
74
     */
75
76
    /**
77
     * Get the sidebar item's name.
78
     *
79
     * @return string
80
     */
81 8
    public function name()
82
    {
83 8
        return $this->name;
84
    }
85
86
    /**
87
     * Set the title.
88
     *
89
     * @param  string  $title
90
     *
91
     * @return $this
92
     */
93 52
    public function setTitle(string $title)
94
    {
95 52
        $this->title = trans()->has($title)
96 4
            ? trans()->get($title)
97 48
            : $title;
98
99 52
        return $this;
100
    }
101
102
    /**
103
     * Set the url.
104
     *
105
     * @param  string  $url
106
     *
107
     * @return $this
108
     */
109 52
    public function setUrl(string $url)
110
    {
111 52
        $this->url = $url;
112
113 52
        return $this;
114
    }
115
116
    /**
117
     * Set the selected item.
118
     *
119
     * @param  string  $name
120
     *
121
     * @return $this
122
     */
123 12
    public function setSelected(string $name)
124
    {
125 12
        $this->selected = ($this->name === $name);
126 12
        $this->children->setSelected($name);
127
128 12
        return $this;
129
    }
130
131
    /**
132
     * Get the roles.
133
     *
134
     * @return array|mixed
135
     */
136 8
    public function roles()
137
    {
138 8
        return $this->roles;
139
    }
140
141
    /**
142
     * Get the permissions.
143
     *
144
     * @return array|mixed
145
     */
146 8
    public function permissions()
147
    {
148 8
        return $this->permissions;
149
    }
150
151
    /* -----------------------------------------------------------------
152
     |  Main Methods
153
     | -----------------------------------------------------------------
154
     */
155
156
    /**
157
     * Set the url from the route.
158
     *
159
     * @param  string  $name
160
     * @param  array   $params
161
     *
162
     * @return $this
163
     */
164 4
    public function route($name, array $params = [])
165
    {
166 4
        return $this->setUrl(
167 3
            route($name, $params)
168
        );
169
    }
170
171
    /**
172
     * Set the url from the action.
173
     *
174
     * @param  string|array  $name
175
     * @param  array         $params
176
     *
177
     * @return $this
178
     */
179
    public function action($name, array $params = [])
180
    {
181
        return $this->setUrl(
182
            action($name, $params)
183
        );
184
    }
185
186
    /**
187
     * Set the icon.
188
     *
189
     * @param  string  $classes
190
     *
191
     * @return \Illuminate\Support\HtmlString
192
     */
193
    public function icon($classes = '') : HtmlString
194
    {
195
        $html = $this->icon
196
            ? '<i class="' . $this->icon . ' ' . $classes . '"></i>'
197
            : '';
198
199
        return new HtmlString($html);
200
    }
201
202
    /**
203
     * Get the active/inactive class.
204
     *
205
     * @param  string  $active
206
     * @param  string  $inactive
207
     *
208
     * @return string
209
     */
210 4
    public function active($active = 'active', $inactive = ''): string
211
    {
212 4
        return $this->isActive() ? $active : $inactive;
213
    }
214
215
    /**
216
     * Push multiple child items.
217
     *
218
     * @param  array  $items
219
     *
220
     * @return $this
221
     */
222 4
    public function addChildren(array $items)
223
    {
224 4
        $this->children->pushItems($items);
225
226 4
        return $this;
227
    }
228
229
    /* -----------------------------------------------------------------
230
     |  Check Methods
231
     | -----------------------------------------------------------------
232
     */
233
234
    /**
235
     * Check if has children.
236
     *
237
     * @return bool
238
     */
239 44
    public function hasChildren(): bool
240
    {
241 44
        return $this->children->isNotEmpty();
242
    }
243
244
    /**
245
     * Check if has any selected children.
246
     *
247
     * @return bool
248
     */
249 40
    public function hasAnySelectedChildren()
250
    {
251 40
        return $this->hasChildren()
252 40
            && $this->children->hasAnySelected();
253
    }
254
255
    /**
256
     * Check if the item is active.
257
     *
258
     * @return bool
259
     */
260 40
    public function isActive() : bool
261
    {
262 40
        return $this->isSelected() || $this->hasAnySelectedChildren();
263
    }
264
265
    /**
266
     * Check if the item is selected.
267
     *
268
     * @return bool
269
     */
270 40
    public function isSelected() : bool
271
    {
272 40
        return $this->selected;
273
    }
274
275
    /* -----------------------------------------------------------------
276
     |  Other Methods
277
     | -----------------------------------------------------------------
278
     */
279
280
    /**
281
     * Parse the url attribute.
282
     *
283
     * @param  array  $attributes
284
     *
285
     * @return void
286
     */
287 52
    protected function parseUrl(array $attributes) : void
288
    {
289 52
        if (isset($attributes['url']))
290 52
            $this->setUrl($attributes['url']);
291 4
        elseif (isset($attributes['route']))
292 4
            $this->route(...Arr::wrap($attributes['route']));
0 ignored issues
show
Documentation introduced by
\Illuminate\Support\Arr:...p($attributes['route']) is of type array, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
293
        elseif (isset($attributes['action']))
294
            $this->action(...Arr::wrap($attributes['action']));
295
        else
296
            $this->setUrl('#');
297 52
    }
298
299
    /**
300
     * Get the instance as an array.
301
     *
302
     * @return array
303
     */
304 24
    public function toArray()
305
    {
306
        return [
307 24
            'name'        => $this->name,
308 24
            'title'       => $this->title,
309 24
            'url'         => $this->url,
310 24
            'icon'        => $this->icon,
311 24
            'active'      => $this->isActive(),
312 24
            'children'    => $this->children->toArray(),
313 24
            'roles'       => $this->roles,
314 24
            'permissions' => $this->permissions,
315
        ];
316
    }
317
318
    /**
319
     * Convert the object into something JSON serializable.
320
     *
321
     * @return array
322
     */
323 8
    public function jsonSerialize()
324
    {
325 8
        return $this->toArray();
326
    }
327
328
    /**
329
     * Get the collection of items as JSON.
330
     *
331
     * @param  int  $options
332
     *
333
     * @return string
334
     */
335 4
    public function toJson($options = 0)
336
    {
337 4
        return json_encode($this->jsonSerialize(), $options);
338
    }
339
340
    /**
341
     * Check if has roles.
342
     *
343
     * @return bool
344
     */
345 8
    public function hasRoles()
346
    {
347 8
        return ! empty($this->roles);
348
    }
349
350
    /**
351
     * Check if has permissions.
352
     *
353
     * @return bool
354
     */
355 8
    public function hasPermissions()
356
    {
357 8
        return ! empty($this->permissions);
358
    }
359
}
360