Passed
Pull Request — master (#31)
by Mr.
02:50 queued 14s
created

Toast   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 341
Duplicated Lines 0 %

Test Coverage

Coverage 79.45%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 22
eloc 70
c 1
b 0
f 0
dl 0
loc 341
ccs 58
cts 73
cp 0.7945
rs 10

17 Methods

Rating   Name   Duplication   Size   Complexity  
A headerOptions() 0 5 1
A footer() 0 5 1
A renderBodyBegin() 0 4 1
A initOptions() 0 19 4
A closeButton() 0 5 1
A bodyOptions() 0 5 1
A dateTime() 0 5 1
A footerOptions() 0 5 1
A title() 0 5 1
A run() 0 7 1
A titleOptions() 0 5 1
A begin() 0 14 2
A options() 0 5 1
A renderHeader() 0 16 2
A dateTimeOptions() 0 5 1
A renderCloseButton() 0 8 1
A renderBodyEnd() 0 3 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
use function array_merge;
12
13
/**
14
 * Toasts renders an toast bootstrap component.
15
 *
16
 * For example,
17
 *
18
 * ```php
19
  * Toast::widget()
20
 *     ->title('Hello world!')
21
 *     ->dateTime('a minute ago')
22
 *     ->body('Say hello...')
23
 * ])
24
 * ->begin();
25
 * ```
26
 *
27
 * The following example will show the content enclosed between the [[begin()]]
28
 * and [[end()]] calls within the toast box:
29
 *
30
 * ```php
31
 * Toast::widget()
32
 *     ->title('Hello world!')
33
 *     ->dateTime('a minute ago')
34
 * ])
35
 * ->begin();
36
 *
37
 * echo 'Say hello...';
38
 *
39
 * Toast::end();
40
 * ```
41
 *
42
 * @see https://getbootstrap.com/docs/components/toasts/
43
 * @author Simon Karlen <[email protected]>
44
 */
45
final class Toast extends Widget
46
{
47
    /**
48
     * @var string the body content in the alert component. Note that anything between
49
     * the [[begin()]] and [[end()]] calls of the Toast widget will also be treated
50
     * as the body content, and will be rendered before this.
51
     */
52
    private string $body = '';
53
    /**
54
     * @var string The title content in the toast.
55
     */
56
    private string $title = '';
57
    /**
58
     * @var string The date time the toast message references to.
59
     * This will be formatted as relative time (via formatter component). It will be omitted if
60
     * set to `false` (default).
61
     */
62
    private string $dateTime = '';
63
    /**
64
     * @var array the options for rendering the close button tag.
65
     * The close button is displayed in the header of the toast. Clicking on the button will hide the toast.
66
     *
67
     * The following special options are supported:
68
     *
69
     * - tag: string, the tag name of the button. Defaults to 'button'.
70
     * - label: string, the label of the button. Defaults to '&times;'.
71
     *
72
     * The rest of the options will be rendered as the HTML attributes of the button tag.
73
     * Please refer to the [Toast documentation](https://getbootstrap.com/docs/4.5/components/toasts/)
74
     * for the supported HTML attributes.
75
     */
76
    private array $closeButton = [];
77
    /**
78
     * @var array additional title options
79
     *
80
     * The following special options are supported:
81
     *
82
     * - tag: string, the tag name of the button. Defaults to 'strong'.
83
     *
84
     * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
85
     */
86
    public array $titleOptions = [];
87
    /**
88
     * @var array additional date time part options
89
     *
90
     * The following special options are supported:
91
     *
92
     * - tag: string, the tag name of the button. Defaults to 'small'.
93
     *
94
     * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
95
     */
96
    private array $dateTimeOptions = [];
97
    /**
98
     * @var array additional header options
99
     * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
100
     */
101
    private array $headerOptions = [];
102
    /**
103
     * @var array body options
104
     * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
105
     */
106
    private array $bodyOptions = [];
107
108 4
    public function begin(): ?string
109
    {
110 4
        parent::begin();
111
112 4
        if (!isset($this->options['id'])) {
113 4
            $this->options['id'] = "{$this->getId()}-toast";
0 ignored issues
show
Bug Best Practice introduced by
The property options does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
114
        }
115
116 4
        $this->initOptions();
117
118
        return
119 4
            Html::beginTag('div', $this->options) . "\n" .
120 4
            $this->renderHeader() . "\n" .
121 4
            $this->renderBodyBegin() . "\n";
122
    }
123
124 4
    protected function run(): string
125
    {
126 4
        $this->registerPlugin('toast', $this->options);
127
128
        return
129 4
            "\n" . $this->renderBodyEnd() .
130 4
            "\n" . Html::endTag('div');
131
    }
132
133
    /**
134
     * Renders the header HTML markup of the toast.
135
     *
136
     * @throws JsonException
137
     *
138
     * @return string the rendering result
139
     */
140 4
    protected function renderHeader(): string
141
    {
142 4
        $button = $this->renderCloseButton();
143 4
        $tag = ArrayHelper::remove($this->titleOptions, 'tag', 'strong');
144 4
        Html::addCssClass($this->titleOptions, ['widget' => 'me-auto']);
145 4
        $title = Html::tag($tag, $this->title, $this->titleOptions);
146
147 4
        if ($this->dateTime !== '') {
148 2
            $tag = ArrayHelper::remove($this->dateTimeOptions, 'tag', 'small');
149 2
            $title .= "\n" . Html::tag($tag, $this->dateTime, $this->dateTimeOptions);
150
        }
151
152 4
        $title .= "\n" . $button;
153
154 4
        Html::addCssClass($this->headerOptions, ['widget' => 'toast-header']);
155 4
        return Html::div("\n" . $title . "\n", $this->headerOptions);
156
    }
157
158
    /**
159
     * Renders the opening tag of the toast body.
160
     *
161
     * @throws JsonException
162
     *
163
     * @return string the rendering result
164
     */
165 4
    protected function renderBodyBegin(): string
166
    {
167 4
        Html::addCssClass($this->bodyOptions, ['widget' => 'toast-body']);
168 4
        return Html::beginTag('div', $this->bodyOptions);
169
    }
170
171
    /**
172
     * Renders the closing tag of the toast body.
173
     *
174
     * @return string the rendering result
175
     */
176 4
    protected function renderBodyEnd(): string
177
    {
178 4
        return $this->body . "\n" . Html::endTag('div');
179
    }
180
181
    /**
182
     * Renders the close button.
183
     *
184
     * @throws JsonException
185
     *
186
     * @return string the rendering result
187
     */
188 4
    protected function renderCloseButton(): ?string
189
    {
190 4
        $tag = ArrayHelper::remove($this->closeButton, 'tag', 'button');
191 4
        $label = ArrayHelper::remove($this->closeButton, 'label', Html::tag('span', '&times;', [
192 4
            'aria-hidden' => 'true'
193
        ]));
194
195 4
        return Html::tag($tag, "\n" . $label . "\n", $this->closeButton);
196
    }
197
198
    /**
199
     * Initializes the widget options.
200
     *
201
     * This method sets the default values for various options.
202
     */
203 4
    protected function initOptions(): void
204
    {
205 4
        Html::addCssClass($this->options, ['widget' => 'toast']);
206
207 4
        $this->closeButton = array_merge([
208 4
            'aria' => ['label' => 'Close'],
209
            'data' => ['bs-dismiss' => 'toast'],
210
            'class' => ['widget' => 'btn-close'],
211
            'type' => 'button'
212 4
        ], $this->closeButton);
213
214 4
        if (!isset($this->options['role'])) {
215 4
            $this->options['role'] = 'alert';
0 ignored issues
show
Bug Best Practice introduced by
The property options does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
216
        }
217
218 4
        if (!isset($this->options['aria']) && !isset($this->options['aria-live'])) {
219 4
            $this->options['aria'] = [
220
                'live' => 'assertive',
221
                'atomic' => 'true'
222
            ];
223
        }
224 4
    }
225
226
    /**
227
     * Body options.
228
     *
229
     * {@see \Yiisoft\Html\Html::renderTagAttributes()} for details on how attributes are being rendered.
230
     *
231
     * @param array $value
232
     *
233
     * @return $this
234
     */
235 1
    public function bodyOptions(array $value): self
236
    {
237 1
        $this->bodyOptions = $value;
238
239 1
        return $this;
240
    }
241
242
    /**
243
     * The options for rendering the close button tag.
244
     *
245
     * The close button is displayed in the header of the toast window. Clicking on the button will hide the toast
246
     * window. If {@see closeButtonEnabled} is false, no close button will be rendered.
247
     *
248
     * The following special options are supported:
249
     *
250
     * - tag: string, the tag name of the button. Defaults to 'button'.
251
     * - label: string, the label of the button. Defaults to '&times;'.
252
     *
253
     * The rest of the options will be rendered as the HTML attributes of the button tag. Please refer to the
254
     * [Toast plugin help](https://getbootstrap.com/docs/components/toasts/) for the supported HTML attributes.
255
     *
256
     * @param array $value
257
     *
258
     * @return $this
259
     */
260
    public function closeButton(array $value): self
261
    {
262
        $this->closeButton = $value;
263
264
        return $this;
265
    }
266
267
    /**
268
     * The date/time content in the toast window.
269
     *
270
     * @param string $value
271
     *
272
     * @return $this
273
     */
274 2
    public function dateTime(string $value): self
275
    {
276 2
        $this->dateTime = $value;
277
278 2
        return $this;
279
    }
280
281
    /**
282
     * Additional DateTime options.
283
     *
284
     * @param array $value
285
     *
286
     * @return $this
287
     *
288
     * {@see \Yiisoft\Html\Html::renderTagAttributes()} for details on how attributes are being rendered.
289
     */
290 1
    public function dateTimeOptions(array $value): self
291
    {
292 1
        $this->dateTimeOptions = $value;
293
294 1
        return $this;
295
    }
296
297
    /**
298
     * The footer content in the toast window.
299
     *
300
     * @param string|null $value
301
     *
302
     * @return $this
303
     */
304
    public function footer(?string $value): self
305
    {
306
        $this->footer = $value;
0 ignored issues
show
Bug Best Practice introduced by
The property footer does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
307
308
        return $this;
309
    }
310
311
    /**
312
     * Additional footer options.
313
     *
314
     * @param array $value
315
     *
316
     * @return $this
317
     *
318
     * {@see \Yiisoft\Html\Html::renderTagAttributes()} for details on how attributes are being rendered.
319
     */
320
    public function footerOptions(array $value): self
321
    {
322
        $this->footerOptions = $value;
0 ignored issues
show
Bug Best Practice introduced by
The property footerOptions does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
323
324
        return $this;
325
    }
326
327
    /**
328
     * Additional header options.
329
     *
330
     * @param array $value
331
     *
332
     * @return $this
333
     *
334
     * {@see \Yiisoft\Html\Html::renderTagAttributes()} for details on how attributes are being rendered.
335
     */
336
    public function headerOptions(array $value): self
337
    {
338
        $this->headerOptions = $value;
339
340
        return $this;
341
    }
342
343
    /**
344
     * @param array $value the HTML attributes for the widget container tag. The following special options are
345
     * recognized.
346
     *
347
     * @return $this
348
     *
349
     * {@see \Yiisoft\Html\Html::renderTagAttributes()} for details on how attributes are being rendered.
350
     */
351
    public function options(array $value): self
352
    {
353
        $this->options = $value;
0 ignored issues
show
Bug Best Practice introduced by
The property options does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
354
355
        return $this;
356
    }
357
358
    /**
359
     * The title content in the toast window.
360
     *
361
     * @param string|null $value
362
     *
363
     * @return $this
364
     */
365 3
    public function title(?string $value): self
366
    {
367 3
        $this->title = $value;
368
369 3
        return $this;
370
    }
371
372
    /**
373
     * Additional title options.
374
     *
375
     * {@see \Yiisoft\Html\Html::renderTagAttributes()} for details on how attributes are being rendered.
376
     *
377
     * @param array $value
378
     *
379
     * @return $this
380
     */
381 1
    public function titleOptions(array $value): self
382
    {
383 1
        $this->titleOptions = $value;
384
385 1
        return $this;
386
    }
387
}
388