Passed
Pull Request — master (#31)
by Mr.
03:11 queued 42s
created

Toast   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 357
Duplicated Lines 0 %

Test Coverage

Coverage 79.45%

Importance

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

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