1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Genkgo\Mail\Mime; |
5
|
|
|
|
6
|
|
|
use Genkgo\Mail\Header\ContentTransferEncoding; |
7
|
|
|
use Genkgo\Mail\Header\ContentType; |
8
|
|
|
use Genkgo\Mail\HeaderInterface; |
9
|
|
|
use Genkgo\Mail\Stream\OptimalTransferEncodedTextStream; |
10
|
|
|
use Genkgo\Mail\StreamInterface; |
11
|
|
|
|
12
|
|
|
final class PlainTextPart implements PartInterface |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var PartInterface |
16
|
|
|
*/ |
17
|
|
|
private $decoratedPart; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @param string $text |
21
|
|
|
* @param string $charset |
22
|
|
|
*/ |
23
|
27 |
|
public function __construct(string $text, string $charset = '') |
24
|
|
|
{ |
25
|
27 |
|
$stream = new OptimalTransferEncodedTextStream($text); |
26
|
27 |
|
$encoding = $stream->getMetadata(['transfer-encoding'])['transfer-encoding']; |
27
|
|
|
|
28
|
27 |
|
$this->decoratedPart = (new GenericPart()) |
29
|
27 |
|
->withBody($stream) |
30
|
27 |
|
->withHeader(new ContentType('text/plain', $charset)) |
31
|
27 |
|
->withHeader(new ContentTransferEncoding($encoding)); |
32
|
27 |
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @return iterable<HeaderInterface> |
|
|
|
|
36
|
|
|
*/ |
37
|
24 |
|
public function getHeaders(): iterable |
38
|
|
|
{ |
39
|
24 |
|
return $this->decoratedPart->getHeaders(); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param string $name |
44
|
|
|
* @return bool |
45
|
|
|
*/ |
46
|
1 |
|
public function hasHeader(string $name): bool |
47
|
|
|
{ |
48
|
1 |
|
return $this->decoratedPart->hasHeader($name); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param string $name |
53
|
|
|
* @return HeaderInterface |
54
|
|
|
*/ |
55
|
24 |
|
public function getHeader(string $name): HeaderInterface |
56
|
|
|
{ |
57
|
24 |
|
return $this->decoratedPart->getHeader($name); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param HeaderInterface $header |
62
|
|
|
* @return PartInterface |
63
|
|
|
*/ |
64
|
1 |
|
public function withHeader(HeaderInterface $header): PartInterface |
65
|
|
|
{ |
66
|
1 |
|
$clone = clone $this; |
67
|
1 |
|
$clone->decoratedPart = $this->decoratedPart->withHeader($header); |
68
|
1 |
|
return $clone; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param string $name |
73
|
|
|
* @return PartInterface |
74
|
|
|
*/ |
75
|
1 |
|
public function withoutHeader(string $name): PartInterface |
76
|
|
|
{ |
77
|
1 |
|
$clone = clone $this; |
78
|
1 |
|
$clone->decoratedPart = $this->decoratedPart->withoutHeader($name); |
79
|
1 |
|
return $clone; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param StreamInterface $body |
84
|
|
|
* @return PartInterface |
85
|
|
|
*/ |
86
|
1 |
|
public function withBody(StreamInterface $body): PartInterface |
87
|
|
|
{ |
88
|
1 |
|
throw new \RuntimeException('Cannot modify body of PlainTextPart'); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @return StreamInterface |
93
|
|
|
*/ |
94
|
24 |
|
public function getBody(): StreamInterface |
95
|
|
|
{ |
96
|
24 |
|
return $this->decoratedPart->getBody(); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
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.