-
-
Notifications
You must be signed in to change notification settings - Fork 252
/
Copy pathHeaders.php
76 lines (64 loc) · 1.87 KB
/
Headers.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<?php
declare(strict_types=1);
namespace Ddeboer\Imap\Message;
use Ddeboer\Imap\Exception\UnsupportedCharsetException;
final class Headers extends Parameters
{
public function __construct(\stdClass $headers)
{
parent::__construct();
// Store all headers as lowercase
$headers = \array_change_key_case((array) $headers);
foreach ($headers as $key => $value) {
try {
$this[$key] = $this->parseHeader($key, $value);
} catch (UnsupportedCharsetException) {
// safely skip header with unsupported charset
}
}
}
/**
* Get header.
*
* @return null|int|\stdClass[]|string
*/
public function get(string $key)
{
return parent::get(\strtolower($key));
}
/**
* Parse header.
*
* @param int|\stdClass[]|string $value
*
* @return int|\stdClass[]|string
*/
private function parseHeader(string $key, $value)
{
switch ($key) {
case 'msgno':
\assert(\is_string($value));
return (int) $value;
case 'from':
case 'to':
case 'cc':
case 'bcc':
case 'reply_to':
case 'sender':
case 'return_path':
\assert(\is_array($value));
foreach ($value as $address) {
if (isset($address->mailbox)) {
$address->host = $address->host ?? null;
$address->personal = isset($address->personal) ? $this->decode($address->personal) : null;
}
}
return $value;
case 'date':
case 'subject':
\assert(\is_string($value));
return $this->decode($value);
}
return $value;
}
}