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