1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace FH\Bundle\MailerBundle\Email; |
6
|
|
|
|
7
|
|
|
use FH\Bundle\MailerBundle\Exception\InvalidArgumentException; |
8
|
|
|
use Symfony\Component\Mime\Address; |
9
|
|
|
|
10
|
|
|
final class Participants |
11
|
|
|
{ |
12
|
|
|
/** @var Address|null */ |
13
|
|
|
private $sender; |
14
|
|
|
|
15
|
|
|
/** @var array */ |
16
|
|
|
private $from; |
17
|
|
|
|
18
|
|
|
/** @var array */ |
19
|
|
|
private $replyTo; |
20
|
|
|
|
21
|
|
|
/** @var array */ |
22
|
|
|
private $to; |
23
|
|
|
|
24
|
|
|
/** @var array */ |
25
|
|
|
private $cc; |
26
|
|
|
|
27
|
|
|
/** @var array */ |
28
|
|
|
private $bcc; |
29
|
|
|
|
30
|
1 |
|
public static function fromArray(array $participants): self |
31
|
|
|
{ |
32
|
1 |
|
$createAddress = static function (array $address) { |
33
|
1 |
|
return self::createAddress($address); |
34
|
1 |
|
}; |
35
|
|
|
|
36
|
1 |
|
return new self( |
37
|
1 |
|
isset($participants['sender']) && \is_array($participants['sender']) ? self::createAddress($participants['sender']) : null, |
38
|
1 |
|
array_map($createAddress, $participants['from'] ?? []), |
39
|
1 |
|
array_map($createAddress, $participants['reply_to'] ?? []), |
40
|
1 |
|
array_map($createAddress, $participants['to'] ?? []), |
41
|
1 |
|
array_map($createAddress, $participants['cc'] ?? []), |
42
|
1 |
|
array_map($createAddress, $participants['bcc'] ?? []) |
43
|
|
|
); |
44
|
|
|
} |
45
|
|
|
|
46
|
1 |
|
public function __construct( |
47
|
|
|
Address $sender = null, |
48
|
|
|
array $from = [], |
49
|
|
|
array $replyTo = [], |
50
|
|
|
array $to = [], |
51
|
|
|
array $cc = [], |
52
|
|
|
array $bcc = [] |
53
|
|
|
) { |
54
|
1 |
|
$this->sender = $sender; |
55
|
1 |
|
$this->from = $from; |
56
|
1 |
|
$this->replyTo = $replyTo; |
57
|
1 |
|
$this->to = $to; |
58
|
1 |
|
$this->cc = $cc; |
59
|
1 |
|
$this->bcc = $bcc; |
60
|
1 |
|
} |
61
|
|
|
|
62
|
1 |
|
public function getSender(): Address |
63
|
|
|
{ |
64
|
1 |
|
if (!$this->sender instanceof Address) { |
65
|
|
|
throw new InvalidArgumentException('Invalid sender'); |
66
|
|
|
} |
67
|
|
|
|
68
|
1 |
|
return $this->sender; |
69
|
|
|
} |
70
|
|
|
|
71
|
1 |
|
public function hasSender(): bool |
72
|
|
|
{ |
73
|
1 |
|
return $this->sender instanceof Address; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @return Address[] |
78
|
|
|
*/ |
79
|
1 |
|
public function getFrom(): array |
80
|
|
|
{ |
81
|
1 |
|
return $this->from; |
82
|
|
|
} |
83
|
|
|
|
84
|
1 |
|
public function hasFrom(): bool |
85
|
|
|
{ |
86
|
1 |
|
return !empty($this->from); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @return Address[] |
91
|
|
|
*/ |
92
|
1 |
|
public function getReplyTo(): array |
93
|
|
|
{ |
94
|
1 |
|
return $this->replyTo; |
95
|
|
|
} |
96
|
|
|
|
97
|
1 |
|
public function hasReplyTo(): bool |
98
|
|
|
{ |
99
|
1 |
|
return !empty($this->replyTo); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @return Address[] |
104
|
|
|
*/ |
105
|
1 |
|
public function getTo(): array |
106
|
|
|
{ |
107
|
1 |
|
return $this->to; |
108
|
|
|
} |
109
|
|
|
|
110
|
1 |
|
public function hasTo(): bool |
111
|
|
|
{ |
112
|
1 |
|
return !empty($this->to); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @return Address[] |
117
|
|
|
*/ |
118
|
1 |
|
public function getCc(): array |
119
|
|
|
{ |
120
|
1 |
|
return $this->cc; |
121
|
|
|
} |
122
|
|
|
|
123
|
1 |
|
public function hasCc(): bool |
124
|
|
|
{ |
125
|
1 |
|
return !empty($this->cc); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @return Address[] |
130
|
|
|
*/ |
131
|
1 |
|
public function getBcc(): array |
132
|
|
|
{ |
133
|
1 |
|
return $this->bcc; |
134
|
|
|
} |
135
|
|
|
|
136
|
1 |
|
public function hasBcc(): bool |
137
|
|
|
{ |
138
|
1 |
|
return !empty($this->bcc); |
139
|
|
|
} |
140
|
|
|
|
141
|
1 |
|
private static function createAddress(array $address): Address |
142
|
|
|
{ |
143
|
1 |
|
return new Address($address['address'], $address['name'] ?? ''); |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|