1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the ZBateson\MailMimeParser project. |
4
|
|
|
* |
5
|
|
|
* @license http://opensource.org/licenses/bsd-license.php BSD |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace ZBateson\MailMimeParser\Header\Part; |
9
|
|
|
|
10
|
|
|
use Psr\Log\LoggerInterface; |
11
|
|
|
use Psr\Log\LogLevel; |
12
|
|
|
use ZBateson\MbWrapper\MbWrapper; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Holds a group of addresses, and an optional group name. |
16
|
|
|
* |
17
|
|
|
* Because AddressGroupConsumer is only called once a colon (":") character is |
18
|
|
|
* found, an AddressGroupPart is initially constructed without a $name. Once it |
19
|
|
|
* is returned to AddressConsumer, a new AddressGroupPart is created out of |
20
|
|
|
* AddressGroupConsumer's AddressGroupPart. |
21
|
|
|
* |
22
|
|
|
* @author Zaahid Bateson |
23
|
|
|
*/ |
24
|
|
|
class AddressGroupPart extends NameValuePart |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var AddressPart[] an array of AddressParts |
28
|
|
|
*/ |
29
|
|
|
protected array $addresses; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Creates an AddressGroupPart out of the passed array of AddressParts/ |
33
|
|
|
* AddressGroupParts and name. |
34
|
|
|
* |
35
|
|
|
* @param HeaderPart[] $nameParts |
36
|
|
|
* @param AddressPart[]|AddressGroupPart[] $addressesAndGroupParts |
37
|
|
|
*/ |
38
|
2 |
|
public function __construct( |
39
|
|
|
LoggerInterface $logger, |
40
|
|
|
MbWrapper $charsetConverter, |
41
|
|
|
array $nameParts, |
42
|
|
|
array $addressesAndGroupParts |
43
|
|
|
) { |
44
|
2 |
|
parent::__construct( |
45
|
2 |
|
$logger, |
46
|
2 |
|
$charsetConverter, |
47
|
2 |
|
$nameParts, |
48
|
2 |
|
$addressesAndGroupParts |
49
|
2 |
|
); |
50
|
2 |
|
$this->addresses = \array_merge(...\array_map( |
51
|
2 |
|
fn ($p) => ($p instanceof AddressGroupPart) ? $p->getAddresses() : [$p], |
52
|
2 |
|
$addressesAndGroupParts |
53
|
2 |
|
)); |
54
|
|
|
// for backwards compatibility |
55
|
2 |
|
$this->value = $this->name; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Return the AddressGroupPart's array of addresses. |
60
|
|
|
* |
61
|
|
|
* @return AddressPart[] An array of address parts. |
62
|
|
|
*/ |
63
|
1 |
|
public function getAddresses() : array |
64
|
|
|
{ |
65
|
1 |
|
return $this->addresses; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Returns the AddressPart at the passed index or null. |
70
|
|
|
* |
71
|
|
|
* @param int $index The 0-based index. |
72
|
|
|
* @return ?AddressPart The address. |
73
|
|
|
*/ |
74
|
1 |
|
public function getAddress(int $index) : ?AddressPart |
75
|
|
|
{ |
76
|
1 |
|
if (!isset($this->addresses[$index])) { |
77
|
1 |
|
return null; |
78
|
|
|
} |
79
|
1 |
|
return $this->addresses[$index]; |
80
|
|
|
} |
81
|
|
|
|
82
|
1 |
|
protected function validate() : void |
83
|
|
|
{ |
84
|
1 |
|
if ($this->name === null || \mb_strlen($this->name) === 0) { |
85
|
1 |
|
$this->addError('Address group doesn\'t have a name', LogLevel::ERROR); |
86
|
|
|
} |
87
|
1 |
|
if (empty($this->addresses)) { |
88
|
1 |
|
$this->addError('Address group doesn\'t have any email addresses defined in it', LogLevel::NOTICE); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|