CloseButtonTrait::renderCloseButton()   B
last analyzed

Complexity

Conditions 9
Paths 13

Size

Total Lines 30
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 9

Importance

Changes 0
Metric Value
cc 9
eloc 17
nc 13
nop 1
dl 0
loc 30
ccs 18
cts 18
cp 1
crap 9
rs 8.0555
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\Bootstrap5;
6
7
use Stringable;
8
use Yiisoft\Arrays\ArrayHelper;
9
use Yiisoft\Html\Html;
10
use Yiisoft\Html\Tag\Base\Tag;
11
12
trait CloseButtonTrait
13
{
14
    private array $closeButtonOptions = [];
15
    private string|Stringable $closeButtonLabel = '';
16
    private bool $encodeCloseButton = true;
17
    private bool $showCloseButton = true;
18
19
    abstract protected function toggleComponent(): string;
20
21
    abstract public function getId(): ?string;
22
23
    /**
24
     * The HTML attributes for the widget close button tag. The following special options are recognized.
25
     *
26
     * {@see Html::renderTagAttributes()} for details on how attributes are being rendered.
27
     */
28 6
    public function withCloseButtonOptions(array $options): static
29
    {
30 6
        $new = clone $this;
31 6
        $new->closeButtonOptions = $options;
32
33 6
        return $new;
34
    }
35
36
    /**
37
     * Disable close button.
38
     */
39 12
    public function withoutCloseButton(): static
40
    {
41 12
        $new = clone $this;
42 12
        $new->showCloseButton = false;
43
44 12
        return $new;
45
    }
46
47 1
    public function withCloseButton(): static
48
    {
49 1
        $new = clone $this;
50 1
        $new->showCloseButton = true;
51
52 1
        return $new;
53
    }
54
55 3
    public function withCloseButtonLabel(string|Stringable $label): static
56
    {
57 3
        $new = clone $this;
58 3
        $new->closeButtonLabel = $label;
59
60 3
        return $new;
61
    }
62
63 1
    public function withEncodeCloseButton(bool $encode): static
64
    {
65 1
        $new = clone $this;
66 1
        $new->encodeCloseButton = $encode;
67
68 1
        return $new;
69
    }
70
71 70
    public function renderCloseButton(bool $inside = false): ?Tag
72
    {
73 70
        if ($inside && $this->showCloseButton === false) {
74 12
            return null;
75
        }
76
77 59
        $options = $this->closeButtonOptions;
78 59
        $tagName = ArrayHelper::remove($options, 'tag', 'button');
79
80 59
        Html::addCssClass($options, ['widget' => 'btn-close']);
81
82 59
        $label = (string) $this->closeButtonLabel;
83 59
        $options['data-bs-dismiss'] = $this->toggleComponent();
84
85 59
        if (!$inside) {
86 1
            $options['data-bs-target'] = '#' . $this->getId();
87
        }
88
89 59
        if (empty($label) && !isset($options['aria-label']) && !isset($options['aria']['label'])) {
90 57
            $options['aria-label'] = 'Close';
91
        }
92
93 59
        if ($tagName !== 'button') {
94 1
            $options['role'] = 'button';
95 59
        } elseif (!isset($options['type'])) {
96 59
            $options['type'] = 'button';
97
        }
98
99 59
        return Html::tag($tagName, $label, $options)
100 59
            ->encode($this->encodeCloseButton);
101
    }
102
}
103