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\Message\Part; |
8
|
|
|
|
9
|
|
|
use ZBateson\MailMimeParser\Header\HeaderFactory; |
10
|
|
|
use ZBateson\MailMimeParser\Header\ParameterHeader; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Header related methods attached to a mime part. |
14
|
|
|
* |
15
|
|
|
* @author Zaahid Bateson |
16
|
|
|
*/ |
17
|
|
|
trait MimePartHeaderTrait |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var HeaderFactory the HeaderFactory object used for created headers |
21
|
|
|
*/ |
22
|
|
|
protected $headerFactory; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var string[][] array of headers, with keys set to lower-cased, |
26
|
|
|
* alphanumeric characters of the header's name, and values set to an |
27
|
|
|
* array of 2 elements, the first being the header's original name with |
28
|
|
|
* non-alphanumeric characters and original case, and the second set to |
29
|
|
|
* the header's value. |
30
|
|
|
*/ |
31
|
|
|
protected $rawHeaders; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var \ZBateson\MailMimeParser\Header\AbstractHeader[] array of parsed |
35
|
|
|
* header objects populated on-demand, the key is set to the header's name |
36
|
|
|
* lower-cased, and with non-alphanumeric characters removed. |
37
|
|
|
*/ |
38
|
|
|
protected $headers; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param HeaderFactory $headerFactory |
42
|
|
|
*/ |
43
|
24 |
|
public function __construct(HeaderFactory $headerFactory, PartBuilder $partBuilder) { |
44
|
24 |
|
$this->headerFactory = $headerFactory; |
45
|
24 |
|
$this->headers['contenttype'] = $partBuilder->getContentType(); |
46
|
24 |
|
$this->rawHeaders = $partBuilder->getRawHeaders(); |
47
|
24 |
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Returns the AbstractHeader object for the header with the given $name |
51
|
|
|
* |
52
|
|
|
* Note that mime headers aren't case sensitive. |
53
|
|
|
* |
54
|
|
|
* @param string $name |
55
|
|
|
* @return \ZBateson\MailMimeParser\Header\AbstractHeader |
56
|
|
|
*/ |
57
|
16 |
|
public function getHeader($name) |
58
|
|
|
{ |
59
|
16 |
|
$nameKey = preg_replace('/[^a-z0-9]/', '', strtolower($name)); |
60
|
16 |
|
if (isset($this->rawHeaders[$nameKey])) { |
61
|
15 |
|
if (!isset($this->headers[$nameKey])) { |
62
|
14 |
|
$this->headers[$nameKey] = $this->headerFactory->newInstance( |
63
|
14 |
|
$this->rawHeaders[$nameKey][0], |
64
|
14 |
|
$this->rawHeaders[$nameKey][1] |
65
|
|
|
); |
66
|
|
|
} |
67
|
15 |
|
return $this->headers[$nameKey]; |
68
|
|
|
} |
69
|
1 |
|
return null; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Returns the string value for the header with the given $name. |
74
|
|
|
* |
75
|
|
|
* Note that mime headers aren't case sensitive. |
76
|
|
|
* |
77
|
|
|
* @param string $name |
78
|
|
|
* @param string $defaultValue |
79
|
|
|
* @return string |
80
|
|
|
*/ |
81
|
15 |
|
public function getHeaderValue($name, $defaultValue = null) |
82
|
|
|
{ |
83
|
15 |
|
$header = $this->getHeader($name); |
84
|
15 |
|
if ($header !== null) { |
85
|
14 |
|
return $header->getValue(); |
86
|
|
|
} |
87
|
1 |
|
return $defaultValue; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Returns a parameter of the header $header, given the parameter named |
92
|
|
|
* $param. |
93
|
|
|
* |
94
|
|
|
* Only headers of type |
95
|
|
|
* \ZBateson\MailMimeParser\Header\ParameterHeader have parameters. |
96
|
|
|
* Content-Type and Content-Disposition are examples of headers with |
97
|
|
|
* parameters. "Charset" is a common parameter of Content-Type. |
98
|
|
|
* |
99
|
|
|
* @param string $header |
100
|
|
|
* @param string $param |
101
|
|
|
* @param string $defaultValue |
102
|
|
|
* @return string |
103
|
|
|
*/ |
104
|
14 |
|
public function getHeaderParameter($header, $param, $defaultValue = null) |
105
|
|
|
{ |
106
|
14 |
|
$obj = $this->getHeader($header); |
107
|
14 |
|
if ($obj && $obj instanceof ParameterHeader) { |
108
|
13 |
|
return $obj->getValueFor($param, $defaultValue); |
109
|
|
|
} |
110
|
1 |
|
return $defaultValue; |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|