Passed
Push — master ( 34e66c...44dbec )
by Sergei
03:09
created

CloseButtonTrait::renderCloseButton()   B

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
20
    abstract protected function toggleComponent(): string;
21
22
    abstract public function getId(): ?string;
23
24
    /**
25
     * The HTML attributes for the widget close button tag. The following special options are recognized.
26
     *
27
     * {@see Html::renderTagAttributes()} for details on how attributes are being rendered.
28
     */
29 6
    public function withCloseButtonOptions(array $options): static
30
    {
31 6
        $new = clone $this;
32 6
        $new->closeButtonOptions = $options;
33
34 6
        return $new;
35
    }
36
37
    /**
38
     * Disable close button.
39
     */
40 12
    public function withoutCloseButton(): static
41
    {
42 12
        $new = clone $this;
43 12
        $new->showCloseButton = false;
44
45 12
        return $new;
46
    }
47
48 1
    public function withCloseButton(): static
49
    {
50 1
        $new = clone $this;
51 1
        $new->showCloseButton = true;
52
53 1
        return $new;
54
    }
55
56 3
    public function withCloseButtonLabel(string|Stringable $label): static
57
    {
58 3
        $new = clone $this;
59 3
        $new->closeButtonLabel = $label;
60
61 3
        return $new;
62
    }
63
64 1
    public function withEncodeCloseButton(bool $encode): static
65
    {
66 1
        $new = clone $this;
67 1
        $new->encodeCloseButton = $encode;
68
69 1
        return $new;
70
    }
71
72 70
    public function renderCloseButton(bool $inside = false): ?Tag
73
    {
74 70
        if ($inside && $this->showCloseButton === false) {
75 12
            return null;
76
        }
77
78 59
        $options = $this->closeButtonOptions;
79 59
        $tagName = ArrayHelper::remove($options, 'tag', 'button');
80
81 59
        Html::addCssClass($options, ['widget' => 'btn-close']);
82
83 59
        $label = (string) $this->closeButtonLabel;
84 59
        $options['data-bs-dismiss'] = $this->toggleComponent();
85
86 59
        if (!$inside) {
87 1
            $options['data-bs-target'] = '#' . $this->getId();
88
        }
89
90 59
        if (empty($label) && !isset($options['aria-label']) && !isset($options['aria']['label'])) {
91 57
            $options['aria-label'] = 'Close';
92
        }
93
94 59
        if ($tagName !== 'button') {
95 1
            $options['role'] = 'button';
96 59
        } elseif (!isset($options['type'])) {
97 59
            $options['type'] = 'button';
98
        }
99
100 59
        return Html::tag($tagName, $label, $options)
101 59
            ->encode($this->encodeCloseButton);
102
    }
103
}
104