1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Genkgo\Mail\Mime; |
5
|
|
|
|
6
|
|
|
use Genkgo\Mail\HeaderInterface; |
7
|
|
|
use Genkgo\Mail\MessageInterface; |
8
|
|
|
use Genkgo\Mail\Stream\EmptyStream; |
9
|
|
|
use Genkgo\Mail\StreamInterface; |
10
|
|
|
|
11
|
|
|
final class GenericPart implements PartInterface |
12
|
|
|
{ |
13
|
|
|
private const ALLOWED_HEADERS = [ |
14
|
|
|
'content-type' => true, |
15
|
|
|
'content-transfer-encoding' => true, |
16
|
|
|
'content-id' => true, |
17
|
|
|
'content-disposition' => true, |
18
|
|
|
'content-description' => true, |
19
|
|
|
'content-location' => true, |
20
|
|
|
'content-language' => true, |
21
|
|
|
]; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var array|HeaderInterface[] |
25
|
|
|
*/ |
26
|
|
|
private $headers = []; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var StreamInterface |
30
|
|
|
*/ |
31
|
|
|
private $body; |
32
|
|
|
|
33
|
78 |
|
public function __construct() |
34
|
|
|
{ |
35
|
78 |
|
$this->body = new EmptyStream(); |
36
|
78 |
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @return iterable<HeaderInterface> |
|
|
|
|
40
|
|
|
*/ |
41
|
31 |
|
public function getHeaders(): iterable |
42
|
|
|
{ |
43
|
31 |
|
return $this->headers; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param string $name |
48
|
|
|
* @return bool |
49
|
|
|
*/ |
50
|
29 |
|
public function hasHeader(string $name): bool |
51
|
|
|
{ |
52
|
29 |
|
$name = \strtolower($name); |
53
|
|
|
|
54
|
29 |
|
return isset($this->headers[$name]); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param string $name |
59
|
|
|
* @return HeaderInterface |
60
|
|
|
*/ |
61
|
51 |
|
public function getHeader(string $name): HeaderInterface |
62
|
|
|
{ |
63
|
51 |
|
$name = \strtolower($name); |
64
|
|
|
|
65
|
51 |
|
if (!isset($this->headers[$name])) { |
66
|
17 |
|
throw new \UnexpectedValueException('No header with name ' . $name); |
67
|
|
|
} |
68
|
|
|
|
69
|
50 |
|
return $this->headers[$name]; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param HeaderInterface $header |
74
|
|
|
* @return PartInterface |
75
|
|
|
*/ |
76
|
78 |
|
public function withHeader(HeaderInterface $header): PartInterface |
77
|
|
|
{ |
78
|
78 |
|
$name = \strtolower((string)$header->getName()); |
79
|
78 |
|
$this->assertValidHeader($name); |
80
|
|
|
|
81
|
77 |
|
$clone = clone $this; |
82
|
77 |
|
$clone->headers[$name] = $header; |
83
|
77 |
|
return $clone; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param string $name |
88
|
|
|
* @return PartInterface |
89
|
|
|
*/ |
90
|
7 |
|
public function withoutHeader(string $name): PartInterface |
91
|
|
|
{ |
92
|
7 |
|
$name = \strtolower($name); |
93
|
|
|
|
94
|
7 |
|
$clone = clone $this; |
95
|
7 |
|
unset($clone->headers[$name]); |
96
|
7 |
|
return $clone; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @param StreamInterface $body |
101
|
|
|
* @return PartInterface |
102
|
|
|
*/ |
103
|
63 |
|
public function withBody(StreamInterface $body): PartInterface |
104
|
|
|
{ |
105
|
63 |
|
$clone = clone $this; |
106
|
63 |
|
$clone->body = $body; |
107
|
63 |
|
return $clone; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @return StreamInterface |
112
|
|
|
*/ |
113
|
42 |
|
public function getBody(): StreamInterface |
114
|
|
|
{ |
115
|
42 |
|
return $this->body; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @param string $name |
120
|
|
|
*/ |
121
|
78 |
|
private function assertValidHeader(string $name): void |
122
|
|
|
{ |
123
|
78 |
|
if (!isset(self::ALLOWED_HEADERS[$name])) { |
124
|
1 |
|
throw new \InvalidArgumentException('Invalid Mime part header ' . $name); |
125
|
|
|
} |
126
|
77 |
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @param MessageInterface $message |
130
|
|
|
* @return GenericPart |
131
|
|
|
*/ |
132
|
18 |
|
public static function fromMessage(MessageInterface $message): self |
133
|
|
|
{ |
134
|
18 |
|
$part = new self(); |
135
|
18 |
|
foreach ($message->getHeaders() as $headers) { |
136
|
18 |
|
foreach ($headers as $header) { |
137
|
18 |
|
$headerName = \strtolower((string)$header->getName()); |
138
|
18 |
|
if (isset(self::ALLOWED_HEADERS[$headerName])) { |
139
|
18 |
|
$part->headers[$headerName] = $header; |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|
144
|
18 |
|
$part->body = $message->getBody(); |
145
|
18 |
|
return $part; |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|
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.