1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Genkgo\Mail\Mime; |
5
|
|
|
|
6
|
|
|
use Genkgo\Mail\Header\ContentDisposition; |
7
|
|
|
use Genkgo\Mail\Header\ContentTransferEncoding; |
8
|
|
|
use Genkgo\Mail\Header\ContentType; |
9
|
|
|
use Genkgo\Mail\HeaderInterface; |
10
|
|
|
use Genkgo\Mail\Stream\Base64EncodedStream; |
11
|
|
|
use Genkgo\Mail\StreamInterface; |
12
|
|
|
|
13
|
|
|
final class ResourceAttachment implements PartInterface |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var PartInterface |
17
|
|
|
*/ |
18
|
|
|
private $decoratedPart; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @param resource $resource |
22
|
|
|
* @param string $filename |
23
|
|
|
* @param ContentType $contentType |
24
|
|
|
*/ |
25
|
8 |
|
public function __construct($resource, string $filename, ContentType $contentType) |
26
|
|
|
{ |
27
|
8 |
|
if (!\is_resource($resource)) { |
28
|
|
|
throw new \InvalidArgumentException('Resource must be a resource'); |
29
|
|
|
} |
30
|
|
|
|
31
|
8 |
|
$this->decoratedPart = (new GenericPart()) |
32
|
8 |
|
->withBody(new Base64EncodedStream($resource)) |
33
|
8 |
|
->withHeader($contentType) |
34
|
8 |
|
->withHeader(new ContentTransferEncoding('base64')) |
35
|
8 |
|
->withHeader(ContentDisposition::newAttachment($filename)); |
36
|
8 |
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param string $string |
40
|
|
|
* @param string $filename |
41
|
|
|
* @param ContentType $contentType |
42
|
|
|
* @return ResourceAttachment |
43
|
|
|
*/ |
44
|
8 |
|
public static function fromString(string $string, string $filename, ContentType $contentType) |
45
|
|
|
{ |
46
|
8 |
|
$resource = \fopen('php://memory', 'r+'); |
47
|
8 |
|
if ($resource === false) { |
48
|
|
|
throw new \UnexpectedValueException('Cannot open php://memory for writing'); |
49
|
|
|
} |
50
|
|
|
|
51
|
8 |
|
\fwrite($resource, $string); |
52
|
8 |
|
return new self($resource, $filename, $contentType); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @return iterable<HeaderInterface> |
|
|
|
|
57
|
|
|
*/ |
58
|
6 |
|
public function getHeaders(): iterable |
59
|
|
|
{ |
60
|
6 |
|
return $this->decoratedPart->getHeaders(); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param string $name |
65
|
|
|
* @return bool |
66
|
|
|
*/ |
67
|
|
|
public function hasHeader(string $name): bool |
68
|
|
|
{ |
69
|
|
|
return $this->decoratedPart->hasHeader($name); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param string $name |
74
|
|
|
* @return HeaderInterface |
75
|
|
|
*/ |
76
|
8 |
|
public function getHeader(string $name): HeaderInterface |
77
|
|
|
{ |
78
|
8 |
|
return $this->decoratedPart->getHeader($name); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param HeaderInterface $header |
83
|
|
|
* @return PartInterface |
84
|
|
|
*/ |
85
|
|
|
public function withHeader(HeaderInterface $header): PartInterface |
86
|
|
|
{ |
87
|
|
|
$clone = clone $this; |
88
|
|
|
$clone->decoratedPart = $this->decoratedPart->withHeader($header); |
89
|
|
|
return $clone; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param string $name |
94
|
|
|
* @return PartInterface |
95
|
|
|
*/ |
96
|
|
|
public function withoutHeader(string $name): PartInterface |
97
|
|
|
{ |
98
|
|
|
$clone = clone $this; |
99
|
|
|
$clone->decoratedPart = $this->decoratedPart->withoutHeader($name); |
100
|
|
|
return $clone; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @param StreamInterface $body |
105
|
|
|
* @return PartInterface |
106
|
|
|
*/ |
107
|
|
|
public function withBody(StreamInterface $body): PartInterface |
108
|
|
|
{ |
109
|
|
|
throw new \RuntimeException('Cannot modify body of ResourceAttachment'); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @return StreamInterface |
114
|
|
|
*/ |
115
|
6 |
|
public function getBody(): StreamInterface |
116
|
|
|
{ |
117
|
6 |
|
return $this->decoratedPart->getBody(); |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
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.