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