|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace Genkgo\Mail; |
|
5
|
|
|
|
|
6
|
|
|
use Genkgo\Mail\Header\HeaderLine; |
|
7
|
|
|
use Genkgo\Mail\Header\MimeVersion; |
|
8
|
|
|
use Genkgo\Mail\Stream\EmptyStream; |
|
9
|
|
|
use Genkgo\Mail\Stream\MessageStream; |
|
10
|
|
|
use Genkgo\Mail\Stream\StringStream; |
|
11
|
|
|
|
|
12
|
|
|
final class GenericMessage implements MessageInterface |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @var array<string, array<int, HeaderInterface>> |
|
16
|
|
|
*/ |
|
17
|
|
|
private $headers = [ |
|
18
|
|
|
'return-path' => [], |
|
19
|
|
|
'received' => [], |
|
20
|
|
|
'dkim-signature' => [], |
|
21
|
|
|
'domainkey-signature' => [], |
|
22
|
|
|
'sender' => [], |
|
23
|
|
|
'message-id' => [], |
|
24
|
|
|
'date' => [], |
|
25
|
|
|
'subject' => [], |
|
26
|
|
|
'from' => [], |
|
27
|
|
|
'reply-to' => [], |
|
28
|
|
|
'to' => [], |
|
29
|
|
|
'cc' => [], |
|
30
|
|
|
'bcc' => [], |
|
31
|
|
|
'mime-version' => [], |
|
32
|
|
|
'content-type' => [], |
|
33
|
|
|
'content-transfer-encoding' => [], |
|
34
|
|
|
]; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var StreamInterface |
|
38
|
|
|
*/ |
|
39
|
|
|
private $body; |
|
40
|
|
|
|
|
41
|
132 |
|
public function __construct() |
|
42
|
|
|
{ |
|
43
|
132 |
|
$this->body = new EmptyStream(); |
|
44
|
132 |
|
$this->headers['mime-version'] = [new MimeVersion()]; |
|
45
|
132 |
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @return iterable<iterable<HeaderInterface>> |
|
|
|
|
|
|
49
|
|
|
*/ |
|
50
|
76 |
|
public function getHeaders(): iterable |
|
51
|
|
|
{ |
|
52
|
76 |
|
return $this->headers; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @param string $name |
|
57
|
|
|
* @return bool |
|
58
|
|
|
*/ |
|
59
|
34 |
|
public function hasHeader(string $name): bool |
|
60
|
|
|
{ |
|
61
|
34 |
|
$name = \strtolower($name); |
|
62
|
34 |
|
return isset($this->headers[$name]) && $this->headers[$name]; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @param string $name |
|
67
|
|
|
* @return HeaderInterface[]|iterable |
|
68
|
|
|
*/ |
|
69
|
61 |
|
public function getHeader(string $name): iterable |
|
70
|
|
|
{ |
|
71
|
61 |
|
$name = \strtolower($name); |
|
72
|
|
|
|
|
73
|
61 |
|
if (!isset($this->headers[$name])) { |
|
74
|
2 |
|
return []; |
|
|
|
|
|
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
61 |
|
return $this->headers[$name]; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @param HeaderInterface $header |
|
82
|
|
|
* @return MessageInterface |
|
83
|
|
|
*/ |
|
84
|
115 |
|
public function withHeader(HeaderInterface $header): MessageInterface |
|
85
|
|
|
{ |
|
86
|
115 |
|
$clone = clone $this; |
|
87
|
115 |
|
$clone->headers[\strtolower((string)$header->getName())] = [$header]; |
|
88
|
115 |
|
return $clone; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @param HeaderInterface $header |
|
93
|
|
|
* @return MessageInterface |
|
94
|
|
|
*/ |
|
95
|
3 |
|
public function withAddedHeader(HeaderInterface $header): MessageInterface |
|
96
|
|
|
{ |
|
97
|
3 |
|
$clone = clone $this; |
|
98
|
3 |
|
$clone->headers[\strtolower((string)$header->getName())][] = $header; |
|
99
|
3 |
|
return $clone; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @param string $name |
|
104
|
|
|
* @return MessageInterface |
|
105
|
|
|
*/ |
|
106
|
5 |
|
public function withoutHeader(string $name): MessageInterface |
|
107
|
|
|
{ |
|
108
|
5 |
|
$clone = clone $this; |
|
109
|
5 |
|
unset($clone->headers[(string)\strtolower($name)]); |
|
110
|
5 |
|
return $clone; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* @param StreamInterface $body |
|
115
|
|
|
* @return MessageInterface |
|
116
|
|
|
*/ |
|
117
|
93 |
|
public function withBody(StreamInterface $body): MessageInterface |
|
118
|
|
|
{ |
|
119
|
93 |
|
$clone = clone $this; |
|
120
|
93 |
|
$clone->body = $body; |
|
121
|
93 |
|
return $clone; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* @return StreamInterface |
|
126
|
|
|
*/ |
|
127
|
78 |
|
public function getBody(): StreamInterface |
|
128
|
|
|
{ |
|
129
|
78 |
|
return $this->body; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* @return string |
|
134
|
|
|
*/ |
|
135
|
58 |
|
public function __toString(): string |
|
136
|
|
|
{ |
|
137
|
58 |
|
return (string) (new MessageStream($this)); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* @param string $messageString |
|
142
|
|
|
* @return MessageInterface |
|
143
|
|
|
*/ |
|
144
|
48 |
|
public static function fromString(string $messageString): MessageInterface |
|
145
|
|
|
{ |
|
146
|
48 |
|
$message = new self(); |
|
147
|
|
|
|
|
148
|
48 |
|
$lines = \preg_split('/\r\n/', $messageString); |
|
149
|
48 |
|
if ($lines === false) { |
|
150
|
|
|
throw new \UnexpectedValueException('Cannot parse from string, cannot split lines'); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
48 |
|
for ($n = 0, $length = \count($lines); $n < $length; $n++) { |
|
154
|
48 |
|
$line = $lines[$n]; |
|
155
|
|
|
|
|
156
|
48 |
|
if ($line === '') { |
|
157
|
47 |
|
$message = $message->withBody( |
|
158
|
47 |
|
new StringStream( |
|
159
|
47 |
|
\implode( |
|
160
|
47 |
|
"\r\n", |
|
161
|
47 |
|
\array_slice($lines, $n + 1) |
|
162
|
|
|
) |
|
163
|
|
|
) |
|
164
|
|
|
); |
|
165
|
47 |
|
break; |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
48 |
|
while (isset($lines[$n + 1]) && $lines[$n + 1] !== '' && \trim($lines[$n + 1][0]) === '') { |
|
169
|
2 |
|
$line .= ' ' . \ltrim($lines[$n + 1]); |
|
170
|
2 |
|
$n++; |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
48 |
|
$message = $message->withHeader(HeaderLine::fromString($line)->getHeader()); |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
47 |
|
return $message; |
|
177
|
|
|
} |
|
178
|
|
|
} |
|
179
|
|
|
|
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.