blob: f76fec3e014007e4ad839338755bbe87ce612263 [file] [log] [blame]
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01001<?php
2
3declare(strict_types=1);
4
5namespace Ddeboer\Imap\Message;
6
7/**
8 * Collection of message headers.
9 */
10final class Headers extends Parameters
11{
12 /**
13 * Constructor.
14 */
15 public function __construct(\stdClass $headers)
16 {
17 parent::__construct();
18
19 // Store all headers as lowercase
20 $headers = \array_change_key_case((array) $headers);
21
22 foreach ($headers as $key => $value) {
23 $this[$key] = $this->parseHeader($key, $value);
24 }
25 }
26
27 /**
28 * Get header.
29 *
30 * @return mixed
31 */
32 public function get(string $key)
33 {
34 return parent::get(\strtolower($key));
35 }
36
37 /**
38 * Parse header.
39 *
40 * @param mixed $value
41 *
42 * @return mixed
43 */
44 private function parseHeader(string $key, $value)
45 {
46 switch ($key) {
47 case 'msgno':
48 return (int) $value;
49 case 'from':
50 case 'to':
51 case 'cc':
52 case 'bcc':
53 case 'reply_to':
54 case 'sender':
55 case 'return_path':
56 /** @var \stdClass $address */
57 foreach ($value as $address) {
58 if (isset($address->mailbox)) {
59 $address->host = $address->host ?? null;
60 $address->personal = isset($address->personal) ? $this->decode($address->personal) : null;
61 }
62 }
63
64 return $value;
65 case 'date':
66 case 'subject':
67 return $this->decode($value);
68 }
69
70 return $value;
71 }
72}