Passed
Push — master ( 73cd93...7a270d )
by Alexander
30:15 queued 27:55
created

NavBar::optionsToggle()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

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
ccs 3
cts 3
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\Bulma;
6
7
use Yiisoft\Arrays\ArrayHelper;
8
use Yiisoft\Html\Html;
9
10
final class NavBar extends Widget
11
{
12
    private string $brand = '';
13
    private string $brandLabel = '';
14
    private string $brandImage = '';
15
    private string $brandUrl = '/';
16
    private array $options = [];
17
    private array $optionsBrand = [];
18
    private array $optionsBrandLabel = [];
19
    private array $optionsBrandImage = [];
20
    private array $optionsItems = [];
21
    private array $optionsMenu = [];
22
    private array $optionsToggle = [
23
        'aria-expanded' => 'false',
24
        'aria-label' => 'menu',
25
        'class' => 'navbar-burger',
26
        'role' => 'button'
27
    ];
28
29 11
    public function start(): string
30
    {
31 11
        $id = '';
32
33 11
        if (!isset($this->options['id'])) {
34 11
            $id = $this->getId();
35 11
            $this->options['id'] = "{$id}-navbar";
36
        }
37
38 11
        $this->options = $this->addOptions($this->options, 'navbar');
39 11
        $this->optionsBrand = $this->addOptions($this->optionsBrand, 'navbar-brand');
40 11
        $this->optionsBrandLabel = $this->addOptions($this->optionsBrandLabel, 'navbar-item');
41 11
        $this->optionsBrandImage = $this->addOptions($this->optionsBrandImage, 'navbar-item');
42 11
        $this->optionsMenu = $this->addOptions($this->optionsMenu, 'navbar-menu');
43
44 11
        $this->initOptionsItems();
45 11
        $this->renderBrand();
46
47 11
        $navOptions = $this->options;
48 11
        $navTag = ArrayHelper::remove($navOptions, 'tag', 'nav');
49
50 11
        $this->optionsMenu['id'] = "{$id}-navbar-Menu";
51
52
        return
53 11
            Html::beginTag($navTag, $navOptions) . "\n" .
54 11
            $this->brand . "\n" .
55 11
            Html::beginTag('div', $this->optionsMenu) .
56 11
            Html::beginTag('div', $this->optionsItems);
57
    }
58
59 11
    protected function run(): string
60
    {
61 11
        $tag = ArrayHelper::remove($this->options, 'tag', 'nav');
62
63
        return
64 11
            Html::endTag('div') . "\n" .
65 11
            Html::endTag('div') . "\n" .
66 11
            Html::endTag($tag);
67
    }
68
69
    /**
70
     * The text of the brand label or empty if it's not used. Note that this is not HTML-encoded.
71
     *
72
     * @param string $value
73
     *
74
     * @return self
75
     */
76 10
    public function brandLabel(string $value): self
77
    {
78 10
        $this->brandLabel = $value;
79 10
        return $this;
80
    }
81
82
    /**
83
     * The image of the brand or empty if it's not used.
84
     *
85
     * @param string $value
86
     *
87
     * @return self
88
     */
89 9
    public function brandImage(string $value): self
90
    {
91 9
        $this->brandImage = $value;
92 9
        return $this;
93
    }
94
95
    /**
96
     * The URL for the brand's hyperlink tag and will be used for the "href" attribute of the brand link. Default value
97
     * is '/' will be used. You may set it to `null` if you want to have no link at all.
98
     *
99
     * @param string $value
100
     *
101
     * @return self
102
     */
103 8
    public function brandUrl(string $value): self
104
    {
105 8
        $this->brandUrl = $value;
106 8
        return $this;
107
    }
108
109
    /**
110
     * Options HTML attributes for the tag nav.
111
     *
112
     * @param array $value
113
     *
114
     * @return self
115
     *
116
     * {@see \Yiisoft\Html\Html::renderTagAttributes()} for details on how attributes are being rendered.
117
     */
118 1
    public function options(array $value): self
119
    {
120 1
        $this->options = $value;
121 1
        return $this;
122
    }
123
124
    /**
125
     * Options HTML attributes of the tag div brand.
126
     *
127
     * @param array $value default value `navbar-item`.
128
     *
129
     * @return self
130
     *
131
     * {@see \Yiisoft\Html\Html::renderTagAttributes()} for details on how attributes are being rendered.
132
     */
133 1
    public function optionsBrand(array $value): self
134
    {
135 1
        $this->optionsBrand = $value;
136 1
        return $this;
137
    }
138
139
    /**
140
     * Options HTML attributes of the tag div brand label.
141
     *
142
     * @param array $value default value `navbar-item`.
143
     *
144
     * @return self
145
     *
146
     * {@see \Yiisoft\Html\Html::renderTagAttributes()} for details on how attributes are being rendered.
147
     */
148 1
    public function optionsBrandLabel(array $value): self
149
    {
150 1
        $this->optionsBrandLabel = $value;
151 1
        return $this;
152
    }
153
154
    /**
155
     * Options HTML attributes of the tag div brand link.
156
     *
157
     * @param array $value default value `navbar-item`.
158
     *
159
     * @return self
160
     *
161
     * {@see \Yiisoft\Html\Html::renderTagAttributes()} for details on how attributes are being rendered.
162
     */
163 1
    public function optionsBrandImage(array $value): self
164
    {
165 1
        $this->optionsBrandImage = $value;
166 1
        return $this;
167
    }
168
169
    /**
170
     * Options HTML attributes of the tag div items nav, values `navbar-start`, `navbar-end`.
171
     *
172
     * @param array $value default value `navbar-start`.
173
     *
174
     * @return self
175
     *
176
     * {@see \Yiisoft\Html\Html::renderTagAttributes()} for details on how attributes are being rendered.
177
     */
178 1
    public function optionsItems(array $value): self
179
    {
180 1
        $this->optionsItems = $value;
181 1
        return $this;
182
    }
183
184
    /**
185
     * Options HTML attributes of the tag div nav menu.
186
     *
187
     * @param array $value default value `navbar-menu`.
188
     *
189
     * @return self
190
     *
191
     * {@see \Yiisoft\Html\Html::renderTagAttributes()} for details on how attributes are being rendered.
192
     */
193 1
    public function optionsMenu(array $value): self
194
    {
195 1
        $this->optionsMenu = $value;
196 1
        return $this;
197
    }
198
199
    /**
200
     * The HTML attributes of the navbar toggler button.
201
     *
202
     * @param array $value
203
     *
204
     * @return self
205
     *
206
     * {@see \Yiisoft\Html\Html::renderTagAttributes()} for details on how attributes are being rendered.
207
     */
208 1
    public function optionsToggle(array $value): self
209
    {
210 1
        $this->optionsToggle = $value;
211 1
        return $this;
212
    }
213
214 11
    private function initOptionsItems(): void
215
    {
216 11
        $optionsItems = '';
217
218 11
        if (isset($this->optionsItems['class'])) {
219 1
            $optionsItems = $this->optionsItems['class'];
220
221 1
            unset($this->optionsItems['class']);
222
        }
223
224 11
        if (!strstr($optionsItems, 'navbar-end')) {
225 11
            Html::addCssClass($this->optionsItems, 'navbar-start');
226
        }
227
228 11
        if (!empty($optionsItems)) {
229 1
            Html::addCssClass($this->optionsItems, $optionsItems);
230
        }
231 11
    }
232
233 11
    private function renderBrand(): void
234
    {
235 11
        $this->brand = Html::beginTag('div', $this->optionsBrand);
236
237 11
        if ($this->brandImage !== '' && $this->brandLabel !== '') {
238 9
            $this->brand .= Html::tag('span', Html::img($this->brandImage), $this->optionsBrandImage);
239
        }
240
241 11
        if ($this->brandImage !== '' && $this->brandLabel === '') {
242 1
            $this->brand .= Html::a(Html::img($this->brandImage), $this->brandUrl, $this->optionsBrandImage);
243
        }
244
245 11
        if ($this->brandLabel !== '') {
246 10
            $this->brand .= Html::a($this->brandLabel, $this->brandUrl, $this->optionsBrandLabel);
247
        }
248
249 11
        $this->brand .= $this->renderToggleButton();
250 11
        $this->brand .= Html::endTag('div');
251 11
    }
252
253
    /**
254
     * Renders collapsible toggle button.
255
     *
256
     * @return string the rendering toggle button.
257
     */
258 11
    private function renderToggleButton(): string
259
    {
260
        return
261 11
            Html::beginTag('a', $this->optionsToggle) .
262 11
                Html::tag('span', '', ['aria-hidden' => 'true']) .
263 11
                Html::tag('span', '', ['aria-hidden' => 'true']) .
264 11
                Html::tag('span', '', ['aria-hidden' => 'true']) .
265 11
            Html::endTag('a');
266
    }
267
}
268