Bootstrap5DismissibleTemplate   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 20
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
dl 0
loc 20
rs 10
c 1
b 0
f 0
wmc 2

1 Method

Rating   Name   Duplication   Size   Complexity  
A wrapMessages() 0 5 2
1
<?php
2
3
use Tamtamchik\SimpleFlash\BaseTemplate;
4
use Tamtamchik\SimpleFlash\TemplateFactory;
5
use Tamtamchik\SimpleFlash\TemplateInterface;
6
use Tamtamchik\SimpleFlash\Templates;
7
use function Tamtamchik\SimpleFlash\flash;
8
9
if ( ! session_id()) {
10
    session_start();
11
}
12
13
require_once '../../vendor/autoload.php';
14
15
class Bootstrap5DismissibleTemplate extends BaseTemplate implements TemplateInterface
16
{
17
    protected $prefix = ''; // every line prefix
18
    protected $postfix = '<br>'; // every postfix
19
    protected $wrapper = '<div class="alert alert-%s alert-dismissible fade show" role="alert">
20
                            %s
21
                            <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
22
                          </div>'; // wrapper over messages of same type
23
24
    /**
25
     * @param string $messages - message text
26
     * @param string $type - message type: success, info, warning, error
27
     *
28
     * @return string
29
     */
30
    public function wrapMessages(string $messages, string $type): string
31
    {
32
        $type = ($type == 'error') ? 'danger' : $type;
33
34
        return sprintf($this->getWrapper(), $type, $messages);
35
    }
36
}
37
38
flash()
39
    ->error(['Invalid email!', 'Invalid username!'])
40
    ->warning('Warning message.')
41
    ->info('Info message.')
42
    ->success('Success message!');
43
44
?>
45
46
<!doctype html>
47
<html lang="en">
48
<head>
49
    <meta charset="UTF-8">
50
    <title>Test Bootstrap default template example.</title>
51
    <!-- Latest compiled and minified CSS -->
52
    <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet"
53
          integrity="sha384-BmbxuPwQa2lc/FVzBcNJ7UAyJxM6wuqIj61tLrc4wSX0szH/Ev+nYRRuWlolflfl" crossorigin="anonymous">
54
</head>
55
<body>
56
57
<div class="container" style="width: 600px; margin: 1em auto;">
58
    <?php
59
    if (flash()->some('error')) {
60
        echo flash()->setTemplate(new Bootstrap5DismissibleTemplate())->display('error');
61
    }
62
    echo flash()->setTemplate(TemplateFactory::create(Templates::BOOTSTRAP))->display()
63
    ?>
64
</div>
65
66
<!-- Latest compiled and minified JavaScript -->
67
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"
68
        integrity="sha384-b5kHyXgcpbZJO/tY9Ul7kGkf1S0CWuKcCD38l8YkeH8z8QjE0GmW1gYU5S9FOnJ0"
69
        crossorigin="anonymous"></script>
70
71
</body>
72
</html>
73