Passed
Pull Request — master (#57)
by Kyle
07:54
created

MenuItemContract   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 194
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 36
dl 0
loc 194
rs 10
c 1
b 0
f 0
wmc 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __call() 0 6 2
A toArray() 0 3 1
A make() 0 5 1
A __get() 0 8 2
A setIconAttribute() 0 12 2
A add() 0 7 1
A getRandomName() 0 3 1
1
<?php
2
3
namespace KyleMassacre\Menus\Contracts;
4
5
use Illuminate\Contracts\Support\Arrayable as ArrayableContract;
6
use Illuminate\Support\Arr;
7
use Illuminate\Support\Str;
8
9
abstract class MenuItemContract implements ArrayableContract
10
{
11
    /**
12
     * Array properties.
13
     *
14
     * @var array
15
     */
16
    protected array $properties = [];
17
18
    /**
19
     * The child collections for current menu item.
20
     *
21
     * @var array
22
     */
23
    protected array $childs = [];
24
25
    /**
26
     * The fillable attribute.
27
     *
28
     * @var array
29
     */
30
    protected array $fillable = [
31
        'url',
32
        'route',
33
        'title',
34
        'name',
35
        'icon',
36
        'parent',
37
        'attributes',
38
        'active',
39
        'order',
40
        'hideWhen',
41
        'badge',
42
        'class',
43
    ];
44
45
    abstract public function __construct(array $properties = []);
46
47
    /**
48
     * Set the icon property when the icon is defined in the link attributes.
49
     *
50
     * @param array $properties
51
     *
52
     * @return array
53
     */
54
    protected static function setIconAttribute(array $properties): array
55
    {
56
        $icon = Arr::get($properties, 'attributes.icon');
57
        if (!is_null($icon)) {
58
            $properties['icon'] = $icon;
59
60
            Arr::forget($properties, 'attributes.icon');
61
62
            return $properties;
63
        }
64
65
        return $properties;
66
    }
67
68
    /**
69
     * Get random name.
70
     *
71
     * @param array $attributes
72
     *
73
     * @return string
74
     */
75
    protected static function getRandomName(array $attributes): string
76
    {
77
        return substr(md5(Arr::get($attributes, 'title', Str::random(6))), 0, 5);
78
    }
79
80
    /**
81
     * Create new static instance.
82
     *
83
     * @param array $properties
84
     *
85
     * @return static
86
     */
87
    public static function make(array $properties): static
88
    {
89
        $properties = self::setIconAttribute($properties);
90
91
        return new static($properties);
92
    }
93
94
    abstract public function fill(array $attributes): void;
95
96
    abstract public function child(array $attributes): self;
97
98
    abstract public function dropdown(string $title, \Closure $callback, int $order = 0, array $attributes = []): static;
99
100
    abstract public function route(string $route, string $title, array $parameters = [], int $order = 0, array $attributes = []): static;
101
102
    abstract public function url(string $url, string $title, int $order = 0, array $attributes = []): static;
103
104
    /**
105
     * Add new child item.
106
     *
107
     * @param array $properties
108
     *
109
     * @return $this
110
     */
111
    public function add(array $properties): static
112
    {
113
        $item = static::make($properties);
114
115
        $this->childs[] = $item;
116
117
        return $item;
118
    }
119
120
    abstract public function addDivider($order = null): static;
121
122
    abstract public function divider($order = null): static;
123
124
    abstract public function addHeader(string $title): static;
125
126
    abstract public function header(string $title): static;
127
128
    abstract public function addBadge(string $type, string $text): static;
129
130
    abstract public function getChildren(): array;
131
132
    abstract public function getUrl(): string;
133
134
    abstract public function getRequest(): string;
135
136
    abstract public function getBadge(): string;
137
138
    abstract public function getIcon(null|string $default): ?string;
139
140
    abstract public function getProperties(): array;
141
142
    abstract public function getAttributes(): mixed;
143
144
    abstract public function is(string $name): bool;
145
146
    abstract public function hasSubMenu(): bool;
147
148
    abstract public function hasActiveOnChild(): mixed;
149
150
    abstract public function getActiveStateFromChildren(): bool;
151
152
    abstract public function inactive(): bool;
153
154
    abstract public function getActiveAttribute(): string;
155
156
    abstract public function getInactiveAttribute(): string;
157
158
    abstract public function isActive(): mixed;
159
160
    abstract protected function hasRoute(): mixed;
161
162
    abstract protected function getActiveStateFromRoute(): bool;
163
164
    abstract protected function getActiveStateFromUrl(): bool;
165
166
    abstract public function order(int $order): self;
167
168
    abstract public function hasBadge(): bool;
169
170
    /**
171
     * Get the instance as an array.
172
     *
173
     * @return array
174
     */
175
    public function toArray(): array
176
    {
177
        return $this->getProperties();
178
    }
179
180
    /**
181
     * Get property.
182
     *
183
     * @param string $key
184
     *
185
     * @return string|null
186
     */
187
    public function __get(string $key): ?string
188
    {
189
        $return = null;
190
        if(property_exists($this, $key)) {
191
            $return = $this->$key ?? null;
192
        }
193
194
        return $return;
195
    }
196
197
    public function __call($name, $arguments)
198
    {
199
        if(str($name)->is('is*')) {
200
            $slice = str($name)->after('is')->lower();
201
202
            return $this->is($slice);
203
        }
204
    }
205
}
206