SimpleMessage.php
5.1 KB
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
<?php
namespace Illuminate\Notifications\Messages;
use Illuminate\Contracts\Support\Htmlable;
use Illuminate\Notifications\Action;
class SimpleMessage
{
/**
* The "level" of the notification (info, success, error).
*
* @var string
*/
public $level = 'info';
/**
* The subject of the notification.
*
* @var string
*/
public $subject;
/**
* The notification's greeting.
*
* @var string
*/
public $greeting;
/**
* The notification's salutation.
*
* @var string
*/
public $salutation;
/**
* The "intro" lines of the notification.
*
* @var array
*/
public $introLines = [];
/**
* The "outro" lines of the notification.
*
* @var array
*/
public $outroLines = [];
/**
* The text / label for the action.
*
* @var string
*/
public $actionText;
/**
* The action URL.
*
* @var string
*/
public $actionUrl;
/**
* The name of the mailer that should send the notification.
*
* @var string
*/
public $mailer;
/**
* Indicate that the notification gives information about a successful operation.
*
* @return $this
*/
public function success()
{
$this->level = 'success';
return $this;
}
/**
* Indicate that the notification gives information about an error.
*
* @return $this
*/
public function error()
{
$this->level = 'error';
return $this;
}
/**
* Set the "level" of the notification (success, error, etc.).
*
* @param string $level
* @return $this
*/
public function level($level)
{
$this->level = $level;
return $this;
}
/**
* Set the subject of the notification.
*
* @param string $subject
* @return $this
*/
public function subject($subject)
{
$this->subject = $subject;
return $this;
}
/**
* Set the greeting of the notification.
*
* @param string $greeting
* @return $this
*/
public function greeting($greeting)
{
$this->greeting = $greeting;
return $this;
}
/**
* Set the salutation of the notification.
*
* @param string $salutation
* @return $this
*/
public function salutation($salutation)
{
$this->salutation = $salutation;
return $this;
}
/**
* Add a line of text to the notification.
*
* @param mixed $line
* @return $this
*/
public function line($line)
{
return $this->with($line);
}
/**
* Add lines of text to the notification.
*
* @param iterable $lines
* @return $this
*/
public function lines($lines)
{
foreach ($lines as $line) {
$this->line($line);
}
return $this;
}
/**
* Add a line of text to the notification.
*
* @param mixed $line
* @return $this
*/
public function with($line)
{
if ($line instanceof Action) {
$this->action($line->text, $line->url);
} elseif (! $this->actionText) {
$this->introLines[] = $this->formatLine($line);
} else {
$this->outroLines[] = $this->formatLine($line);
}
return $this;
}
/**
* Format the given line of text.
*
* @param \Illuminate\Contracts\Support\Htmlable|string|array $line
* @return \Illuminate\Contracts\Support\Htmlable|string
*/
protected function formatLine($line)
{
if ($line instanceof Htmlable) {
return $line;
}
if (is_array($line)) {
return implode(' ', array_map('trim', $line));
}
return trim(implode(' ', array_map('trim', preg_split('/\\r\\n|\\r|\\n/', $line ?? ''))));
}
/**
* Configure the "call to action" button.
*
* @param string $text
* @param string $url
* @return $this
*/
public function action($text, $url)
{
$this->actionText = $text;
$this->actionUrl = $url;
return $this;
}
/**
* Set the name of the mailer that should send the notification.
*
* @param string $mailer
* @return $this
*/
public function mailer($mailer)
{
$this->mailer = $mailer;
return $this;
}
/**
* Get an array representation of the message.
*
* @return array
*/
public function toArray()
{
return [
'level' => $this->level,
'subject' => $this->subject,
'greeting' => $this->greeting,
'salutation' => $this->salutation,
'introLines' => $this->introLines,
'outroLines' => $this->outroLines,
'actionText' => $this->actionText,
'actionUrl' => $this->actionUrl,
'displayableActionUrl' => str_replace(['mailto:', 'tel:'], '', $this->actionUrl ?? ''),
];
}
}