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 Psr\Http\Message\StreamInterface; |
10
|
|
|
use ZBateson\MailMimeParser\Header\HeaderFactory; |
11
|
|
|
use ZBateson\MailMimeParser\Header\ParameterHeader; |
12
|
|
|
use ZBateson\MailMimeParser\Stream\StreamFactory; |
13
|
|
|
use ZBateson\MailMimeParser\Message\PartFilterFactory; |
14
|
|
|
use ZBateson\MailMimeParser\Header\HeaderContainer; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* A parent part containing headers. |
18
|
|
|
* |
19
|
|
|
* @author Zaahid Bateson |
20
|
|
|
*/ |
21
|
|
|
abstract class ParentHeaderPart extends ParentPart |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var HeaderContainer |
25
|
|
|
*/ |
26
|
|
|
protected $headerContainer; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param PartStreamFilterManager $partStreamFilterManager |
30
|
|
|
* @param StreamFactory $streamFactory |
31
|
|
|
* @param PartFilterFactory $partFilterFactory |
32
|
|
|
* @param PartBuilder $partBuilder |
33
|
|
|
* @param StreamInterface $stream |
34
|
|
|
* @param StreamInterface $contentStream |
35
|
|
|
*/ |
36
|
29 |
|
public function __construct( |
37
|
|
|
PartStreamFilterManager $partStreamFilterManager, |
38
|
|
|
StreamFactory $streamFactory, |
39
|
|
|
PartFilterFactory $partFilterFactory, |
40
|
|
|
PartBuilder $partBuilder, |
41
|
|
|
StreamInterface $stream = null, |
42
|
|
|
StreamInterface $contentStream = null |
43
|
|
|
) { |
44
|
29 |
|
parent::__construct( |
45
|
29 |
|
$partStreamFilterManager, |
46
|
29 |
|
$streamFactory, |
47
|
29 |
|
$partFilterFactory, |
48
|
29 |
|
$partBuilder, |
49
|
29 |
|
$stream, |
50
|
29 |
|
$contentStream |
51
|
|
|
); |
52
|
29 |
|
$this->headerContainer = $partBuilder->getHeaderContainer(); |
|
|
|
|
53
|
29 |
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Returns the AbstractHeader object for the header with the given $name |
57
|
|
|
* |
58
|
|
|
* Note that mime headers aren't case sensitive. |
59
|
|
|
* |
60
|
|
|
* @param string $name |
61
|
|
|
* @return \ZBateson\MailMimeParser\Header\AbstractHeader |
62
|
|
|
*/ |
63
|
18 |
|
public function getHeader($name, $offset = 0) |
64
|
|
|
{ |
65
|
18 |
|
return $this->headerContainer->get($name, $offset); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* |
70
|
|
|
* @param type $name |
|
|
|
|
71
|
|
|
*/ |
72
|
|
|
public function getAllHeadersByName($name) |
73
|
|
|
{ |
74
|
|
|
return $this->headerContainer->getAll($name); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Returns an array of all headers for the mime part with the first element |
79
|
|
|
* holding the name, and the second its value. |
80
|
|
|
* |
81
|
|
|
* @return string[][] |
82
|
|
|
*/ |
83
|
1 |
|
public function getRawHeaders() |
84
|
|
|
{ |
85
|
1 |
|
return $this->headerContainer->getHeaders(); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* |
90
|
|
|
* |
91
|
|
|
* @return \Iterator |
92
|
|
|
*/ |
93
|
|
|
public function getRawHeaderIterator() |
94
|
|
|
{ |
95
|
|
|
return $this->headerContainer->getIterator(); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Returns the string value for the header with the given $name. |
100
|
|
|
* |
101
|
|
|
* Note that mime headers aren't case sensitive. |
102
|
|
|
* |
103
|
|
|
* @param string $name |
104
|
|
|
* @param string $defaultValue |
105
|
|
|
* @return string |
106
|
|
|
*/ |
107
|
14 |
|
public function getHeaderValue($name, $defaultValue = null) |
108
|
|
|
{ |
109
|
14 |
|
$header = $this->getHeader($name); |
110
|
14 |
|
if ($header !== null) { |
111
|
13 |
|
return $header->getValue(); |
112
|
|
|
} |
113
|
1 |
|
return $defaultValue; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Returns a parameter of the header $header, given the parameter named |
118
|
|
|
* $param. |
119
|
|
|
* |
120
|
|
|
* Only headers of type |
121
|
|
|
* \ZBateson\MailMimeParser\Header\ParameterHeader have parameters. |
122
|
|
|
* Content-Type and Content-Disposition are examples of headers with |
123
|
|
|
* parameters. "Charset" is a common parameter of Content-Type. |
124
|
|
|
* |
125
|
|
|
* @param string $header |
126
|
|
|
* @param string $param |
127
|
|
|
* @param string $defaultValue |
128
|
|
|
* @return string |
129
|
|
|
*/ |
130
|
15 |
|
public function getHeaderParameter($header, $param, $defaultValue = null) |
131
|
|
|
{ |
132
|
15 |
|
$obj = $this->getHeader($header); |
133
|
15 |
|
if ($obj && $obj instanceof ParameterHeader) { |
134
|
13 |
|
return $obj->getValueFor($param, $defaultValue); |
135
|
|
|
} |
136
|
2 |
|
return $defaultValue; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Adds a header with the given $name and $value. |
141
|
|
|
* |
142
|
|
|
* Creates a new \ZBateson\MailMimeParser\Header\AbstractHeader object and |
143
|
|
|
* registers it as a header. |
144
|
|
|
* |
145
|
|
|
* @param string $name |
146
|
|
|
* @param string $value |
147
|
|
|
*/ |
148
|
1 |
|
public function setRawHeader($name, $value, $offset = 0) |
149
|
|
|
{ |
150
|
1 |
|
$this->headerContainer->set($name, $value, $offset); |
151
|
1 |
|
$this->onChange(); |
152
|
1 |
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Adds a header with the given $name and $value. |
156
|
|
|
* |
157
|
|
|
* Creates a new \ZBateson\MailMimeParser\Header\AbstractHeader object and |
158
|
|
|
* registers it as a header. |
159
|
|
|
* |
160
|
|
|
* @param string $name |
161
|
|
|
* @param string $value |
162
|
|
|
*/ |
163
|
1 |
|
public function addRawHeader($name, $value) |
164
|
|
|
{ |
165
|
1 |
|
$this->headerContainer->add($name, $value); |
166
|
1 |
|
$this->onChange(); |
167
|
1 |
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Removes the header with the given name |
171
|
|
|
* |
172
|
|
|
* @param string $name |
173
|
|
|
*/ |
174
|
1 |
|
public function removeHeader($name) |
175
|
|
|
{ |
176
|
1 |
|
$this->headerContainer->removeAll($name); |
177
|
1 |
|
$this->onChange(); |
178
|
1 |
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Removes the header with the given name |
182
|
|
|
* |
183
|
|
|
* @param string $name |
184
|
|
|
*/ |
185
|
1 |
|
public function removeSingleHeader($name, $offset = 0) |
186
|
|
|
{ |
187
|
1 |
|
$this->headerContainer->remove($name, $offset); |
188
|
1 |
|
$this->onChange(); |
189
|
1 |
|
} |
190
|
|
|
} |
191
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..