Passed
Pull Request — master (#130)
by
unknown
14:32
created

CloseButtonTrait   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Test Coverage

Coverage 81.82%

Importance

Changes 0
Metric Value
eloc 43
dl 0
loc 102
ccs 36
cts 44
cp 0.8182
rs 10
c 0
b 0
f 0
wmc 17

6 Methods

Rating   Name   Duplication   Size   Complexity  
A withCloseButtonLabel() 0 6 1
B renderCloseButton() 0 30 9
A withEncodeCloseButton() 0 10 2
A withCloseButton() 0 10 2
A withCloseButtonOptions() 0 6 1
A withoutCloseButton() 0 10 2
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 $insideCloseButton = 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
        if ($this->insideCloseButton === false) {
43
            return $this;
44
        }
45
46 12
        $new = clone $this;
47 12
        $new->insideCloseButton = false;
48
49 12
        return $new;
50
    }
51
52
    public function withCloseButton(): static
53
    {
54
        if ($this->insideCloseButton === true) {
55
            return $this;
56
        }
57
58
        $new = clone $this;
59
        $new->insideCloseButton = true;
60
61
        return $new;
62
    }
63
64 3
    public function withCloseButtonLabel(string|Stringable $label): static
65
    {
66 3
        $new = clone $this;
67 3
        $new->closeButtonLabel = $label;
68
69 3
        return $new;
70
    }
71
72 1
    public function withEncodeCloseButton(bool $encode): static
73
    {
74 1
        if ($this->encodeCloseButton === $encode) {
75
            return $this;
76
        }
77
78 1
        $new = clone $this;
79 1
        $new->encodeCloseButton = $encode;
80
81 1
        return $new;
82
    }
83
84 70
    public function renderCloseButton(bool $inside = false): ?Tag
85
    {
86 70
        if ($inside && $this->insideCloseButton === false) {
87 12
            return null;
88
        }
89
90 59
        $options = $this->closeButtonOptions;
91 59
        $tagName = ArrayHelper::remove($options, 'tag', 'button');
92
93 59
        Html::addCssClass($options, ['widget' => 'btn-close']);
94
95 59
        $label = (string) $this->closeButtonLabel;
96 59
        $options['data-bs-dismiss'] = $this->toggleComponent();
97
98 59
        if (!$inside) {
99 1
            $options['data-bs-target'] = '#' . $this->getId();
100
        }
101
102 59
        if (empty($label) && !isset($options['aria-label']) && !isset($options['aria']['label'])) {
103 57
            $options['aria-label'] = 'Close';
104
        }
105
106 59
        if ($tagName !== 'button') {
107 1
            $options['role'] = 'button';
108 59
        } elseif (!isset($options['type'])) {
109 59
            $options['type'] = 'button';
110
        }
111
112 59
        return Html::tag($tagName, $label, $options)
113 59
            ->encode($this->encodeCloseButton);
114
    }
115
}
116