-
Notifications
You must be signed in to change notification settings - Fork 4
/
Message.php
174 lines (155 loc) · 4.27 KB
/
Message.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
<?php
namespace Sunlight;
use Sunlight\Util\StringHelper;
/**
* System message
*/
class Message
{
const OK = 'ok';
const WARNING = 'warn';
const ERROR = 'err';
/** @var string */
private $type;
/** @var string */
private $message;
/** @var bool */
private $isHtml;
/**
* @param string $type see class constants
* @param string $message the message
* @param bool $isHtml display the message should be rendered as html (unescaped) 1/0
*/
function __construct(string $type, string $message, bool $isHtml = false)
{
$this->type = $type;
$this->message = $message;
$this->isHtml = $isHtml;
}
/**
* Render a message
*
* @see __construct()
*/
static function render(string $type, string $message, bool $isHtml = false): string
{
$message = new self($type, $message, $isHtml);
return $message->__toString();
}
/**
* Create an informational message
*
* @param string $message the message
* @param bool $isHtml display the message should be rendered as html (unescaped) 1/0
*/
static function ok(string $message, bool $isHtml = false): self
{
return new self(self::OK, $message, $isHtml);
}
/**
* Create a warning message
*
* @param string $message the message
* @param bool $isHtml display the message should be rendered as html (unescaped) 1/0
*/
static function warning(string $message, bool $isHtml = false): self
{
return new self(self::WARNING, $message, $isHtml);
}
/**
* Create an error message
*
* @param string $message the message
* @param bool $isHtml display the message should be rendered as html (unescaped) 1/0
*/
static function error(string $message, bool $isHtml = false): self
{
return new self(self::ERROR, $message, $isHtml);
}
/**
* List messages
*
* Supported options:
* ------------------
* - type (warn) see Message class constants
* - text content at the beginning of the message
* - list options for {@see GenericTemplates::renderMessageList()}
*
* @param string[] $messages
* @param array{
* type?: string,
* text?: string,
* list?: array{lcfirst?: bool|null, trim_dots?: bool|null, escape?: bool|null, show_keys?: bool|null},
* } $options see description
*/
static function list(array $messages, array $options = []): self
{
return new self(
$options['type'] ?? self::WARNING,
($options['text'] ?? _lang('error.list_text'))
. "\n"
. GenericTemplates::renderMessageList($messages, $options['list'] ?? []),
true
);
}
/**
* Prefix a message
*/
static function prefix(string $prefix, string $message): string
{
return $prefix . ': ' . StringHelper::lcfirst($message);
}
/**
* Render the message
*/
function __toString(): string
{
$output = Extend::buffer('message.render', ['message' => $this]);
if ($output === '') {
$output = "\n<div class=\"message message-" . _e($this->type) . '">'
. ($this->isHtml ? $this->message : _e($this->message))
. "</div>\n";
}
return $output;
}
/**
* Get the message type
*/
function getType(): string
{
return $this->type;
}
/**
* Get the message
*/
function getMessage(): string
{
return $this->message;
}
/**
* See if the message is HTML
*/
function isHtml(): bool
{
return $this->isHtml;
}
/**
* Append a string to the message
*
* This forces the message to become HTML, if it isn't already
*/
function append(string $str, bool $isHtml = false): void
{
if ($this->isHtml) {
// append to current HTML
$this->message .= $isHtml ? $str : _e($str);
} elseif ($isHtml) {
// convert message to HTML
$this->message = _e($this->message) . $str;
$this->isHtml = true;
} else {
// append as-is
$this->message .= $str;
}
}
}