Passed
Push — master ( 28ce82...da5298 )
by Yuri
03:53 queued 23s
created

CustomTemplate::wrapMessages()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 3
rs 10
c 1
b 0
f 0
1
<?php
2
3
use Tamtamchik\SimpleFlash\BaseTemplate;
4
use Tamtamchik\SimpleFlash\TemplateInterface;
5
use function Tamtamchik\SimpleFlash\flash;
6
7
session_start();
8
9
require_once __DIR__ . '/../vendor/autoload.php';
10
11
class CustomTemplate extends BaseTemplate implements TemplateInterface
12
{
13
    protected $prefix = '<li>'; // every line prefix
14
    protected $postfix = '</li>'; // every postfix
15
    protected $wrapper = '<ul class="a a-%s">%s</ul>'; // wrapper over messages of same type
16
17
    /**
18
     * @param string $messages - message text
19
     * @param string $type - message type: success, info, warning, error
20
     *
21
     * @return string
22
     */
23
    public function wrapMessages(string $messages, string $type): string
24
    {
25
        return sprintf($this->getWrapper(), $type, $messages);
26
    }
27
}
28
29
flash()->setTemplate(new CustomTemplate)->error(['Invalid email!', 'Invalid username!'])
30
    ->warning('Warning message.')
31
    ->info('Info message.')
32
    ->success('Success message!');
33
?>
34
35
<!doctype html>
36
<html lang="en">
37
<head>
38
    <meta charset="UTF-8">
39
    <title>Test custom template example.</title>
40
41
    <style>
42
        .container {
43
            max-width: 600px;
44
            margin: 0 auto;
45
            font-family: -apple-system, --sans-serif, sans-serif;
46
        }
47
48
        .a {
49
            border: 1px solid;
50
            border-radius: 10px;
51
            padding: 1em;
52
            margin: 0.25em 0;
53
        }
54
55
        .a li {
56
            list-style: none;
57
        }
58
59
        .a-info {
60
            background: lightblue;
61
        }
62
63
        .a-error {
64
            background: lightpink;
65
        }
66
67
        .a-warning {
68
            background: lightgoldenrodyellow;
69
        }
70
71
        .a-success {
72
            background: lightgreen;
73
        }
74
    </style>
75
</head>
76
<body>
77
78
<?php include_once '_ribbon.php'; ?>
79
80
<div class="container" style="width: 600px;">
81
82
    <?php include_once '_menu.php'; ?>
83
84
    <?= flash() ?>
85
86
</div>
87
88
</body>
89
</html>
90