|
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\BitEncodedStream; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Class ImmutableMessage |
|
13
|
|
|
* @package Genkgo\Mail |
|
14
|
|
|
*/ |
|
15
|
|
|
final class GenericMessage implements MessageInterface |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @var array |
|
19
|
|
|
*/ |
|
20
|
|
|
private $headers = [ |
|
21
|
|
|
'return-path' => [], |
|
22
|
|
|
'received' => [], |
|
23
|
|
|
'dkim-signature' => [], |
|
24
|
|
|
'domainkey-signature' => [], |
|
25
|
|
|
'sender' => [], |
|
26
|
|
|
'message-id' => [], |
|
27
|
|
|
'date' => [], |
|
28
|
|
|
'subject' => [], |
|
29
|
|
|
'from' => [], |
|
30
|
|
|
'reply-to' => [], |
|
31
|
|
|
'to' => [], |
|
32
|
|
|
'cc' => [], |
|
33
|
|
|
'bcc' => [], |
|
34
|
|
|
'mime-version' => [], |
|
35
|
|
|
'content-type' => [], |
|
36
|
|
|
'content-transfer-encoding' => [], |
|
37
|
|
|
]; |
|
38
|
|
|
/** |
|
39
|
|
|
* @var StreamInterface |
|
40
|
|
|
*/ |
|
41
|
|
|
private $body; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* ImmutableMessage constructor. |
|
45
|
|
|
*/ |
|
46
|
56 |
|
public function __construct() |
|
47
|
|
|
{ |
|
48
|
56 |
|
$this->body = new EmptyStream(); |
|
49
|
56 |
|
$this->headers['mime-version'] = [new MimeVersion()]; |
|
50
|
56 |
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @return iterable |
|
54
|
|
|
*/ |
|
55
|
5 |
|
public function getHeaders(): iterable |
|
56
|
|
|
{ |
|
57
|
5 |
|
return $this->headers; |
|
|
|
|
|
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @param string $name |
|
62
|
|
|
* @return bool |
|
63
|
|
|
*/ |
|
64
|
16 |
|
public function hasHeader(string $name): bool |
|
65
|
|
|
{ |
|
66
|
16 |
|
$name = strtolower($name); |
|
67
|
16 |
|
return isset($this->headers[$name]) && $this->headers[$name]; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @param string $name |
|
72
|
|
|
* @return HeaderInterface[]|iterable |
|
73
|
|
|
*/ |
|
74
|
16 |
|
public function getHeader(string $name): iterable |
|
75
|
|
|
{ |
|
76
|
16 |
|
$name = strtolower($name); |
|
77
|
|
|
|
|
78
|
16 |
|
if (!isset($this->headers[$name])) { |
|
79
|
1 |
|
return []; |
|
|
|
|
|
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
16 |
|
return $this->headers[$name]; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @param HeaderInterface $header |
|
87
|
|
|
* @return MessageInterface |
|
88
|
|
|
*/ |
|
89
|
42 |
|
public function withHeader(HeaderInterface $header): MessageInterface |
|
90
|
|
|
{ |
|
91
|
42 |
|
$clone = clone $this; |
|
92
|
42 |
|
$clone->headers[strtolower((string)$header->getName())] = [$header]; |
|
93
|
42 |
|
return $clone; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* @param HeaderInterface $header |
|
98
|
|
|
* @return MessageInterface |
|
99
|
|
|
*/ |
|
100
|
3 |
|
public function withAddedHeader(HeaderInterface $header): MessageInterface |
|
101
|
|
|
{ |
|
102
|
3 |
|
$clone = clone $this; |
|
103
|
3 |
|
$clone->headers[strtolower((string)$header->getName())][] = $header; |
|
104
|
3 |
|
return $clone; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* @param string $name |
|
109
|
|
|
* @return MessageInterface |
|
110
|
|
|
*/ |
|
111
|
5 |
|
public function withoutHeader(string $name): MessageInterface |
|
112
|
|
|
{ |
|
113
|
5 |
|
$clone = clone $this; |
|
114
|
5 |
|
unset($clone->headers[(string)strtolower($name)]); |
|
115
|
5 |
|
return $clone; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* @param StreamInterface $body |
|
120
|
|
|
* @return MessageInterface |
|
121
|
|
|
*/ |
|
122
|
27 |
|
public function withBody(StreamInterface $body): MessageInterface |
|
123
|
|
|
{ |
|
124
|
27 |
|
$clone = clone $this; |
|
125
|
27 |
|
$clone->body = $body; |
|
126
|
27 |
|
return $clone; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* @return StreamInterface |
|
131
|
|
|
*/ |
|
132
|
3 |
|
public function getBody(): StreamInterface |
|
133
|
|
|
{ |
|
134
|
3 |
|
return $this->body; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* @return string |
|
139
|
|
|
*/ |
|
140
|
21 |
|
public function __toString(): string |
|
141
|
|
|
{ |
|
142
|
21 |
|
$headerString = array_values( |
|
143
|
21 |
|
array_filter( |
|
144
|
21 |
|
array_map( |
|
145
|
|
|
function (array $headers) { |
|
146
|
21 |
|
return implode( |
|
147
|
21 |
|
"\r\n", |
|
148
|
21 |
|
array_map( |
|
149
|
21 |
|
function (HeaderInterface $header) { |
|
150
|
21 |
|
return (string) (new HeaderLine($header)); |
|
151
|
21 |
|
}, |
|
152
|
21 |
|
$headers |
|
153
|
|
|
) |
|
154
|
|
|
); |
|
155
|
21 |
|
}, |
|
156
|
21 |
|
$this->headers |
|
157
|
|
|
) |
|
158
|
|
|
) |
|
159
|
|
|
); |
|
160
|
|
|
|
|
161
|
21 |
|
return implode("\r\n", array_merge($headerString, ['', (string)$this->body])); |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* @param string $messageString |
|
166
|
|
|
* @return MessageInterface |
|
167
|
|
|
*/ |
|
168
|
11 |
|
public static function fromString(string $messageString): MessageInterface |
|
169
|
|
|
{ |
|
170
|
11 |
|
$message = new self(); |
|
171
|
|
|
|
|
172
|
11 |
|
$lines = preg_split('/\r\n/', $messageString); |
|
173
|
11 |
|
for ($n = 0, $length = count($lines); $n < $length; $n++) { |
|
174
|
11 |
|
$line = $lines[$n]; |
|
175
|
|
|
|
|
176
|
11 |
|
if ($line === '') { |
|
177
|
11 |
|
$message = $message->withBody( |
|
178
|
11 |
|
new BitEncodedStream( |
|
179
|
11 |
|
implode( |
|
180
|
11 |
|
"\r\n", |
|
181
|
11 |
|
array_slice($lines, $n + 1) |
|
182
|
|
|
) |
|
183
|
|
|
) |
|
184
|
|
|
); |
|
185
|
11 |
|
break; |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
11 |
|
while (isset($lines[$n + 1]) && $lines[$n + 1] !== '' && $lines[$n + 1][0] === ' ') { |
|
189
|
1 |
|
$line .= $lines[$n + 1]; |
|
190
|
1 |
|
$n++; |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
11 |
|
$message = $message->withHeader(HeaderLine::fromString($line)->getHeader()); |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
11 |
|
return $message; |
|
197
|
|
|
} |
|
198
|
|
|
} |
|
199
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.