Completed
Push — master ( 868943...de56f4 )
by ARCANEDEV
02:45
created

Item::toArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 13
ccs 0
cts 13
cp 0
rs 9.4286
cc 1
eloc 10
nc 1
nop 0
crap 2
1
<?php namespace Arcanesoft\Core\Helpers\Sidebar\Entities;
2
3
use Illuminate\Contracts\Support\Arrayable;
4
5
/**
6
 * Class     Item
7
 *
8
 * @package  Arcanesoft\Core\Helpers\Sidebar\Entities
9
 * @author   ARCANEDEV <[email protected]>
10
 */
11
class Item implements Arrayable
12
{
13
    /* ------------------------------------------------------------------------------------------------
14
     |  Properties
15
     | ------------------------------------------------------------------------------------------------
16
     */
17
    /**
18
     * The item name.
19
     *
20
     * @var string
21
     */
22
    protected $name;
23
24
    /**
25
     * The item title.
26
     *
27
     * @var string
28
     */
29
    public $title;
30
31
    /**
32
     * The item url.
33
     *
34
     * @var string
35
     */
36
    public $url;
37
38
    /**
39
     * The item icon.
40
     *
41
     * @var string
42
     */
43
    public $icon;
44
45
    /**
46
     * The item active state.
47
     *
48
     * @var bool
49
     */
50
    public $active = false;
51
52
    /**
53
     * The item roles.
54
     *
55
     * @var array
56
     */
57
    protected $roles      = [];
58
59
    /**
60
     * The item permissions.
61
     *
62
     * @var array
63
     */
64
    protected $permissions = [];
65
66
    /**
67
     * The item children (sub-items).
68
     *
69
     * @var \Arcanesoft\Core\Helpers\Sidebar\Entities\ItemCollection
70
     */
71
    public $children;
72
73
    /* ------------------------------------------------------------------------------------------------
74
     |  Constructor
75
     | ------------------------------------------------------------------------------------------------
76
     */
77
    /**
78
     * Item constructor.
79
     *
80
     * @param  string       $name
81
     * @param  string       $title
82
     * @param  string       $url
83
     * @param  string|null  $icon
84
     */
85
    public function __construct($name, $title, $url, $icon = null)
86
    {
87
        $this->name     = $name;
88
        $this->title    = $title;
89
        $this->url      = $url;
90
        $this->icon     = $icon;
91
        $this->active   = false;
92
        $this->children = new ItemCollection;
93
    }
94
95
    /* ------------------------------------------------------------------------------------------------
96
     |  Getters & Setters
97
     | ------------------------------------------------------------------------------------------------
98
     */
99
    /**
100
     * Set the current name.
101
     *
102
     * @param  string  $currentName
103
     *
104
     * @return self
105
     */
106
    public function setCurrent($currentName)
107
    {
108
        $this->children->setCurrent($currentName);
109
        $this->active = ($this->name === $currentName || $this->children->hasActiveItem());
110
111
        return $this;
112
    }
113
114
    /**
115
     * Set the roles.
116
     *
117
     * @param  array  $roles
118
     *
119
     * @return self
120
     */
121
    public function setRoles(array $roles)
122
    {
123
        $this->roles = $roles;
124
125
        return $this;
126
    }
127
128
    /**
129
     * Set the permissions.
130
     *
131
     * @param  array  $permissions
132
     *
133
     * @return self
134
     */
135
    public function setPermissions(array $permissions)
136
    {
137
        $this->permissions = $permissions;
138
139
        return $this;
140
    }
141
142
    /**
143
     * Get the active class.
144
     *
145
     * @param  string  $class
146
     *
147
     * @return string
148
     */
149
    public function activeClass($class = 'active')
150
    {
151
        return $this->isActive() ? $class : '';
152
    }
153
154
    /**
155
     * Get the tree view class.
156
     *
157
     * @param  string  $class
158
     *
159
     * @return string
160
     */
161
    public function treeViewClass($class = 'treeview')
162
    {
163
        return $this->hasChildren() ? $class : '';
164
    }
165
166
    /* ------------------------------------------------------------------------------------------------
167
     |  Main Functions
168
     | ------------------------------------------------------------------------------------------------
169
     */
170
    /**
171
     * Make the item.
172
     *
173
     * @param  string       $name
174
     * @param  string       $title
175
     * @param  string       $url
176
     * @param  string|null  $icon
177
     *
178
     * @return self
179
     */
180
    public static function make($name, $title, $url, $icon = null)
181
    {
182
        return new self($name, $title, $url, $icon);
183
    }
184
185
    /**
186
     * Make a Sidebar item from array.
187
     *
188
     * @param  array  $array
189
     *
190
     * @return self
191
     */
192
    public static function makeFromArray(array $array)
193
    {
194
        $item = self::make(
195
            $array['name'],
196
            $array['title'],
197
            self::getUrlFromArray($array),
198
            array_get($array, 'icon', null)
199
        );
200
201
        $item->setRoles(array_get($array, 'roles', []));
202
        $item->setPermissions(array_get($array, 'permissions', []));
203
        $item->addChildren(array_get($array, 'children', []));
204
205
        return $item;
206
    }
207
208
    /**
209
     * Get url from array.
210
     *
211
     * @param  array  $array
212
     *
213
     * @return string
214
     */
215
    private static function getUrlFromArray(array $array)
216
    {
217
        if (array_has($array, 'route')) {
218
            return route(array_get($array, 'route'));
219
        }
220
221
        return array_get($array, 'url', '#');
222
    }
223
224
    /**
225
     * Add children to the parent.
226
     *
227
     * @param  array  $children
228
     *
229
     * @return self
230
     */
231
    private function addChildren(array $children)
232
    {
233
        foreach ($children as $child) {
234
            $item = self::makeFromArray($child);
235
236
            $this->children->push($item);
237
        }
238
239
        return $this;
240
    }
241
242
    /* ------------------------------------------------------------------------------------------------
243
     |  Check Functions
244
     | ------------------------------------------------------------------------------------------------
245
     */
246
    /**
247
     * Check if the item is active one.
248
     *
249
     * @return bool
250
     */
251
    public function isActive()
252
    {
253
        return $this->active;
254
    }
255
256
    /**
257
     * Check if the item has children.
258
     *
259
     * @return bool
260
     */
261
    public function hasChildren()
262
    {
263
        return ! $this->children->isEmpty();
264
    }
265
266
    /**
267
     * Get the instance as an array.
268
     *
269
     * @return array
270
     */
271
    public function toArray()
272
    {
273
        return [
274
            'name'        => $this->name,
275
            'title'       => $this->title,
276
            'url'         => $this->url,
277
            'icon'        => $this->icon,
278
            'active'      => $this->active,
279
            'roles'       => $this->roles,
280
            'permissions' => $this->permissions,
281
            'children'    => $this->children->toArray(),
282
        ];
283
    }
284
}
285