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 ZBateson\MbWrapper\MbWrapper; |
11
|
|
|
use ZBateson\MailMimeParser\Header\IHeaderPart; |
12
|
|
|
use ZBateson\MailMimeParser\ErrorBag; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Abstract base class representing a single part of a parsed header. |
16
|
|
|
* |
17
|
|
|
* @author Zaahid Bateson |
18
|
|
|
*/ |
19
|
|
|
abstract class HeaderPart extends ErrorBag implements IHeaderPart |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var ?string the value of the part |
23
|
|
|
*/ |
24
|
|
|
protected ?string $value; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var MbWrapper $charsetConverter the charset converter used for |
28
|
|
|
* converting strings in HeaderPart::convertEncoding |
29
|
|
|
*/ |
30
|
|
|
protected MbWrapper $charsetConverter; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Sets up dependencies. |
34
|
|
|
* |
35
|
|
|
*/ |
36
|
151 |
|
public function __construct(MbWrapper $charsetConverter) |
37
|
|
|
{ |
38
|
151 |
|
parent::__construct(); |
39
|
151 |
|
$this->charsetConverter = $charsetConverter; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Returns the part's value. |
44
|
|
|
* |
45
|
|
|
* @return ?string the value of the part |
46
|
|
|
*/ |
47
|
125 |
|
public function getValue() : ?string |
48
|
|
|
{ |
49
|
125 |
|
return $this->value; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Returns the value of the part (which is a string). |
54
|
|
|
* |
55
|
|
|
* @return string the value |
56
|
|
|
*/ |
57
|
1 |
|
public function __toString() : string |
58
|
|
|
{ |
59
|
1 |
|
return $this->value; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Returns true if spaces before this part should be ignored. True is only |
64
|
|
|
* returned for MimeLiterals if the part begins with a mime-encoded string, |
65
|
|
|
* Tokens if the Token's value is a single space, and for CommentParts. |
66
|
|
|
* |
67
|
|
|
*/ |
68
|
1 |
|
public function ignoreSpacesBefore() : bool |
69
|
|
|
{ |
70
|
1 |
|
return false; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Returns true if spaces after this part should be ignored. True is only |
75
|
|
|
* returned for MimeLiterals if the part ends with a mime-encoded string |
76
|
|
|
* Tokens if the Token's value is a single space, and for CommentParts. |
77
|
|
|
* |
78
|
|
|
*/ |
79
|
1 |
|
public function ignoreSpacesAfter() : bool |
80
|
|
|
{ |
81
|
1 |
|
return false; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Ensures the encoding of the passed string is set to UTF-8. |
86
|
|
|
* |
87
|
|
|
* The method does nothing if the passed $from charset is UTF-8 already, or |
88
|
|
|
* if $force is set to false and mb_check_encoding for $str returns true |
89
|
|
|
* for 'UTF-8'. |
90
|
|
|
* |
91
|
|
|
* @return string utf-8 string |
92
|
|
|
*/ |
93
|
138 |
|
protected function convertEncoding(string $str, string $from = 'ISO-8859-1', bool $force = false) : string |
94
|
|
|
{ |
95
|
138 |
|
if ($from !== 'UTF-8') { |
96
|
|
|
// mime header part decoding will force it. This is necessary for |
97
|
|
|
// UTF-7 because mb_check_encoding will return true |
98
|
138 |
|
if ($force || !($this->charsetConverter->checkEncoding($str, 'UTF-8'))) { |
99
|
72 |
|
return $this->charsetConverter->convert($str, $from, 'UTF-8'); |
100
|
|
|
} |
101
|
|
|
} |
102
|
128 |
|
return $str; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Default implementation returns an empty array. |
107
|
|
|
*/ |
108
|
|
|
protected function getErrorBagChildren() : array |
109
|
|
|
{ |
110
|
|
|
return []; |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|