Passed
Pull Request — master (#22)
by Mr.
02:17
created

Alert::closeButtonEnabled()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
ccs 0
cts 3
cp 0
crap 2
rs 10
c 0
b 0
f 0
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
 *     ->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::div($this->renderBodyEnd(), $this->options);
42
    }
43
44
    /**
45
     * Renders the alert body and the close button (if any).
46
     *
47
     * @throws JsonException
48
     *
49
     * @return string the rendering result
50
     */
51 2
    protected function renderBodyEnd(): string
52
    {
53 2
        return $this->body . "\n" . $this->renderCloseButton() . "\n";
54
    }
55
56
    /**
57
     * Renders the close button.
58
     *
59
     * @throws JsonException
60
     *
61
     * @return string the rendering result
62
     */
63 2
    protected function renderCloseButton(): ?string
64
    {
65 2
        if ($this->closeButtonEnabled === false) {
66
            return null;
67
        }
68
69 2
        $tag = ArrayHelper::remove($this->closeButton, 'tag', 'button');
70 2
        $label = ArrayHelper::remove($this->closeButton, 'label', '');
71
72 2
        if ($tag === 'button' && !isset($this->closeButton['type'])) {
73 2
            $this->closeButton['type'] = 'button';
74
        }
75
76 2
        return Html::tag($tag, $label, $this->closeButton);
77
    }
78
79
    /**
80
     * Initializes the widget options.
81
     *
82
     * This method sets the default values for various options.
83
     */
84 2
    protected function initOptions(): void
85
    {
86 2
        Html::addCssClass($this->options, ['widget' => 'alert']);
87
88 2
        if ($this->closeButtonEnabled !== false) {
89 2
            $this->closeButton = [
90
                'aria-label' => 'Close',
91
                'class' => ['widget' => 'btn-close'],
92
                'data-dismiss' => 'alert',
93
            ];
94
95 2
            Html::addCssClass($this->options, ['alert-dismissible']);
96
        }
97
98 2
        if (!isset($this->options['role'])) {
99 2
            $this->options['role'] = 'alert';
100
        }
101 2
    }
102
103
    /**
104
     * The body content in the alert component. Alert widget will also be treated as the body content, and will be
105
     * rendered before this.
106
     *
107
     * @param string|null $value
108
     *
109
     * @return $this
110
     */
111 2
    public function body(?string $value): self
112
    {
113 2
        $this->body = $value;
114
115 2
        return $this;
116
    }
117
118
    /**
119
     * The options for rendering the close button tag.
120
     *
121
     * The close button is displayed in the header of the modal window. Clicking on the button will hide the modal
122
     * window. If {@see closeButtonEnabled} is false, no close button will be rendered.
123
     *
124
     * The following special options are supported:
125
     *
126
     * - tag: string, the tag name of the button. Defaults to 'button'.
127
     * - label: string, the label of the button. Defaults to '&times;'.
128
     *
129
     * The rest of the options will be rendered as the HTML attributes of the button tag.
130
     *
131
     * Please refer to the [Alert documentation](http://getbootstrap.com/components/#alerts) for the supported HTML
132
     * attributes.
133
     *
134
     * @param array $value
135
     *
136
     * @return $this
137
     */
138
    public function closeButton(array $value): self
139
    {
140
        $this->closeButton = $value;
141
142
        return $this;
143
    }
144
145
    /**
146
     * Enable/Disable close button.
147
     *
148
     * @param bool $value
149
     *
150
     * @return $this
151
     */
152
    public function closeButtonEnabled(bool $value): self
153
    {
154
        $this->closeButtonEnabled = $value;
155
156
        return $this;
157
    }
158
159
    /**
160
     * The HTML attributes for the widget container tag. The following special options are recognized.
161
     *
162
     * {@see \Yiisoft\Html\Html::renderTagAttributes()} for details on how attributes are being rendered.
163
     *
164
     * @param array $value
165
     *
166
     * @return $this
167
     */
168 1
    public function options(array $value): self
169
    {
170 1
        $this->options = $value;
171
172 1
        return $this;
173
    }
174
}
175