ToastrLibrary::warning()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * ToastrLibrary.php
5
 *
6
 * Adapter for the Toastr library.
7
 *
8
 * @package jaxon-dialogs
9
 * @author Thierry Feuzeu <[email protected]>
10
 * @copyright 2016 Thierry Feuzeu <[email protected]>
11
 * @license https://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License
12
 * @link https://github.com/jaxon-php/jaxon-dialogs
13
 */
14
15
namespace Jaxon\Dialogs\Toastr;
16
17
use Jaxon\App\Dialog\Library\DialogLibraryTrait;
18
use Jaxon\App\Dialog\MessageInterface;
19
20
class ToastrLibrary implements MessageInterface
21
{
22
    use DialogLibraryTrait;
23
24
    /**
25
     * @const The library name
26
     */
27
    const NAME = 'toastr';
28
29
    /**
30
     * @inheritDoc
31
     */
32
    public function getName(): string
33
    {
34
        return self::NAME;
35
    }
36
37
    /**
38
     * @inheritDoc
39
     */
40
    public function getSubdir(): string
41
    {
42
        return 'toastr.js';
43
    }
44
45
    /**
46
     * @inheritDoc
47
     */
48
    public function getVersion(): string
49
    {
50
        return '2.1.3';
51
    }
52
53
    /**
54
     * @inheritDoc
55
     */
56
    public function getJs(): string
57
    {
58
        return $this->helper()->getJsCode('toastr.min.js');
59
    }
60
61
    /**
62
     * @inheritDoc
63
     */
64
    public function getCss(): string
65
    {
66
        return $this->helper()->getCssCode('toastr.min.css');
67
    }
68
69
    /**
70
     * @inheritDoc
71
     */
72
    public function getScript(): string
73
    {
74
        return $this->helper()->render('toastr/alert.js');
75
    }
76
77
    /**
78
     * @inheritDoc
79
     */
80
    public function getReadyScript(): string
81
    {
82
        return $this->helper()->render('toastr/ready.js.php', [
83
            'options' =>  $this->helper()->getOptionScript('toastr.options.', 'options.')
84
        ]);
85
    }
86
87
    /**
88
     * Print an alert message.
89
     *
90
     * @param string $sMessage The text of the message
91
     * @param string $sTitle The title of the message
92
     * @param string $sType The type of the message
93
     *
94
     * @return string
95
     */
96
    protected function alert(string $sMessage, string $sTitle, string $sType): string
97
    {
98
        if($this->returnCode())
99
        {
100
            return empty($sTitle) ? "toastr.$sType($sMessage)" : "toastr.$sType($sMessage, '$sTitle')";
101
        }
102
        $aOptions = ['message' => $sMessage, 'title' => $sTitle];
103
        // Show the alert
104
        $this->addCommand(['cmd' => 'toastr.' . $sType], $aOptions);
105
        return '';
106
    }
107
108
    /**
109
     * @inheritDoc
110
     */
111
    public function success(string $sMessage, string $sTitle = ''): string
112
    {
113
        return $this->alert($sMessage, $sTitle, 'success');
114
    }
115
116
    /**
117
     * @inheritDoc
118
     */
119
    public function info(string $sMessage, string $sTitle = ''): string
120
    {
121
        return $this->alert($sMessage, $sTitle, 'info');
122
    }
123
124
    /**
125
     * @inheritDoc
126
     */
127
    public function warning(string $sMessage, string $sTitle = ''): string
128
    {
129
        return $this->alert($sMessage, $sTitle, 'warning');
130
    }
131
132
    /**
133
     * @inheritDoc
134
     */
135
    public function error(string $sMessage, string $sTitle = ''): string
136
    {
137
        return $this->alert($sMessage, $sTitle, 'error');
138
    }
139
140
    public function remove()
141
    {
142
        $this->response()->script('toastr.remove()');
143
    }
144
145
    public function clear()
146
    {
147
        $this->response()->script('toastr.clear()');
148
    }
149
}
150