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\LogLevel; |
||
11 | |||
12 | /** |
||
13 | * Holds a single address or name/address pair. |
||
14 | * |
||
15 | * The name part of the address may be mime-encoded, but the email address part |
||
16 | * can't be mime-encoded. Any whitespace in the email address part is stripped |
||
17 | * out. |
||
18 | * |
||
19 | * A convenience method, getEmail, is provided for clarity -- but getValue |
||
20 | * returns the email address as well. |
||
21 | * |
||
22 | * @author Zaahid Bateson |
||
23 | */ |
||
24 | class AddressPart extends NameValuePart |
||
25 | { |
||
26 | 99 | protected function getValueFromParts(array $parts) : string |
|
27 | { |
||
28 | 99 | return \implode('', \array_map( |
|
29 | 99 | function($p) { |
|
0 ignored issues
–
show
Coding Style
introduced
by
![]() |
|||
30 | 99 | if ($p instanceof AddressPart) { |
|
31 | 95 | return $p->getValue(); |
|
32 | 99 | } elseif ($p instanceof QuotedLiteralPart && $p->getValue() !== '') { |
|
33 | 2 | return '"' . \preg_replace('/(["\\\])/', '\\\$1', $p->getValue()) . '"'; |
|
34 | } |
||
35 | 99 | return \preg_replace('/\s+/', '', $p->getValue()); |
|
36 | 99 | }, |
|
37 | 99 | $parts |
|
38 | 99 | )); |
|
39 | } |
||
40 | |||
41 | /** |
||
42 | * Returns the email address. |
||
43 | * |
||
44 | * @return string The email address. |
||
45 | */ |
||
46 | 3 | public function getEmail() : string |
|
47 | { |
||
48 | 3 | return $this->value; |
|
49 | } |
||
50 | |||
51 | 1 | protected function validate() : void |
|
52 | { |
||
53 | 1 | if (empty($this->value)) { |
|
54 | 1 | $this->addError('Address doesn\'t contain an email address', LogLevel::ERROR); |
|
55 | } |
||
56 | } |
||
57 | } |
||
58 |