Test Failed
Push — main ( 74d919...9fe5ba )
by Michael
09:05 queued 06:57
created

MenuItemTrait::setLabel()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 4
rs 10
1
<?php
2
3
4
namespace Braunstetter\MenuBundle\Items;
5
6
7
use Symfony\Component\String\UnicodeString;
8
use Traversable;
9
use function count;
10
11
trait MenuItemTrait
12
{
13
    private string $type;
14
    private string $label;
15
    private ?string $icon;
16
    private ?string $routeName;
17
    private ?array $routeParameters;
18
    private array|Traversable $children;
19
    private bool $current;
20
    private bool $inActiveTrail;
21
    public string $handle;
22
23
    public function __construct(string $label, string $routeName, array $routeParameters, ?string $icon)
24
    {
25
        $this->children = [];
26
        $this->current = false;
27
        $this->inActiveTrail = false;
28
29
        $this->setLabel($label);
30
        $this->setIcon($icon);
31
        $this->setRouteName($routeName);
32
        $this->setRouteParameters($routeParameters);
33
34
        $this->handle = (new UnicodeString($this->label))->snake()->toString();
35
    }
36
37
    public function getType(): string
38
    {
39
        return $this->type;
40
    }
41
42
    public function setType(string $type): static
43
    {
44
        $this->type = $type;
45
        return $this;
46
    }
47
48
    public function getLabel(): string
49
    {
50
        return $this->label;
51
    }
52
53
    public function setLabel(string $label): static
54
    {
55
        $this->label = $label;
56
        return $this;
57
    }
58
59
    public function getIcon(): ?string
60
    {
61
        return $this->icon;
62
    }
63
64
    public function setIcon(?string $icon): void
65
    {
66
        $this->icon = $icon;
67
    }
68
69
    public function getRouteName(): ?string
70
    {
71
        return $this->routeName;
72
    }
73
74
    public function setRouteName(?string $routeName): static
75
    {
76
        $this->routeName = $routeName;
77
        return $this;
78
    }
79
80
    public function getRouteParameters(): ?array
81
    {
82
        return $this->routeParameters;
83
    }
84
85
    public function setRouteParameter(string $parameterName, $parameterValue): static
86
    {
87
        $this->routeParameters[$parameterName] = $parameterValue;
88
        return $this;
89
    }
90
91
    public function setRouteParameters(?array $routeParameters): static
92
    {
93
        $this->routeParameters = $routeParameters;
94
        return $this;
95
    }
96
97
    /**
98
     * @return array|Traversable
99
     */
100
    public function getChildren(): array|Traversable
101
    {
102
        if (!is_array($this->children)) {
103
            $this->children = iterator_to_array($this->children, false);
104
        }
105
106
        return $this->children;
107
    }
108
109
    public function setChildren(callable $children): static
110
    {
111
        $this->children = call_user_func($children);
112
        return $this;
113
    }
114
115
    public function hasChildren(): bool
116
    {
117
        if ($this->children instanceof Traversable) {
118
            $this->children = iterator_to_array($this->children);
119
        }
120
121
        return !empty($this->children);
122
    }
123
124
    /**
125
     * @return bool
126
     */
127
    public function isCurrent(): bool
128
    {
129
        return $this->current;
130
    }
131
132
    /**
133
     * @param bool $current
134
     */
135
    public function setCurrent(bool $current): void
136
    {
137
        $this->current = $current;
138
    }
139
140
    /**
141
     * @return bool
142
     */
143
    public function isInActiveTrail(): bool
144
    {
145
        return $this->inActiveTrail;
146
    }
147
148
    /**
149
     * @param bool $inActiveTrail
150
     */
151
    public function setInActiveTrail(bool $inActiveTrail): void
152
    {
153
        $this->inActiveTrail = $inActiveTrail;
154
    }
155
156
}