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; |
9
|
|
|
|
10
|
|
|
use ZBateson\MailMimeParser\Header\Consumer\AbstractConsumer; |
11
|
|
|
use ZBateson\MailMimeParser\Header\Consumer\ConsumerService; |
12
|
|
|
use ZBateson\MailMimeParser\Header\Part\AddressGroupPart; |
13
|
|
|
use ZBateson\MailMimeParser\Header\Part\AddressPart; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* A header containing one or more email addresses and/or groups of addresses. |
17
|
|
|
* |
18
|
|
|
* An address is separated by a comma, and each group separated by a semi-colon. |
19
|
|
|
* The AddressHeader provides a complete list of all addresses referenced in a |
20
|
|
|
* header including any addresses in groups, in addition to being able to access |
21
|
|
|
* the groups separately if needed. |
22
|
|
|
* |
23
|
|
|
* For full specifications, see {@link https://www.ietf.org/rfc/rfc2822.txt} |
24
|
|
|
* |
25
|
|
|
* @author Zaahid Bateson |
26
|
|
|
*/ |
27
|
|
|
class AddressHeader extends AbstractHeader |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* @var AddressPart[] array of addresses, included all addresses contained |
31
|
|
|
* in groups. |
32
|
|
|
*/ |
33
|
|
|
protected $addresses = []; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var AddressGroupPart[] array of address groups (lists). |
37
|
|
|
*/ |
38
|
|
|
protected $groups = []; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Returns an AddressBaseConsumer. |
42
|
|
|
* |
43
|
|
|
* @return \ZBateson\MailMimeParser\Header\Consumer\AbstractConsumer |
44
|
|
|
*/ |
45
|
106 |
|
protected function getConsumer(ConsumerService $consumerService) |
46
|
|
|
{ |
47
|
106 |
|
return $consumerService->getAddressBaseConsumer(); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Overridden to extract all addresses into addresses array. |
52
|
|
|
* |
53
|
|
|
* @return static |
54
|
|
|
*/ |
55
|
106 |
|
protected function setParseHeaderValue(AbstractConsumer $consumer) |
56
|
|
|
{ |
57
|
106 |
|
parent::setParseHeaderValue($consumer); |
58
|
106 |
|
foreach ($this->parts as $part) { |
59
|
105 |
|
if ($part instanceof AddressPart) { |
60
|
104 |
|
$this->addresses[] = $part; |
61
|
4 |
|
} elseif ($part instanceof AddressGroupPart) { |
62
|
4 |
|
$this->addresses = \array_merge($this->addresses, $part->getAddresses()); |
63
|
4 |
|
$this->groups[] = $part; |
64
|
|
|
} |
65
|
|
|
} |
66
|
106 |
|
return $this; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Returns all address parts in the header including any addresses that are |
71
|
|
|
* in groups (lists). |
72
|
|
|
* |
73
|
|
|
* @return AddressPart[] The addresses. |
74
|
|
|
*/ |
75
|
1 |
|
public function getAddresses() : array |
76
|
|
|
{ |
77
|
1 |
|
return $this->addresses; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Returns all group parts (lists) in the header. |
82
|
|
|
* |
83
|
|
|
* @return AddressGroupPart[] |
84
|
|
|
*/ |
85
|
1 |
|
public function getGroups() : array |
86
|
|
|
{ |
87
|
1 |
|
return $this->groups; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Returns true if an address exists with the passed email address. |
92
|
|
|
* |
93
|
|
|
* Comparison is done case insensitively. |
94
|
|
|
* |
95
|
|
|
*/ |
96
|
1 |
|
public function hasAddress(string $email) : bool |
97
|
|
|
{ |
98
|
1 |
|
foreach ($this->addresses as $addr) { |
99
|
1 |
|
if (\strcasecmp($addr->getEmail(), $email) === 0) { |
100
|
1 |
|
return true; |
101
|
|
|
} |
102
|
|
|
} |
103
|
1 |
|
return false; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Returns the first email address in the header. |
108
|
|
|
* |
109
|
|
|
* @return string The email address |
110
|
|
|
*/ |
111
|
|
|
public function getEmail() : string |
112
|
|
|
{ |
113
|
|
|
return $this->getValue(); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Returns the name associated with the first email address to complement |
118
|
|
|
* getValue()/getEmail() if one is set, or null if not. |
119
|
|
|
* |
120
|
|
|
* @return string|null The person name. |
121
|
|
|
*/ |
122
|
97 |
|
public function getPersonName() : ?string |
123
|
|
|
{ |
124
|
97 |
|
if (!empty($this->parts)) { |
125
|
96 |
|
return $this->parts[0]->getName(); |
|
|
|
|
126
|
|
|
} |
127
|
1 |
|
return null; |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|