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

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