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
|
|
|
namespace ZBateson\MailMimeParser\Header\Consumer; |
8
|
|
|
|
9
|
|
|
use ZBateson\MailMimeParser\Header\Part\AddressGroupPart; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Parses a single group of addresses (as a named-group part of an address |
13
|
|
|
* header). |
14
|
|
|
* |
15
|
|
|
* Finds addresses using its AddressConsumer sub-consumer separated by commas, |
16
|
|
|
* and ends processing once a semi-colon is found. |
17
|
|
|
* |
18
|
|
|
* Prior to returning to its calling client, AddressGroupConsumer constructs a |
19
|
|
|
* single Part\AddressGroupPart object filling it with all located addresses, and |
20
|
|
|
* returns it. |
21
|
|
|
* |
22
|
|
|
* The AddressGroupConsumer extends AddressBaseConsumer to define start/end |
23
|
|
|
* tokens, token separators, and construct a Part\AddressGroupPart for returning to |
24
|
|
|
* clients. |
25
|
|
|
* |
26
|
|
|
* @author Zaahid Bateson |
27
|
|
|
*/ |
28
|
|
|
class AddressGroupConsumer extends AddressBaseConsumer |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* Overridden to return patterns matching the beginning and end markers of a |
32
|
|
|
* group address: colon and semi-colon (":" and ";") characters. |
33
|
|
|
* |
34
|
|
|
* @return string[] the patterns |
35
|
|
|
*/ |
36
|
2 |
|
public function getTokenSeparators() |
37
|
|
|
{ |
38
|
2 |
|
return [':', ';']; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* AddressGroupConsumer returns true if the passed token is a semi-colon. |
43
|
|
|
* |
44
|
|
|
* @param string $token |
45
|
|
|
* @return boolean false |
46
|
|
|
*/ |
47
|
2 |
|
protected function isEndToken($token) |
48
|
|
|
{ |
49
|
2 |
|
return ($token === ';'); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* AddressGroupConsumer returns true if the passed token is a colon. |
54
|
|
|
* |
55
|
|
|
* @param string $token |
56
|
|
|
* @return boolean false |
57
|
|
|
*/ |
58
|
2 |
|
protected function isStartToken($token) |
59
|
|
|
{ |
60
|
2 |
|
return ($token === ':'); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Performs post-processing on parsed parts. |
65
|
|
|
* |
66
|
|
|
* AddressGroupConsumer returns an array with a single Part\AddressGroupPart |
67
|
|
|
* element with all email addresses from this and any sub-groups. |
68
|
|
|
* |
69
|
|
|
* @param \ZBateson\MailMimeParser\Header\Part\HeaderPart[] $parts |
70
|
|
|
* @return AddressGroupPart[]|array |
71
|
|
|
*/ |
72
|
2 |
|
protected function processParts(array $parts) |
73
|
|
|
{ |
74
|
2 |
|
$emails = []; |
75
|
2 |
|
foreach ($parts as $part) { |
76
|
2 |
|
if ($part instanceof AddressGroupPart) { |
77
|
1 |
|
$emails = array_merge($emails, $part->getAddresses()); |
78
|
1 |
|
continue; |
79
|
|
|
} |
80
|
2 |
|
$emails[] = $part; |
81
|
2 |
|
} |
82
|
2 |
|
$group = $this->partFactory->newAddressGroupPart($emails); |
83
|
2 |
|
return [$group]; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|