Alert   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 152
Duplicated Lines 0 %

Test Coverage

Coverage 82.05%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 37
c 1
b 0
f 0
dl 0
loc 152
ccs 32
cts 39
cp 0.8205
rs 10
wmc 14

8 Methods

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