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\Mail\Mailables;
4
 
5
use Closure;
6
use Illuminate\Support\Arr;
7
use Illuminate\Support\Traits\Conditionable;
8
 
9
class Envelope
10
{
11
    use Conditionable;
12
 
13
    /**
14
     * The address sending the message.
15
     *
16
     * @var \Illuminate\Mail\Mailables\Address|string|null
17
     */
18
    public $from;
19
 
20
    /**
21
     * The recipients of the message.
22
     *
23
     * @var array
24
     */
25
    public $to;
26
 
27
    /**
28
     * The recipients receiving a copy of the message.
29
     *
30
     * @var array
31
     */
32
    public $cc;
33
 
34
    /**
35
     * The recipients receiving a blind copy of the message.
36
     *
37
     * @var array
38
     */
39
    public $bcc;
40
 
41
    /**
42
     * The recipients that should be replied to.
43
     *
44
     * @var array
45
     */
46
    public $replyTo;
47
 
48
    /**
49
     * The subject of the message.
50
     *
51
     * @var string|null
52
     */
53
    public $subject;
54
 
55
    /**
56
     * The message's tags.
57
     *
58
     * @var array
59
     */
60
    public $tags = [];
61
 
62
    /**
63
     * The message's meta data.
64
     *
65
     * @var array
66
     */
67
    public $metadata = [];
68
 
69
    /**
70
     * The message's Symfony Message customization callbacks.
71
     *
72
     * @var array
73
     */
74
    public $using = [];
75
 
76
    /**
77
     * Create a new message envelope instance.
78
     *
79
     * @param  \Illuminate\Mail\Mailables\Address|string|null  $from
80
     * @param  array  $to
81
     * @param  array  $cc
82
     * @param  array  $bcc
83
     * @param  array  $replyTo
84
     * @param  string|null  $subject
85
     * @param  array  $tags
86
     * @param  array  $metadata
87
     * @param  \Closure|array  $using
88
     * @return void
89
     *
90
     * @named-arguments-supported
91
     */
92
    public function __construct(Address|string $from = null, $to = [], $cc = [], $bcc = [], $replyTo = [], string $subject = null, array $tags = [], array $metadata = [], Closure|array $using = [])
93
    {
94
        $this->from = is_string($from) ? new Address($from) : $from;
95
        $this->to = $this->normalizeAddresses($to);
96
        $this->cc = $this->normalizeAddresses($cc);
97
        $this->bcc = $this->normalizeAddresses($bcc);
98
        $this->replyTo = $this->normalizeAddresses($replyTo);
99
        $this->subject = $subject;
100
        $this->tags = $tags;
101
        $this->metadata = $metadata;
102
        $this->using = Arr::wrap($using);
103
    }
104
 
105
    /**
106
     * Normalize the given array of addresses.
107
     *
108
     * @param  array  $addresses
109
     * @return array
110
     */
111
    protected function normalizeAddresses($addresses)
112
    {
113
        return collect($addresses)->map(function ($address) {
114
            return is_string($address) ? new Address($address) : $address;
115
        })->all();
116
    }
117
 
118
    /**
119
     * Specify who the message will be "from".
120
     *
121
     * @param  \Illuminate\Mail\Mailables\Address|string  $address
122
     * @param  string|null  $name
123
     * @return $this
124
     */
125
    public function from(Address|string $address, $name = null)
126
    {
127
        $this->from = is_string($address) ? new Address($address, $name) : $address;
128
 
129
        return $this;
130
    }
131
 
132
    /**
133
     * Add a "to" recipient to the message envelope.
134
     *
135
     * @param  \Illuminate\Mail\Mailables\Address|array|string  $address
136
     * @param  string|null  $name
137
     * @return $this
138
     */
139
    public function to(Address|array|string $address, $name = null)
140
    {
141
        $this->to = array_merge($this->to, $this->normalizeAddresses(
142
            is_string($name) ? [new Address($address, $name)] : Arr::wrap($address),
143
        ));
144
 
145
        return $this;
146
    }
147
 
148
    /**
149
     * Add a "cc" recipient to the message envelope.
150
     *
151
     * @param  \Illuminate\Mail\Mailables\Address|array|string  $address
152
     * @param  string|null  $name
153
     * @return $this
154
     */
155
    public function cc(Address|array|string $address, $name = null)
156
    {
157
        $this->cc = array_merge($this->cc, $this->normalizeAddresses(
158
            is_string($name) ? [new Address($address, $name)] : Arr::wrap($address),
159
        ));
160
 
161
        return $this;
162
    }
163
 
164
    /**
165
     * Add a "bcc" recipient to the message envelope.
166
     *
167
     * @param  \Illuminate\Mail\Mailables\Address|array|string  $address
168
     * @param  string|null  $name
169
     * @return $this
170
     */
171
    public function bcc(Address|array|string $address, $name = null)
172
    {
173
        $this->bcc = array_merge($this->bcc, $this->normalizeAddresses(
174
            is_string($name) ? [new Address($address, $name)] : Arr::wrap($address),
175
        ));
176
 
177
        return $this;
178
    }
179
 
180
    /**
181
     * Add a "reply to" recipient to the message envelope.
182
     *
183
     * @param  \Illuminate\Mail\Mailables\Address|array|string  $address
184
     * @param  string|null  $name
185
     * @return $this
186
     */
187
    public function replyTo(Address|array|string $address, $name = null)
188
    {
189
        $this->replyTo = array_merge($this->replyTo, $this->normalizeAddresses(
190
            is_string($name) ? [new Address($address, $name)] : Arr::wrap($address),
191
        ));
192
 
193
        return $this;
194
    }
195
 
196
    /**
197
     * Set the subject of the message.
198
     *
199
     * @param  string  $subject
200
     * @return $this
201
     */
202
    public function subject(string $subject)
203
    {
204
        $this->subject = $subject;
205
 
206
        return $this;
207
    }
208
 
209
    /**
210
     * Add "tags" to the message.
211
     *
212
     * @param  array  $tags
213
     * @return $this
214
     */
215
    public function tags(array $tags)
216
    {
217
        $this->tags = array_merge($this->tags, $tags);
218
 
219
        return $this;
220
    }
221
 
222
    /**
223
     * Add a "tag" to the message.
224
     *
225
     * @param  string  $tag
226
     * @return $this
227
     */
228
    public function tag(string $tag)
229
    {
230
        $this->tags[] = $tag;
231
 
232
        return $this;
233
    }
234
 
235
    /**
236
     * Add metadata to the message.
237
     *
238
     * @param  string  $key
239
     * @param  string|int  $value
240
     * @return $this
241
     */
242
    public function metadata(string $key, string|int $value)
243
    {
244
        $this->metadata[$key] = $value;
245
 
246
        return $this;
247
    }
248
 
249
    /**
250
     * Add a Symfony Message customization callback to the message.
251
     *
252
     * @param  \Closure  $callback
253
     * @return $this
254
     */
255
    public function using(Closure $callback)
256
    {
257
        $this->using[] = $callback;
258
 
259
        return $this;
260
    }
261
 
262
    /**
263
     * Determine if the message is from the given address.
264
     *
265
     * @param  string  $address
266
     * @param  string|null  $name
267
     * @return bool
268
     */
269
    public function isFrom(string $address, string $name = null)
270
    {
271
        if (is_null($name)) {
272
            return $this->from->address === $address;
273
        }
274
 
275
        return $this->from->address === $address &&
276
               $this->from->name === $name;
277
    }
278
 
279
    /**
280
     * Determine if the message has the given address as a recipient.
281
     *
282
     * @param  string  $address
283
     * @param  string|null  $name
284
     * @return bool
285
     */
286
    public function hasTo(string $address, string $name = null)
287
    {
288
        return $this->hasRecipient($this->to, $address, $name);
289
    }
290
 
291
    /**
292
     * Determine if the message has the given address as a "cc" recipient.
293
     *
294
     * @param  string  $address
295
     * @param  string|null  $name
296
     * @return bool
297
     */
298
    public function hasCc(string $address, string $name = null)
299
    {
300
        return $this->hasRecipient($this->cc, $address, $name);
301
    }
302
 
303
    /**
304
     * Determine if the message has the given address as a "bcc" recipient.
305
     *
306
     * @param  string  $address
307
     * @param  string|null  $name
308
     * @return bool
309
     */
310
    public function hasBcc(string $address, string $name = null)
311
    {
312
        return $this->hasRecipient($this->bcc, $address, $name);
313
    }
314
 
315
    /**
316
     * Determine if the message has the given address as a "reply to" recipient.
317
     *
318
     * @param  string  $address
319
     * @param  string|null  $name
320
     * @return bool
321
     */
322
    public function hasReplyTo(string $address, string $name = null)
323
    {
324
        return $this->hasRecipient($this->replyTo, $address, $name);
325
    }
326
 
327
    /**
328
     * Determine if the message has the given recipient.
329
     *
330
     * @param  array  $recipients
331
     * @param  string  $address
332
     * @param  string|null  $name
333
     * @return bool
334
     */
335
    protected function hasRecipient(array $recipients, string $address, ?string $name = null)
336
    {
337
        return collect($recipients)->contains(function ($recipient) use ($address, $name) {
338
            if (is_null($name)) {
339
                return $recipient->address === $address;
340
            }
341
 
342
            return $recipient->address === $address &&
343
                   $recipient->name === $name;
344
        });
345
    }
346
 
347
    /**
348
     * Determine if the message has the given subject.
349
     *
350
     * @param  string  $subject
351
     * @return bool
352
     */
353
    public function hasSubject(string $subject)
354
    {
355
        return $this->subject === $subject;
356
    }
357
 
358
    /**
359
     * Determine if the message has the given metadata.
360
     *
361
     * @param  string  $key
362
     * @param  string  $value
363
     * @return bool
364
     */
365
    public function hasMetadata(string $key, string $value)
366
    {
367
        return isset($this->metadata[$key]) && (string) $this->metadata[$key] === $value;
368
    }
369
}