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 Psr\Log\LoggerInterface; |
11
|
|
|
use ZBateson\MailMimeParser\Header\IHeaderPart; |
12
|
|
|
use ZBateson\MbWrapper\MbWrapper; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Constructs and returns IHeaderPart objects. |
16
|
|
|
* |
17
|
|
|
* @author Zaahid Bateson |
18
|
|
|
*/ |
19
|
|
|
class HeaderPartFactory |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var MbWrapper $charsetConverter passed to IHeaderPart constructors |
23
|
|
|
* for converting strings in IHeaderPart::convertEncoding |
24
|
|
|
*/ |
25
|
|
|
protected MbWrapper $charsetConverter; |
26
|
|
|
|
27
|
|
|
protected LoggerInterface $logger; |
28
|
|
|
|
29
|
|
|
public function __construct(LoggerInterface $logger, MbWrapper $charsetConverter) |
30
|
12 |
|
{ |
31
|
|
|
$this->logger = $logger; |
32
|
12 |
|
$this->charsetConverter = $charsetConverter; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Creates and returns a default IHeaderPart for this factory, allowing |
37
|
|
|
* subclass factories for specialized IHeaderParts. |
38
|
|
|
* |
39
|
|
|
* The default implementation returns a new Token |
40
|
|
|
*/ |
41
|
96 |
|
public function newInstance(string $value) : IHeaderPart |
42
|
|
|
{ |
43
|
96 |
|
return $this->newToken($value); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Initializes and returns a new Token. |
48
|
|
|
*/ |
49
|
107 |
|
public function newToken(string $value, bool $isLiteral = false, bool $preserveSpaces = false) : Token |
50
|
|
|
{ |
51
|
107 |
|
return new Token($this->logger, $this->charsetConverter, $value, $isLiteral, $preserveSpaces); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Initializes and returns a new SubjectToken. |
56
|
|
|
*/ |
57
|
2 |
|
public function newSubjectToken(string $value) : SubjectToken |
58
|
|
|
{ |
59
|
2 |
|
return new SubjectToken($this->logger, $this->charsetConverter, $value); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Initializes and returns a new MimeToken. |
64
|
|
|
*/ |
65
|
106 |
|
public function newMimeToken(string $value) : MimeToken |
66
|
|
|
{ |
67
|
106 |
|
return new MimeToken($this->logger, $this->charsetConverter, $value); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Initializes and returns a new ContainerPart. |
72
|
|
|
* |
73
|
106 |
|
* @param HeaderPart[] $children |
74
|
|
|
*/ |
75
|
106 |
|
public function newContainerPart(array $children) : ContainerPart |
76
|
|
|
{ |
77
|
|
|
return new ContainerPart($this->logger, $this->charsetConverter, $children); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
16 |
|
* Instantiates and returns a SplitParameterPart. |
82
|
|
|
* |
83
|
16 |
|
* @param ParameterPart[] $children |
84
|
|
|
*/ |
85
|
|
|
public function newSplitParameterPart(array $children) : SplitParameterPart |
86
|
|
|
{ |
87
|
|
|
return new SplitParameterPart($this->logger, $this->charsetConverter, $this, $children); |
88
|
|
|
} |
89
|
96 |
|
|
90
|
|
|
/** |
91
|
96 |
|
* Initializes and returns a new QuotedLiteralPart. |
92
|
|
|
* |
93
|
|
|
* @param HeaderPart[] $children |
94
|
|
|
*/ |
95
|
|
|
public function newQuotedLiteralPart(array $parts) : QuotedLiteralPart |
96
|
|
|
{ |
97
|
|
|
return new QuotedLiteralPart($this->logger, $this->charsetConverter, $parts); |
98
|
|
|
} |
99
|
1 |
|
|
100
|
|
|
/** |
101
|
1 |
|
* Initializes and returns a new CommentPart. |
102
|
|
|
* |
103
|
|
|
* @param HeaderPart[] $children |
104
|
|
|
*/ |
105
|
|
|
public function newCommentPart(array $children) : CommentPart |
106
|
|
|
{ |
107
|
20 |
|
return new CommentPart($this->logger, $this->charsetConverter, $this, $children); |
108
|
|
|
} |
109
|
20 |
|
|
110
|
|
|
/** |
111
|
|
|
* Initializes and returns a new AddressPart. |
112
|
|
|
* |
113
|
|
|
* @param HeaderPart[] $nameParts |
114
|
|
|
* @param HeaderPart[] $emailParts |
115
|
101 |
|
*/ |
116
|
|
|
public function newAddress(array $nameParts, array $emailParts) : AddressPart |
117
|
101 |
|
{ |
118
|
|
|
return new AddressPart($this->logger, $this->charsetConverter, $nameParts, $emailParts); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Initializes and returns a new AddressGroupPart |
123
|
1 |
|
* |
124
|
|
|
* @param HeaderPart[] $nameParts |
125
|
1 |
|
* @param AddressPart[]|AddressGroupPart[] $addressesAndGroups |
126
|
|
|
*/ |
127
|
|
|
public function newAddressGroupPart(array $nameParts, array $addressesAndGroups) : AddressGroupPart |
128
|
|
|
{ |
129
|
|
|
return new AddressGroupPart($this->logger, $this->charsetConverter, $nameParts, $addressesAndGroups); |
130
|
|
|
} |
131
|
1 |
|
|
132
|
|
|
/** |
133
|
|
|
* Initializes and returns a new DatePart |
134
|
|
|
* |
135
|
|
|
* @param HeaderPart[] $children |
136
|
|
|
*/ |
137
|
|
|
public function newDatePart(array $children) : DatePart |
138
|
1 |
|
{ |
139
|
1 |
|
return new DatePart($this->logger, $this->charsetConverter, $children); |
140
|
1 |
|
} |
141
|
1 |
|
|
142
|
1 |
|
/** |
143
|
1 |
|
* Initializes and returns a new ParameterPart. |
144
|
1 |
|
* |
145
|
1 |
|
* @param HeaderPart[] $nameParts |
146
|
|
|
*/ |
147
|
|
|
public function newParameterPart(array $nameParts, ContainerPart $valuePart) : ParameterPart |
148
|
|
|
{ |
149
|
|
|
return new ParameterPart($this->logger, $this->charsetConverter, $nameParts, $valuePart); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Initializes and returns a new ReceivedPart. |
154
|
|
|
* |
155
|
|
|
* @param HeaderPart[] $children |
156
|
|
|
*/ |
157
|
|
|
public function newReceivedPart(string $name, array $children) : ReceivedPart |
158
|
|
|
{ |
159
|
|
|
return new ReceivedPart($this->logger, $this->charsetConverter, $name, $children); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Initializes and returns a new ReceivedDomainPart. |
164
|
|
|
* |
165
|
|
|
* @param HeaderPart[] $children |
166
|
|
|
*/ |
167
|
|
|
public function newReceivedDomainPart(string $name, array $children) : ReceivedDomainPart |
168
|
|
|
{ |
169
|
|
|
return new ReceivedDomainPart( |
170
|
|
|
$this->logger, |
171
|
|
|
$this->charsetConverter, |
172
|
|
|
$name, |
173
|
|
|
$children |
174
|
|
|
); |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
|