|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* MessageTrait.php - Show alert messages. |
|
5
|
|
|
* |
|
6
|
|
|
* @package jaxon-core |
|
|
|
|
|
|
7
|
|
|
* @author Thierry Feuzeu <[email protected]> |
|
8
|
|
|
* @copyright 2024 Thierry Feuzeu <[email protected]> |
|
9
|
|
|
* @license https://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License |
|
10
|
|
|
* @link https://github.com/jaxon-php/jaxon-core |
|
11
|
|
|
*/ |
|
|
|
|
|
|
12
|
|
|
|
|
13
|
|
|
namespace Jaxon\App\Dialog\Library; |
|
14
|
|
|
|
|
15
|
|
|
use Jaxon\App\Dialog\MessageInterface; |
|
16
|
|
|
use Jaxon\Request\Call\Parameter; |
|
17
|
|
|
|
|
18
|
|
|
use function array_map; |
|
19
|
|
|
|
|
20
|
|
|
trait MessageTrait |
|
|
|
|
|
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* The next message title |
|
24
|
|
|
* |
|
25
|
|
|
* @var string |
|
26
|
|
|
*/ |
|
27
|
|
|
private $sTitle = ''; |
|
|
|
|
|
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Set the title of the next message. |
|
31
|
|
|
* |
|
32
|
|
|
* @param string $sTitle The title of the message |
|
|
|
|
|
|
33
|
|
|
* |
|
34
|
|
|
* @return MessageInterface |
|
35
|
|
|
*/ |
|
36
|
|
|
public function title(string $sTitle): MessageInterface |
|
|
|
|
|
|
37
|
|
|
{ |
|
38
|
|
|
$this->sTitle = $sTitle; |
|
39
|
|
|
|
|
40
|
|
|
return $this; |
|
|
|
|
|
|
41
|
|
|
} |
|
|
|
|
|
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Print an alert message. |
|
45
|
|
|
* |
|
46
|
|
|
* @param string $sType The type of the message |
|
|
|
|
|
|
47
|
|
|
* @param string $sMessage The text of the message |
|
|
|
|
|
|
48
|
|
|
* @param array $aArgs The message arguments |
|
|
|
|
|
|
49
|
|
|
* |
|
50
|
|
|
* @return array |
|
51
|
|
|
*/ |
|
52
|
|
|
private function alert(string $sType, string $sMessage, array $aArgs): array |
|
53
|
|
|
{ |
|
54
|
|
|
$sTitle = $this->sTitle; |
|
|
|
|
|
|
55
|
|
|
$this->sTitle = ''; |
|
56
|
|
|
|
|
57
|
|
|
return [ |
|
58
|
|
|
'type' => $sType, |
|
59
|
|
|
'message' => [ |
|
60
|
|
|
'title' => $sTitle, |
|
61
|
|
|
'phrase' => [ |
|
62
|
|
|
'str' => $sMessage, |
|
63
|
|
|
'args' => array_map(fn($xArg) => Parameter::make($xArg), $aArgs), |
|
64
|
|
|
], |
|
65
|
|
|
], |
|
66
|
|
|
]; |
|
67
|
|
|
} |
|
|
|
|
|
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Show a success message. |
|
71
|
|
|
* |
|
72
|
|
|
* @param string $sMessage The text of the message |
|
|
|
|
|
|
73
|
|
|
* @param array $aArgs The message arguments |
|
|
|
|
|
|
74
|
|
|
* |
|
75
|
|
|
* @return array |
|
76
|
|
|
*/ |
|
77
|
|
|
public function success(string $sMessage, array $aArgs = []): array |
|
78
|
|
|
{ |
|
79
|
|
|
return $this->alert('success', $sMessage, $aArgs); |
|
80
|
|
|
} |
|
|
|
|
|
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Show an information message. |
|
84
|
|
|
* |
|
85
|
|
|
* @param string $sMessage The text of the message |
|
|
|
|
|
|
86
|
|
|
* @param array $aArgs The message arguments |
|
|
|
|
|
|
87
|
|
|
* |
|
88
|
|
|
* @return array |
|
89
|
|
|
*/ |
|
90
|
|
|
public function info(string $sMessage, array $aArgs = []): array |
|
91
|
|
|
{ |
|
92
|
|
|
return $this->alert('info', $sMessage, $aArgs); |
|
93
|
|
|
} |
|
|
|
|
|
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Show a warning message. |
|
97
|
|
|
* |
|
98
|
|
|
* @param string $sMessage The text of the message |
|
|
|
|
|
|
99
|
|
|
* @param array $aArgs The message arguments |
|
|
|
|
|
|
100
|
|
|
* |
|
101
|
|
|
* @return array |
|
102
|
|
|
*/ |
|
103
|
|
|
public function warning(string $sMessage, array $aArgs = []): array |
|
104
|
|
|
{ |
|
105
|
|
|
return $this->alert('warning', $sMessage, $aArgs); |
|
106
|
|
|
} |
|
|
|
|
|
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Show an error message. |
|
110
|
|
|
* |
|
111
|
|
|
* @param string $sMessage The text of the message |
|
|
|
|
|
|
112
|
|
|
* @param array $aArgs The message arguments |
|
|
|
|
|
|
113
|
|
|
* |
|
114
|
|
|
* @return array |
|
115
|
|
|
*/ |
|
116
|
|
|
public function error(string $sMessage, array $aArgs = []): array |
|
117
|
|
|
{ |
|
118
|
|
|
return $this->alert('error', $sMessage, $aArgs); |
|
119
|
|
|
} |
|
|
|
|
|
|
120
|
|
|
} |
|
121
|
|
|
|