Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
namespace Illuminate\Notifications\Messages;
4
 
5
use Illuminate\Contracts\Support\Htmlable;
6
use Illuminate\Notifications\Action;
7
 
8
class SimpleMessage
9
{
10
    /**
11
     * The "level" of the notification (info, success, error).
12
     *
13
     * @var string
14
     */
15
    public $level = 'info';
16
 
17
    /**
18
     * The subject of the notification.
19
     *
20
     * @var string
21
     */
22
    public $subject;
23
 
24
    /**
25
     * The notification's greeting.
26
     *
27
     * @var string
28
     */
29
    public $greeting;
30
 
31
    /**
32
     * The notification's salutation.
33
     *
34
     * @var string
35
     */
36
    public $salutation;
37
 
38
    /**
39
     * The "intro" lines of the notification.
40
     *
41
     * @var array
42
     */
43
    public $introLines = [];
44
 
45
    /**
46
     * The "outro" lines of the notification.
47
     *
48
     * @var array
49
     */
50
    public $outroLines = [];
51
 
52
    /**
53
     * The text / label for the action.
54
     *
55
     * @var string
56
     */
57
    public $actionText;
58
 
59
    /**
60
     * The action URL.
61
     *
62
     * @var string
63
     */
64
    public $actionUrl;
65
 
66
    /**
67
     * The name of the mailer that should send the notification.
68
     *
69
     * @var string
70
     */
71
    public $mailer;
72
 
73
    /**
74
     * Indicate that the notification gives information about a successful operation.
75
     *
76
     * @return $this
77
     */
78
    public function success()
79
    {
80
        $this->level = 'success';
81
 
82
        return $this;
83
    }
84
 
85
    /**
86
     * Indicate that the notification gives information about an error.
87
     *
88
     * @return $this
89
     */
90
    public function error()
91
    {
92
        $this->level = 'error';
93
 
94
        return $this;
95
    }
96
 
97
    /**
98
     * Set the "level" of the notification (success, error, etc.).
99
     *
100
     * @param  string  $level
101
     * @return $this
102
     */
103
    public function level($level)
104
    {
105
        $this->level = $level;
106
 
107
        return $this;
108
    }
109
 
110
    /**
111
     * Set the subject of the notification.
112
     *
113
     * @param  string  $subject
114
     * @return $this
115
     */
116
    public function subject($subject)
117
    {
118
        $this->subject = $subject;
119
 
120
        return $this;
121
    }
122
 
123
    /**
124
     * Set the greeting of the notification.
125
     *
126
     * @param  string  $greeting
127
     * @return $this
128
     */
129
    public function greeting($greeting)
130
    {
131
        $this->greeting = $greeting;
132
 
133
        return $this;
134
    }
135
 
136
    /**
137
     * Set the salutation of the notification.
138
     *
139
     * @param  string  $salutation
140
     * @return $this
141
     */
142
    public function salutation($salutation)
143
    {
144
        $this->salutation = $salutation;
145
 
146
        return $this;
147
    }
148
 
149
    /**
150
     * Add a line of text to the notification.
151
     *
152
     * @param  mixed  $line
153
     * @return $this
154
     */
155
    public function line($line)
156
    {
157
        return $this->with($line);
158
    }
159
 
160
    /**
161
     * Add a line of text to the notification if the given condition is true.
162
     *
163
     * @param  bool  $boolean
164
     * @param  mixed  $line
165
     * @return $this
166
     */
167
    public function lineIf($boolean, $line)
168
    {
169
        if ($boolean) {
170
            return $this->line($line);
171
        }
172
 
173
        return $this;
174
    }
175
 
176
    /**
177
     * Add lines of text to the notification.
178
     *
179
     * @param  iterable  $lines
180
     * @return $this
181
     */
182
    public function lines($lines)
183
    {
184
        foreach ($lines as $line) {
185
            $this->line($line);
186
        }
187
 
188
        return $this;
189
    }
190
 
191
    /**
192
     * Add lines of text to the notification if the given condition is true.
193
     *
194
     * @param  bool  $boolean
195
     * @param  iterable  $lines
196
     * @return $this
197
     */
198
    public function linesIf($boolean, $lines)
199
    {
200
        if ($boolean) {
201
            return $this->lines($lines);
202
        }
203
 
204
        return $this;
205
    }
206
 
207
    /**
208
     * Add a line of text to the notification.
209
     *
210
     * @param  mixed  $line
211
     * @return $this
212
     */
213
    public function with($line)
214
    {
215
        if ($line instanceof Action) {
216
            $this->action($line->text, $line->url);
217
        } elseif (! $this->actionText) {
218
            $this->introLines[] = $this->formatLine($line);
219
        } else {
220
            $this->outroLines[] = $this->formatLine($line);
221
        }
222
 
223
        return $this;
224
    }
225
 
226
    /**
227
     * Format the given line of text.
228
     *
229
     * @param  \Illuminate\Contracts\Support\Htmlable|string|array  $line
230
     * @return \Illuminate\Contracts\Support\Htmlable|string
231
     */
232
    protected function formatLine($line)
233
    {
234
        if ($line instanceof Htmlable) {
235
            return $line;
236
        }
237
 
238
        if (is_array($line)) {
239
            return implode(' ', array_map('trim', $line));
240
        }
241
 
242
        return trim(implode(' ', array_map('trim', preg_split('/\\r\\n|\\r|\\n/', $line ?? ''))));
243
    }
244
 
245
    /**
246
     * Configure the "call to action" button.
247
     *
248
     * @param  string  $text
249
     * @param  string  $url
250
     * @return $this
251
     */
252
    public function action($text, $url)
253
    {
254
        $this->actionText = $text;
255
        $this->actionUrl = $url;
256
 
257
        return $this;
258
    }
259
 
260
    /**
261
     * Set the name of the mailer that should send the notification.
262
     *
263
     * @param  string  $mailer
264
     * @return $this
265
     */
266
    public function mailer($mailer)
267
    {
268
        $this->mailer = $mailer;
269
 
270
        return $this;
271
    }
272
 
273
    /**
274
     * Get an array representation of the message.
275
     *
276
     * @return array
277
     */
278
    public function toArray()
279
    {
280
        return [
281
            'level' => $this->level,
282
            'subject' => $this->subject,
283
            'greeting' => $this->greeting,
284
            'salutation' => $this->salutation,
285
            'introLines' => $this->introLines,
286
            'outroLines' => $this->outroLines,
287
            'actionText' => $this->actionText,
288
            'actionUrl' => $this->actionUrl,
289
            'displayableActionUrl' => str_replace(['mailto:', 'tel:'], '', $this->actionUrl ?? ''),
290
        ];
291
    }
292
}