Passed
Push — master ( 61d1a6...f87603 )
by Alexander
04:12 queued 02:11
created

NavBar::attributes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\Bulma;
6
7
use Yiisoft\Html\Html;
8
use Yiisoft\Html\Tag\A;
9
use Yiisoft\Html\Tag\Div;
10
use Yiisoft\Html\Tag\Img;
11
use Yiisoft\Html\Tag\Span;
12
use Yiisoft\Widget\Widget;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Yiisoft\Yii\Bulma\Widget. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
13
14
/**
15
 * NavBar renders a navbar HTML component.
16
 *
17
 * Any content enclosed between the {@see begin()} and {@see end()} calls of NavBar is treated as the content of the
18
 * navbar. You may use widgets such as {@see Nav} to build up such content. For example,
19
 *
20
 * @link https://bulma.io/documentation/components/navbar/
21
 */
22
final class NavBar extends Widget
23
{
24
    private array $attributes = [];
25
    private string $autoIdPrefix = 'w';
26
    private array $brandAttributes = [];
27
    private string $brandImage = '';
28
    private array $brandImageAttributes = [];
29
    private string $brandText = '';
30
    private array $brandTextAttributes = [];
31
    private string $brandUrl = '/';
32
    private string $buttonLinkAriaExpanded = 'false';
33
    private string $buttonLinkAriaLabelText = 'menu';
34
    private string $buttonLinkContent = '';
35
    private string $buttonLinkRole = 'button';
36
    private string $navBarAriaLabel = 'main navigation';
37
    private string $navBarBrandCssClass = 'navbar-brand';
38
    private array $navBarBurgerAttributes = [];
39
    private string $navBarBurgerCssClass = 'navbar-burger';
40
    private string $navBarCssClass = 'navbar';
41
    private string $navBarItemCssClass = 'navbar-item';
42
    private string $navBarRole = 'navigation';
43
44 18
    public function begin(): string
45
    {
46 18
        parent::begin();
47 18
        return $this->renderNavBar();
48
    }
49
50
    /**
51
     * Returns a new instance with the specified attributes.
52
     *
53
     * @param array $value The HTML attributes for the widget container nav tag.
54
     *
55
     * @return self
56
     *
57
     * {@see Html::renderTagAttributes()} for details on how attributes are being rendered.
58
     */
59 1
    public function attributes(array $value): self
60
    {
61 1
        $new = clone $this;
62 1
        $new->attributes = $value;
63 1
        return $new;
64
    }
65
66
    /**
67
     * Returns a new instance with the specified prefix to the automatically generated widget IDs.
68
     *
69
     * @param string $value The prefix to the automatically generated widget IDs.
70
     *
71
     * @return self
72
     */
73 1
    public function autoIdPrefix(string $value): self
74
    {
75 1
        $new = clone $this;
76 1
        $new->autoIdPrefix = $value;
77 1
        return $new;
78
    }
79
80
    /**
81
     * The HTML attributes of the brand.
82
     *
83
     * {@see Html::renderTagAttributes()} for details on how attributes are being rendered.
84
     *
85
     * @param array $value
86
     *
87
     * @return self
88
     */
89 2
    public function brandAttributes(array $value): self
90
    {
91 2
        $new = clone $this;
92 2
        $new->brandAttributes = $value;
93 2
        return $new;
94
    }
95
96
    /**
97
     * Src of the brand image or empty if it's not used. Note that this param will override `$this->brandText` param.
98
     *
99
     * @param string $value
100
     *
101
     * @return self
102
     */
103 3
    public function brandImage(string $value): self
104
    {
105 3
        $new = clone $this;
106 3
        $new->brandImage = $value;
107 3
        return $new;
108
    }
109
110
    /**
111
     * The HTML attributes of the brand image.
112
     *
113
     * {@see Html::renderTagAttributes()} for details on how attributes are being rendered.
114
     *
115
     * @param array $value
116
     *
117
     * @return self
118
     */
119 3
    public function brandImageAttributes(array $value): self
120
    {
121 3
        $new = clone $this;
122 3
        $new->brandImageAttributes = $value;
123 3
        return $new;
124
    }
125
126
    /**
127
     * The text of the brand or empty if it's not used. Note that this is not HTML-encoded.
128
     *
129
     * @param string $value
130
     *
131
     * @return self
132
     */
133 5
    public function brandText(string $value): self
134
    {
135 5
        $new = clone $this;
136 5
        $new->brandText = $value;
137 5
        return $new;
138
    }
139
140
    /**
141
     * The HTML attributes of the brand text.
142
     *
143
     * {@see Html::renderTagAttributes()} for details on how attributes are being rendered.
144
     *
145
     * @param array $value
146
     *
147
     * @return self
148
     */
149 2
    public function brandTextAttributes(array $value): self
150
    {
151 2
        $new = clone $this;
152 2
        $new->brandTextAttributes = $value;
153 2
        return $new;
154
    }
155
156
    /**
157
     * The URL for the brand's hyperlink tag and will be used for the "href" attribute of the brand link. Default value
158
     * is "/". You may set it to empty string if you want no link at all.
159
     *
160
     * @param string $value
161
     *
162
     * @return self
163
     */
164 4
    public function brandUrl(string $value): self
165
    {
166 4
        $new = clone $this;
167 4
        $new->brandUrl = $value;
168 4
        return $new;
169
    }
170
171
    /**
172
     * The ARIA expanded attribute of the button link.
173
     *
174
     * @param string $value
175
     *
176
     * @return self
177
     */
178 2
    public function buttonLinkAriaExpanded(string $value): self
179
    {
180 2
        $new = clone $this;
181 2
        $new->buttonLinkAriaExpanded = $value;
182 2
        return $new;
183
    }
184
185
    /**
186
     * The ARIA label text of the button link.
187
     *
188
     * @param string $value
189
     *
190
     * @return self
191
     */
192 2
    public function buttonLinkAriaLabelText(string $value): self
193
    {
194 2
        $new = clone $this;
195 2
        $new->buttonLinkAriaLabelText = $value;
196 2
        return $new;
197
    }
198
199
    /**
200
     * The content of the button link.
201
     *
202
     * @param string $value
203
     *
204
     * @return self
205
     */
206 2
    public function buttonLinkContent(string $value): self
207
    {
208 2
        $new = clone $this;
209 2
        $new->buttonLinkContent = $value;
210 2
        return $new;
211
    }
212
213
    /**
214
     * The role of the button link.
215
     *
216
     * @param string $value
217
     *
218
     * @return self
219
     */
220 2
    public function buttonLinkRole(string $value): self
221
    {
222 2
        $new = clone $this;
223 2
        $new->buttonLinkRole = $value;
224 2
        return $new;
225
    }
226
227
    /**
228
     * Returns a new instance with the specified ID of the widget.
229
     *
230
     * @param string $value The ID of the widget.
231
     *
232
     * @return self
233
     */
234 2
    public function id(string $value): self
235
    {
236 2
        $new = clone $this;
237 2
        $new->attributes['id'] = $value;
238 2
        return $new;
239
    }
240
241
    /**
242
     * The ARIA label of the navbar.
243
     *
244
     * @param string $value
245
     *
246
     * @return self
247
     */
248 2
    public function navBarAriaLabel(string $value): self
249
    {
250 2
        $new = clone $this;
251 2
        $new->navBarAriaLabel = $value;
252 2
        return $new;
253
    }
254
255
    /**
256
     * The CSS class of the brand.
257
     *
258
     * @param string $value
259
     *
260
     * @return self
261
     */
262 2
    public function navBarBrandCssClass(string $value): self
263
    {
264 2
        $new = clone $this;
265 2
        $new->navBarBrandCssClass = $value;
266 2
        return $new;
267
    }
268
269
    /**
270
     * The HTML attributes of the burger.
271
     *
272
     * {@see Html::renderTagAttributes()} for details on how attributes are being rendered.
273
     *
274
     * @param array $value
275
     *
276
     * @return self
277
     */
278 2
    public function navBarBurgerAttributes(array $value): self
279
    {
280 2
        $new = clone $this;
281 2
        $new->navBarBurgerAttributes = $value;
282 2
        return $new;
283
    }
284
285
    /**
286
     * The CSS class of the burger.
287
     *
288
     * @param string $value
289
     *
290
     * @return self
291
     */
292 2
    public function navBarBurgerCssClass(string $value): self
293
    {
294 2
        $new = clone $this;
295 2
        $new->navBarBurgerCssClass = $value;
296 2
        return $new;
297
    }
298
299
    /**
300
     * The CSS class of the navbar.
301
     *
302
     * @param string $value
303
     *
304
     * @return self
305
     */
306 2
    public function navBarCssClass(string $value): self
307
    {
308 2
        $new = clone $this;
309 2
        $new->navBarCssClass = $value;
310 2
        return $new;
311
    }
312
313
    /**
314
     * The CSS class of the items navbar.
315
     *
316
     * @param string $value
317
     *
318
     * @return self
319
     */
320 2
    public function navBarItemCssClass(string $value): self
321
    {
322 2
        $new = clone $this;
323 2
        $new->navBarItemCssClass = $value;
324 2
        return $new;
325
    }
326
327
    /**
328
     * The role of the navbar.
329
     *
330
     * @param string $value
331
     *
332
     * @return self
333
     */
334 2
    public function navBarRole(string $value): self
335
    {
336 2
        $new = clone $this;
337 2
        $new->navBarRole = $value;
338 2
        return $new;
339
    }
340
341 18
    protected function run(): string
342
    {
343 18
        return Html::closeTag('nav');
344
    }
345
346 18
    private function renderNavBar(): string
347
    {
348 18
        $attributes = $this->attributes;
349 18
        Html::addCssClass($attributes, $this->navBarCssClass);
350
351 18
        if (!isset($attributes['id'])) {
352 17
            $attributes['id'] = Html::generateId($this->autoIdPrefix) . '-navbar';
353
        }
354
355 18
        $attributes['aria-label'] = $this->navBarAriaLabel;
356 18
        $attributes['role'] = $this->navBarRole;
357
358 18
        return Html::openTag('nav', $attributes) . PHP_EOL . $this->renderNavBarBrand() . PHP_EOL;
359
    }
360
361 18
    private function renderNavBarBrand(): string
362
    {
363 18
        $brand = '';
364 18
        $brandImage = '';
365
366 18
        if ($this->brandImage !== '') {
367 2
            $brandImage = Img::tag()->attributes($this->brandImageAttributes)->url($this->brandImage)->render();
368 2
            $brand = PHP_EOL . A::tag()
369 2
                ->class($this->navBarItemCssClass)
370 2
                ->content($brandImage)
371 2
                ->encode(false)
372 2
                ->url($this->brandUrl)
373 2
                ->render();
374
        }
375
376 18
        if ($this->brandText !== '') {
377 4
            $brandText = $this->brandText;
378
379 4
            if ($brandImage !== '') {
380 1
                $brandText = $brandImage . $this->brandText;
381
            }
382
383 4
            if (empty($this->brandUrl)) {
384 1
                $brand = PHP_EOL . Span::tag()
385 1
                    ->attributes($this->brandTextAttributes)
386 1
                    ->class($this->navBarItemCssClass)
387 1
                    ->content($brandText)
388 1
                    ->render();
389
            } else {
390 3
                $brand = PHP_EOL . A::tag()
391 3
                    ->class($this->navBarItemCssClass)
392 3
                    ->content($brandText)
393 3
                    ->encode(false)
394 3
                    ->url($this->brandUrl)
395 3
                    ->render();
396
            }
397
        }
398
399 18
        $brand .= $this->renderNavBarBurger();
400
401 18
        return Div::tag()
402 18
            ->attributes($this->brandAttributes)
403 18
            ->class($this->navBarBrandCssClass)
404 18
            ->content($brand)
405 18
            ->encode(false)
406 18
            ->render();
407
    }
408
409
    /**
410
     * Renders collapsible toggle button.
411
     *
412
     * @return string the rendering navbar burger link button.
413
     *
414
     * @link https://bulma.io/documentation/components/navbar/#navbar-burger
415
     */
416 18
    private function renderNavBarBurger(): string
417
    {
418 18
        $navBarBurgerAttributes = $this->navBarBurgerAttributes;
419 18
        if ($this->buttonLinkContent === '') {
420 17
            $this->buttonLinkContent = PHP_EOL .
421 17
                Span::tag()->attributes(['aria-hidden' => 'true'])->render() . PHP_EOL .
422 17
                Span::tag()->attributes(['aria-hidden' => 'true'])->render() . PHP_EOL .
423 17
                Span::tag()->attributes(['aria-hidden' => 'true'])->render() . PHP_EOL;
424
        }
425
426 18
        $navBarBurgerAttributes['aria-expanded'] = $this->buttonLinkAriaExpanded;
427 18
        $navBarBurgerAttributes['aria-label'] = $this->buttonLinkAriaLabelText;
428 18
        $navBarBurgerAttributes['role'] = $this->buttonLinkRole;
429
430 18
        return PHP_EOL . A::tag()
431 18
            ->attributes($navBarBurgerAttributes)
432 18
            ->class($this->navBarBurgerCssClass)
433 18
            ->content($this->buttonLinkContent)
434 18
            ->encode(false)
435 18
            ->render() . PHP_EOL;
436
    }
437
}
438