Passed
Push — main ( 82ddde...8805e2 )
by Michael
02:00
created

MenuItemTrait   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 169
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 58
dl 0
loc 169
rs 10
c 1
b 0
f 0
wmc 26

23 Methods

Rating   Name   Duplication   Size   Complexity  
A getUrl() 0 3 1
A getRouteParameters() 0 3 1
A setCurrent() 0 3 1
A getLabel() 0 3 1
A getRouteName() 0 3 1
A setLabel() 0 4 1
A setTarget() 0 4 1
A addClass() 0 5 1
A setInActiveTrail() 0 3 1
A setType() 0 4 1
A __construct() 0 14 1
A setRouteName() 0 4 1
A setIcon() 0 3 1
A getType() 0 3 1
A isCurrent() 0 3 1
A getIcon() 0 3 1
A setRouteParameter() 0 4 1
A getTarget() 0 3 2
A getChildren() 0 7 2
A setRouteParameters() 0 4 1
A hasChildren() 0 7 2
A setChildren() 0 4 1
A isInActiveTrail() 0 3 1
1
<?php
2
3
4
namespace Braunstetter\MenuBundle\Items;
5
6
use Symfony\Component\String\UnicodeString;
7
use Traversable;
8
9
trait MenuItemTrait
10
{
11
    private string $type;
12
    private string $label;
13
    private ?string $icon;
14
    private ?string $routeName;
15
    private ?array $routeParameters;
16
    private array|Traversable $children;
17
    private bool $current;
18
    private bool $inActiveTrail;
19
    public string $handle;
20
    public string $url;
21
    public string $target;
22
    public array $attr;
23
    public array $linkAttr;
24
25
    public function __construct(string $label, ?string $icon, ?array $options = [])
26
    {
27
        $this->children = [];
28
        $this->current = false;
29
        $this->inActiveTrail = false;
30
31
        $this->setLabel($label);
32
        $this->setIcon($icon);
33
        $this->target = $options['target'] ?? '_self';
34
35
        $this->attr = $options['attr'] ?? [];
36
        $this->linkAttr = $options['linkAttr'] ?? [];
37
38
        $this->handle = (new UnicodeString($this->label))->snake()->toString();
39
    }
40
41
    public function getType(): string
42
    {
43
        return $this->type;
44
    }
45
46
    public function setType(string $type): static
47
    {
48
        $this->type = $type;
49
        return $this;
50
    }
51
52
    public function getLabel(): string
53
    {
54
        return $this->label;
55
    }
56
57
    public function setLabel(string $label): static
58
    {
59
        $this->label = $label;
60
        return $this;
61
    }
62
63
    public function getIcon(): ?string
64
    {
65
        return $this->icon;
66
    }
67
68
    public function setIcon(?string $icon): void
69
    {
70
        $this->icon = $icon;
71
    }
72
73
    public function getRouteName(): ?string
74
    {
75
        return $this->routeName ?? null;
76
    }
77
78
    public function setRouteName(?string $routeName): static
79
    {
80
        $this->routeName = $routeName;
81
        return $this;
82
    }
83
84
    public function getRouteParameters(): ?array
85
    {
86
        return $this->routeParameters;
87
    }
88
89
    public function setRouteParameter(string $parameterName, $parameterValue): static
90
    {
91
        $this->routeParameters[$parameterName] = $parameterValue;
92
        return $this;
93
    }
94
95
    public function setRouteParameters(?array $routeParameters): static
96
    {
97
        $this->routeParameters = $routeParameters;
98
        return $this;
99
    }
100
101
    public function getChildren(): array|Traversable
102
    {
103
        if (!is_array($this->children)) {
104
            $this->children = iterator_to_array($this->children, false);
105
        }
106
107
        return $this->children;
108
    }
109
110
    public function setChildren(callable $children): static
111
    {
112
        $this->children = call_user_func($children);
113
        return $this;
114
    }
115
116
    public function hasChildren(): bool
117
    {
118
        if ($this->children instanceof Traversable) {
119
            $this->children = iterator_to_array($this->children);
120
        }
121
122
        return !empty($this->children);
123
    }
124
125
    /**
126
     * @return bool
127
     */
128
    public function isCurrent(): bool
129
    {
130
        return $this->current;
131
    }
132
133
    /**
134
     * @param bool $current
135
     */
136
    public function setCurrent(bool $current): void
137
    {
138
        $this->current = $current;
139
    }
140
141
    /**
142
     * @return bool
143
     */
144
    public function isInActiveTrail(): bool
145
    {
146
        return $this->inActiveTrail;
147
    }
148
149
    /**
150
     * @param bool $inActiveTrail
151
     */
152
    public function setInActiveTrail(bool $inActiveTrail): void
153
    {
154
        $this->inActiveTrail = $inActiveTrail;
155
    }
156
157
    public function getUrl(): string
158
    {
159
        return $this->url;
160
    }
161
162
    public function getTarget(): string
163
    {
164
        return $this->target ?: '_self';
165
    }
166
167
    public function setTarget(string $target): static
168
    {
169
        $this->target = $target;
170
        return $this;
171
    }
172
173
    public function addClass(string $class)
174
    {
175
        $this->attr = array_replace([
176
            'class' => implode(' ', [$class, $this->attr['class'] ?? ''])
177
        ], $this->attr);
178
    }
179
180
}