Item::setLabel()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 1
1
<?php
2
3
/**
4
 * (c) FSi sp. z o.o. <[email protected]>
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
declare(strict_types=1);
11
12
namespace FSi\Bundle\AdminBundle\Menu\Item;
13
14
use FSi\Bundle\AdminBundle\Exception\MissingOptionException;
15
use Symfony\Component\OptionsResolver\Options;
16
use Symfony\Component\OptionsResolver\OptionsResolver;
17
18
class Item
19
{
20
    /**
21
     * @var string
22
     */
23
    private $name;
24
25
    /**
26
     * @var string|null
27
     */
28
    private $label;
29
30
    /**
31
     * @var Item[]
32
     */
33
    private $children;
34
35
    /**
36
     * @var array
37
     */
38
    private $options;
39
40
    public function __construct(?string $name = null)
41
    {
42
        $this->children = [];
43
        $this->name = $name;
44
45
        $this->setOptions([]);
46
    }
47
48
    public function getName(): string
49
    {
50
        return $this->name;
51
    }
52
53
    public function getLabel(): ?string
54
    {
55
        return $this->label;
56
    }
57
58
    public function setLabel(?string $label): void
59
    {
60
        $this->label = $label;
61
    }
62
63
    public function addChild(Item $item): void
64
    {
65
        $this->children[$item->getName()] = $item;
66
    }
67
68
    public function removeChild(string $name): void
69
    {
70
        if (isset($this->children[$name])) {
71
            unset($this->children[$name]);
72
        }
73
    }
74
75
    public function hasChildren(): bool
76
    {
77
        return (bool) count($this->children);
78
    }
79
80
    /**
81
     * @return Item[]
82
     */
83
    public function getChildren(): array
84
    {
85
        return $this->children;
86
    }
87
88
    public function __toString(): string
89
    {
90
        return $this->name;
91
    }
92
93
    public function setOptions(array $options): void
94
    {
95
        $optionsResolver = new OptionsResolver();
96
        $this->configureOptions($optionsResolver);
97
        $this->options = $optionsResolver->resolve($options);
98
    }
99
100
    public function hasOption(string $name): bool
101
    {
102
        return array_key_exists($name, $this->options);
103
    }
104
105
    /**
106
     * @param string $name
107
     * @return mixed
108
     */
109
    public function getOption(string $name)
110
    {
111
        if (!$this->hasOption($name)) {
112
            throw new MissingOptionException(sprintf('Option with name: "%s" does\'t exists.', $name));
113
        }
114
115
        return $this->options[$name];
116
    }
117
118
    public function getOptions(): array
119
    {
120
        return $this->options;
121
    }
122
123
    protected function configureOptions(OptionsResolver $optionsResolver): void
124
    {
125
        $optionsResolver->setDefaults([
126
            'attr' => [],
127
        ]);
128
129
        $optionsResolver->setAllowedTypes('attr', ['array']);
130
131
        $optionsResolver->setNormalizer('attr', function (Options $options, array $value) {
132
            $attrOptionsResolver = new OptionsResolver();
133
            $attrOptionsResolver->setDefaults([
134
                'id' => null,
135
                'class' => null,
136
            ]);
137
138
            $attrOptionsResolver->setAllowedTypes('id', ['null', 'string']);
139
            $attrOptionsResolver->setAllowedTypes('class', ['null', 'string']);
140
141
            return $attrOptionsResolver->resolve($value);
142
        });
143
    }
144
}
145