DialogCommand::alert()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 9
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 3
1
<?php
2
3
/**
4
 * DialogCommand.php
5
 *
6
 * Facade for dialogs functions.
7
 *
8
 * @author Thierry Feuzeu <[email protected]>
9
 * @copyright 2024 Thierry Feuzeu <[email protected]>
10
 * @license https://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License
11
 * @link https://github.com/jaxon-php/jaxon-core
12
 */
13
14
namespace Jaxon\Plugin\Response\Dialog;
15
16
use Jaxon\Script\Call\Parameter;
17
18
use function array_map;
19
20
class DialogCommand
21
{
22
    /**
23
     * @var DialogManager
24
     */
25
    protected $xDialogManager;
26
27
    /**
28
     * The next message title
29
     *
30
     * @var string
31
     */
32
    private $sTitle = '';
33
34
    /**
35
     * The constructor
36
     *
37
     * @param DialogManager $xDialogManager
38
     */
39
    public function __construct(DialogManager $xDialogManager)
40
    {
41
        $this->xDialogManager = $xDialogManager;
42
    }
43
44
    /**
45
     * @param string $sStr
46
     * @param array $aArgs
47
     *
48
     * @return array
49
     */
50
    private function phrase(string $sStr, array $aArgs = []): array
51
    {
52
        return [
53
            'str' => $sStr,
54
            'args' => array_map(function($xArg) {
55
                return Parameter::make($xArg);
56
            }, $aArgs),
57
        ];
58
    }
59
60
    /**
61
     * Set the title of the next message.
62
     *
63
     * @param string $sTitle     The title of the message
64
     *
65
     * @return self
66
     */
67
    public function title(string $sTitle)
68
    {
69
        $this->sTitle = $sTitle;
70
        return $this;
71
    }
72
73
    /**
74
     * Print an alert message.
75
     *
76
     * @param string $sType     The type of the message
77
     * @param string $sMessage  The text of the message
78
     * @param array $aArgs      The message arguments
79
     *
80
     * @return array
81
     */
82
    private function alert(string $sType, string $sMessage, array $aArgs): array
83
    {
84
        $sTitle = $this->sTitle;
85
        $this->sTitle = '';
86
        return [
87
            'lib' => $this->xDialogManager->getMessageLibrary()->getName(),
88
            'type' => $sType,
89
            'title' => $sTitle,
90
            'phrase' => $this->phrase($sMessage, $aArgs),
91
        ];
92
    }
93
94
    /**
95
     * Show a success message.
96
     *
97
     * @param string $sMessage  The text of the message
98
     * @param array $aArgs      The message arguments
99
     *
100
     * @return array
101
     */
102
    public function success(string $sMessage, array $aArgs = []): array
103
    {
104
        return $this->alert('success', $sMessage, $aArgs);
105
    }
106
107
    /**
108
     * Show an information message.
109
     *
110
     * @param string $sMessage  The text of the message
111
     * @param array $aArgs      The message arguments
112
     *
113
     * @return array
114
     */
115
    public function info(string $sMessage, array $aArgs = []): array
116
    {
117
        return $this->alert('info', $sMessage, $aArgs);
118
    }
119
120
    /**
121
     * Show a warning message.
122
     *
123
     * @param string $sMessage  The text of the message
124
     * @param array $aArgs      The message arguments
125
     *
126
     * @return array
127
     */
128
    public function warning(string $sMessage, array $aArgs = []): array
129
    {
130
        return $this->alert('warning', $sMessage, $aArgs);
131
    }
132
133
    /**
134
     * Show an error message.
135
     *
136
     * @param string $sMessage  The text of the message
137
     * @param array $aArgs      The message arguments
138
     *
139
     * @return array
140
     */
141
    public function error(string $sMessage, array $aArgs = []): array
142
    {
143
        return $this->alert('error', $sMessage, $aArgs);
144
    }
145
146
    /**
147
     * Show a modal dialog.
148
     *
149
     * @param string $sTitle The title of the dialog
150
     * @param string $sContent The content of the dialog
151
     * @param array $aButtons The buttons of the dialog
152
     * @param array $aOptions The options of the dialog
153
     *
154
     * Each button is an array with the following entries:
155
     * - title: the text to be printed in the button
156
     * - class: the CSS class of the button
157
     * - click: the javascript function to be called when the button is clicked
158
     * If the click value is set to "close", then the button closes the dialog.
159
     *
160
     * The content of the $aOptions depends on the javascript library in use.
161
     * Check their specific documentation for more information.
162
     *
163
     * @return array
164
     */
165
    public function show(string $sTitle, string $sContent, array $aButtons, array $aOptions = []): array
166
    {
167
        return [
168
            'lib' => $this->xDialogManager->getModalLibrary()->getName(),
169
            'dialog' => [
170
                'title' => $sTitle,
171
                'content' => $sContent,
172
                'buttons' => $aButtons,
173
                'options' => $aOptions,
174
            ],
175
        ];
176
    }
177
178
    /**
179
     * Hide the modal dialog.
180
     *
181
     * @return array
182
     */
183
    public function hide(): array
184
    {
185
        return [
186
            'lib' => $this->xDialogManager->getModalLibrary()->getName(),
187
        ];
188
    }
189
190
    /**
191
     * Add a confirm question to a function call.
192
     *
193
     * @param string $sQuestion
194
     * @param array $aArgs
195
     *
196
     * @return array
197
     */
198
    public function confirm(string $sQuestion, array $aArgs = []): array
199
    {
200
        $sTitle = $this->sTitle;
201
        $this->sTitle = '';
202
        return [
203
            'lib' => $this->xDialogManager->getQuestionLibrary()->getName(),
204
            'title' => $sTitle,
205
            'phrase' => $this->phrase($sQuestion, $aArgs),
206
        ];
207
    }
208
}
209