Test Failed
Pull Request — master (#35)
by Wilmer
02:34
created

Alert   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 177
Duplicated Lines 0 %

Test Coverage

Coverage 80.56%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 47
dl 0
loc 177
ccs 29
cts 36
cp 0.8056
rs 10
c 1
b 0
f 0
wmc 16

9 Methods

Rating   Name   Duplication   Size   Complexity  
A withCloseButtonDisabled() 0 6 1
A run() 0 11 2
A renderCloseButton() 0 14 4
A initOptions() 0 24 4
A renderBodyEnd() 0 3 1
A withEncodeTags() 0 6 1
A withOptions() 0 6 1
A withBody() 0 6 1
A withCloseButton() 0 6 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\Bootstrap5;
6
7
use JsonException;
8
use Yiisoft\Arrays\ArrayHelper;
9
use Yiisoft\Html\Html;
10
11
/**
12
 * Alert renders an alert bootstrap component.
13
 *
14
 * For example,
15
 *
16
 * ```php
17
 * echo Alert::widget()
18
 *     ->withOptions([
19
 *         'class' => 'alert-info',
20
 *     ])
21
 *     ->withBody('Say hello...');
22
 * ```
23
 */
24
final class Alert extends Widget
25
{
26
    private ?string $body = null;
27
    private array $closeButton = [];
28
    private bool $closeButtonEnabled = true;
29
    private bool $encodeTags = false;
30
    private array $options = [];
31 2
32
    protected function run(): string
33 2
    {
34 2
        if (!isset($this->options['id'])) {
35
            $this->options['id'] = "{$this->getId()}-alert";
36
        }
37 2
38
        $this->initOptions();
39 2
40
        $this->registerPlugin('alert', $this->options);
41 2
42
        return Html::div($this->renderBodyEnd(), $this->options);
43
    }
44
45
    /**
46
     * Renders the alert body and the close button (if any).
47
     *
48
     * @throws JsonException
49
     *
50
     * @return string the rendering result
51 2
     */
52
    protected function renderBodyEnd(): string
53 2
    {
54
        return $this->body . "\n" . $this->renderCloseButton() . "\n";
55
    }
56
57
    /**
58
     * Renders the close button.
59
     *
60
     * @throws JsonException
61
     *
62
     * @return string the rendering result
63 2
     */
64
    protected function renderCloseButton(): ?string
65 2
    {
66
        if ($this->closeButtonEnabled === false) {
67
            return null;
68
        }
69 2
70 2
        $tag = ArrayHelper::remove($this->closeButton, 'tag', 'button');
71
        $label = ArrayHelper::remove($this->closeButton, 'label', '');
72 2
73 2
        if ($tag === 'button' && !isset($this->closeButton['type'])) {
74
            $this->closeButton['type'] = 'button';
75
        }
76 2
77
        return Html::tag($tag, $label, $this->closeButton);
78
    }
79
80
    /**
81
     * Initializes the widget options.
82
     *
83
     * This method sets the default values for various options.
84 2
     */
85
    protected function initOptions(): void
86 2
    {
87
        Html::addCssClass($this->options, ['widget' => 'alert']);
88 2
89 2
        if ($this->closeButtonEnabled !== false) {
90
            $this->closeButton = array_merge(
91
                $this->closeButton,
92
                [
93
                    'aria-label' => 'Close',
94
                    'data-bs-dismiss' => 'alert',
95 2
                ],
96
            );
97
98 2
            Html::addCssclass($this->closeButton, 'btn-close');
99 2
            Html::addCssClass($this->options, ['alert-dismissible']);
100
        }
101 2
102
        if ($this->encodeTags === false) {
103
            $this->closeButton = array_merge($this->closeButton, ['encode' => false]);
104
            $this->options = array_merge($this->options, ['encode' => false]);
105
        }
106
107
        if (!isset($this->options['role'])) {
108
            $this->options['role'] = 'alert';
109
        }
110
    }
111 2
112
    /**
113 2
     * The body content in the alert component. Alert widget will also be treated as the body content, and will be
114
     * rendered before this.
115 2
     *
116
     * @param string|null $value
117
     *
118
     * @return $this
119
     */
120
    public function withBody(?string $value): self
121
    {
122
        $new = clone $this;
123
        $new->body = $value;
124
125
        return $new;
126
    }
127
128
    /**
129
     * The options for rendering the close button tag.
130
     *
131
     * The close button is displayed in the header of the modal window. Clicking on the button will hide the modal
132
     * window. If {@see closeButtonEnabled} is false, no close button will be rendered.
133
     *
134
     * The following special options are supported:
135
     *
136
     * - tag: string, the tag name of the button. Defaults to 'button'.
137
     * - label: string, the label of the button. Defaults to '&times;'.
138
     *
139
     * The rest of the options will be rendered as the HTML attributes of the button tag.
140
     *
141
     * Please refer to the [Alert documentation](http://getbootstrap.com/components/#alerts) for the supported HTML
142
     * attributes.
143
     *
144
     * @param array $value
145
     *
146
     * @return $this
147
     */
148
    public function withCloseButton(array $value): self
149
    {
150
        $new = clone $this;
151
        $new->closeButton = $value;
152
153
        return $new;
154
    }
155
156
    /**
157
     * Disable close button.
158
     *
159
     * @param bool $value
160
     *
161
     * @return $this
162
     */
163
    public function withCloseButtonDisabled(bool $value = false): self
164
    {
165
        $new = clone $this;
166
        $new->closeButtonEnabled = $value;
167
168 1
        return $new;
169
    }
170 1
171
    /**
172 1
     * The HTML attributes for the widget container tag. The following special options are recognized.
173
     *
174
     * {@see Html::renderTagAttributes()} for details on how attributes are being rendered.
175
     *
176
     * @param array $value
177
     *
178
     * @return $this
179
     */
180
    public function withOptions(array $value): self
181
    {
182
        $new = clone $this;
183
        $new->options = $value;
184
185
        return $new;
186
    }
187
188
    /**
189
     * Allows you to enable or disable the encoding tags html.
190
     *
191
     * @param bool $value
192
     *
193
     * @return self
194
     */
195
    public function withEncodeTags(bool $value): self
196
    {
197
        $new = clone $this;
198
        $new->encodeTags = $value;
199
200
        return $new;
201
    }
202
}
203