Passed
Push — main ( 8805e2...91509e )
by Michael
09:09
created

MenuItemTrait::getTarget()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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