Passed
Pull Request — master (#31)
by Mr.
04:02 queued 01:13
created

Toast::dateTime()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
rs 10
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
     * @var array options
109
     */
110
    private array $options = [];
111
112 4
    public function begin(): ?string
113
    {
114 4
        parent::begin();
115
116 4
        if (!isset($this->options['id'])) {
117 4
            $this->options['id'] = "{$this->getId()}-toast";
118
        }
119
120 4
        $this->initOptions();
121
122
        return
123 4
            Html::beginTag('div', $this->options) . "\n" .
124 4
            $this->renderHeader() . "\n" .
125 4
            $this->renderBodyBegin() . "\n";
126
    }
127
128 4
    protected function run(): string
129
    {
130 4
        $this->registerPlugin('toast', $this->options);
131
132
        return
133 4
            "\n" . $this->renderBodyEnd() .
134 4
            "\n" . Html::endTag('div');
135
    }
136
137
    /**
138
     * Renders the header HTML markup of the toast.
139
     *
140
     * @throws JsonException
141
     *
142
     * @return string the rendering result
143
     */
144 4
    protected function renderHeader(): string
145
    {
146 4
        $button = $this->renderCloseButton();
147 4
        $tag = ArrayHelper::remove($this->titleOptions, 'tag', 'strong');
148 4
        Html::addCssClass($this->titleOptions, ['widget' => 'me-auto']);
149 4
        $title = Html::tag($tag, $this->title, $this->titleOptions);
150
151 4
        if ($this->dateTime !== '') {
152 2
            $tag = ArrayHelper::remove($this->dateTimeOptions, 'tag', 'small');
153 2
            $title .= "\n" . Html::tag($tag, $this->dateTime, $this->dateTimeOptions);
154
        }
155
156 4
        $title .= "\n" . $button;
157
158 4
        Html::addCssClass($this->headerOptions, ['widget' => 'toast-header']);
159 4
        return Html::div("\n" . $title . "\n", $this->headerOptions);
160
    }
161
162
    /**
163
     * Renders the opening tag of the toast body.
164
     *
165
     * @throws JsonException
166
     *
167
     * @return string the rendering result
168
     */
169 4
    protected function renderBodyBegin(): string
170
    {
171 4
        Html::addCssClass($this->bodyOptions, ['widget' => 'toast-body']);
172 4
        return Html::beginTag('div', $this->bodyOptions);
173
    }
174
175
    /**
176
     * Renders the closing tag of the toast body.
177
     *
178
     * @return string the rendering result
179
     */
180 4
    protected function renderBodyEnd(): string
181
    {
182 4
        return $this->body . "\n" . Html::endTag('div');
183
    }
184
185
    /**
186
     * Renders the close button.
187
     *
188
     * @throws JsonException
189
     *
190
     * @return string the rendering result
191
     */
192 4
    protected function renderCloseButton(): ?string
193
    {
194 4
        $tag = ArrayHelper::remove($this->closeButton, 'tag', 'button');
195 4
        $label = ArrayHelper::remove($this->closeButton, 'label', Html::tag('span', '&times;', [
196 4
            'aria-hidden' => 'true'
197
        ]));
198
199 4
        return Html::tag($tag, "\n" . $label . "\n", $this->closeButton);
200
    }
201
202
    /**
203
     * Initializes the widget options.
204
     *
205
     * This method sets the default values for various options.
206
     */
207 4
    protected function initOptions(): void
208
    {
209 4
        Html::addCssClass($this->options, ['widget' => 'toast']);
210
211 4
        $this->closeButton = array_merge([
212 4
            'aria' => ['label' => 'Close'],
213
            'data' => ['bs-dismiss' => 'toast'],
214
            'class' => ['widget' => 'btn-close'],
215
            'type' => 'button'
216 4
        ], $this->closeButton);
217
218 4
        if (!isset($this->options['role'])) {
219 4
            $this->options['role'] = 'alert';
220
        }
221
222 4
        if (!isset($this->options['aria']) && !isset($this->options['aria-live'])) {
223 4
            $this->options['aria'] = [
224
                'live' => 'assertive',
225
                'atomic' => 'true'
226
            ];
227
        }
228 4
    }
229
230
    /**
231
     * Body options.
232
     *
233
     * {@see \Yiisoft\Html\Html::renderTagAttributes()} for details on how attributes are being rendered.
234
     *
235
     * @param array $value
236
     *
237
     * @return $this
238
     */
239 1
    public function bodyOptions(array $value): self
240
    {
241 1
        $this->bodyOptions = $value;
242
243 1
        return $this;
244
    }
245
246
    /**
247
     * The options for rendering the close button tag.
248
     *
249
     * The close button is displayed in the header of the toast window. Clicking on the button will hide the toast
250
     * window. If {@see closeButtonEnabled} is false, no close button will be rendered.
251
     *
252
     * The following special options are supported:
253
     *
254
     * - tag: string, the tag name of the button. Defaults to 'button'.
255
     * - label: string, the label of the button. Defaults to '&times;'.
256
     *
257
     * The rest of the options will be rendered as the HTML attributes of the button tag. Please refer to the
258
     * [Toast plugin help](https://getbootstrap.com/docs/components/toasts/) for the supported HTML attributes.
259
     *
260
     * @param array $value
261
     *
262
     * @return $this
263
     */
264
    public function closeButton(array $value): self
265
    {
266
        $this->closeButton = $value;
267
268
        return $this;
269
    }
270
271
    /**
272
     * The date/time content in the toast window.
273
     *
274
     * @param string $value
275
     *
276
     * @return $this
277
     */
278 2
    public function dateTime(string $value): self
279
    {
280 2
        $this->dateTime = $value;
281
282 2
        return $this;
283
    }
284
285
    /**
286
     * Additional DateTime options.
287
     *
288
     * @param array $value
289
     *
290
     * @return $this
291
     *
292
     * {@see \Yiisoft\Html\Html::renderTagAttributes()} for details on how attributes are being rendered.
293
     */
294 1
    public function dateTimeOptions(array $value): self
295
    {
296 1
        $this->dateTimeOptions = $value;
297
298 1
        return $this;
299
    }
300
301
    /**
302
     * The footer content in the toast window.
303
     *
304
     * @param string|null $value
305
     *
306
     * @return $this
307
     */
308
    public function footer(?string $value): self
309
    {
310
        $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...
311
312
        return $this;
313
    }
314
315
    /**
316
     * Additional footer options.
317
     *
318
     * @param array $value
319
     *
320
     * @return $this
321
     *
322
     * {@see \Yiisoft\Html\Html::renderTagAttributes()} for details on how attributes are being rendered.
323
     */
324
    public function footerOptions(array $value): self
325
    {
326
        $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...
327
328
        return $this;
329
    }
330
331
    /**
332
     * Additional header options.
333
     *
334
     * @param array $value
335
     *
336
     * @return $this
337
     *
338
     * {@see \Yiisoft\Html\Html::renderTagAttributes()} for details on how attributes are being rendered.
339
     */
340
    public function headerOptions(array $value): self
341
    {
342
        $this->headerOptions = $value;
343
344
        return $this;
345
    }
346
347
    /**
348
     * @param array $value the HTML attributes for the widget container tag. The following special options are
349
     * recognized.
350
     *
351
     * @return $this
352
     *
353
     * {@see \Yiisoft\Html\Html::renderTagAttributes()} for details on how attributes are being rendered.
354
     */
355
    public function options(array $value): self
356
    {
357
        $this->options = $value;
358
359
        return $this;
360
    }
361
362
    /**
363
     * The title content in the toast window.
364
     *
365
     * @param string|null $value
366
     *
367
     * @return $this
368
     */
369 3
    public function title(?string $value): self
370
    {
371 3
        $this->title = $value;
372
373 3
        return $this;
374
    }
375
376
    /**
377
     * Additional title options.
378
     *
379
     * {@see \Yiisoft\Html\Html::renderTagAttributes()} for details on how attributes are being rendered.
380
     *
381
     * @param array $value
382
     *
383
     * @return $this
384
     */
385 1
    public function titleOptions(array $value): self
386
    {
387 1
        $this->titleOptions = $value;
388
389 1
        return $this;
390
    }
391
}
392