|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types = 1); |
|
3
|
|
|
|
|
4
|
|
|
namespace Genkgo\Mail\Stream; |
|
5
|
|
|
|
|
6
|
|
|
use Genkgo\Mail\Header\HeaderLine; |
|
7
|
|
|
use Genkgo\Mail\HeaderInterface; |
|
8
|
|
|
use Genkgo\Mail\MessageInterface; |
|
9
|
|
|
use Genkgo\Mail\StreamInterface; |
|
10
|
|
|
|
|
11
|
|
|
final class MessageStream implements StreamInterface |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* @var StreamInterface |
|
15
|
|
|
*/ |
|
16
|
|
|
private $decoratedStream; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @param MessageInterface $message |
|
20
|
|
|
*/ |
|
21
|
|
|
public function __construct(MessageInterface $message) |
|
22
|
|
|
{ |
|
23
|
56 |
|
$this->decoratedStream = new LazyStream(function () use ($message) { |
|
24
|
56 |
|
$headerString = \implode( |
|
25
|
56 |
|
"\r\n", |
|
26
|
56 |
|
\array_values( |
|
27
|
56 |
|
\array_filter( |
|
28
|
56 |
|
\array_map( |
|
29
|
56 |
|
function (array $headers) { |
|
30
|
56 |
|
return \implode( |
|
31
|
56 |
|
"\r\n", |
|
32
|
56 |
|
\array_map( |
|
33
|
56 |
|
function (HeaderInterface $header) { |
|
34
|
56 |
|
return (string) (new HeaderLine($header)); |
|
35
|
56 |
|
}, |
|
36
|
56 |
|
$headers |
|
37
|
|
|
) |
|
38
|
|
|
); |
|
39
|
56 |
|
}, |
|
40
|
56 |
|
$message->getHeaders() |
|
41
|
|
|
) |
|
42
|
|
|
) |
|
43
|
|
|
) |
|
44
|
|
|
); |
|
45
|
|
|
|
|
46
|
56 |
|
return new ConcatenatedStream( |
|
47
|
56 |
|
new \ArrayObject([ |
|
|
|
|
|
|
48
|
56 |
|
new StringStream($headerString), |
|
49
|
56 |
|
new StringStream("\r\n\r\n"), |
|
50
|
56 |
|
$message->getBody() |
|
51
|
|
|
]) |
|
52
|
|
|
); |
|
53
|
56 |
|
}); |
|
54
|
56 |
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @return string |
|
58
|
|
|
*/ |
|
59
|
55 |
|
public function __toString(): string |
|
60
|
|
|
{ |
|
61
|
55 |
|
return $this->decoratedStream->__toString(); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
public function close(): void |
|
65
|
|
|
{ |
|
66
|
|
|
$this->decoratedStream->close(); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @return mixed |
|
71
|
|
|
*/ |
|
72
|
|
|
public function detach() |
|
73
|
|
|
{ |
|
74
|
|
|
return $this->decoratedStream->detach(); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @return int|null |
|
79
|
|
|
*/ |
|
80
|
|
|
public function getSize(): ?int |
|
81
|
|
|
{ |
|
82
|
|
|
return $this->decoratedStream->getSize(); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @return int |
|
87
|
|
|
* @throws \RuntimeException |
|
88
|
|
|
*/ |
|
89
|
|
|
public function tell(): int |
|
90
|
|
|
{ |
|
91
|
|
|
return $this->decoratedStream->tell(); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @return bool |
|
96
|
|
|
*/ |
|
97
|
2 |
|
public function eof(): bool |
|
98
|
|
|
{ |
|
99
|
2 |
|
return $this->decoratedStream->eof(); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @return bool |
|
104
|
|
|
*/ |
|
105
|
|
|
public function isSeekable(): bool |
|
106
|
|
|
{ |
|
107
|
|
|
return $this->decoratedStream->isSeekable(); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* @param int $offset |
|
112
|
|
|
* @param int $whence |
|
113
|
|
|
* @return int |
|
114
|
|
|
*/ |
|
115
|
|
|
public function seek(int $offset, int $whence = SEEK_SET): int |
|
116
|
|
|
{ |
|
117
|
|
|
return $this->decoratedStream->seek($offset, $whence); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* @return bool |
|
122
|
|
|
*/ |
|
123
|
2 |
|
public function rewind(): bool |
|
124
|
|
|
{ |
|
125
|
2 |
|
return $this->decoratedStream->rewind(); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* @return bool |
|
130
|
|
|
*/ |
|
131
|
|
|
public function isWritable(): bool |
|
132
|
|
|
{ |
|
133
|
|
|
return false; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* @param string $string |
|
138
|
|
|
* @return int |
|
139
|
|
|
*/ |
|
140
|
|
|
public function write($string): int |
|
141
|
|
|
{ |
|
142
|
|
|
throw new \RuntimeException('Cannot write to stream'); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* @return bool |
|
147
|
|
|
*/ |
|
148
|
|
|
public function isReadable(): bool |
|
149
|
|
|
{ |
|
150
|
|
|
return true; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* @param int $length |
|
155
|
|
|
* @return string |
|
156
|
|
|
*/ |
|
157
|
2 |
|
public function read(int $length): string |
|
158
|
|
|
{ |
|
159
|
2 |
|
return $this->decoratedStream->read($length); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* @return string |
|
164
|
|
|
*/ |
|
165
|
|
|
public function getContents(): string |
|
166
|
|
|
{ |
|
167
|
|
|
return $this->decoratedStream->getContents(); |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
/** |
|
171
|
|
|
* @param array $keys |
|
172
|
|
|
* @return array |
|
173
|
|
|
*/ |
|
174
|
|
|
public function getMetadata(array $keys = []): array |
|
175
|
|
|
{ |
|
176
|
|
|
return $this->decoratedStream->getMetadata(); |
|
177
|
|
|
} |
|
178
|
|
|
} |
|
179
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: