1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace PhpEmail\Attachment; |
6
|
|
|
|
7
|
|
|
use PhpEmail\Attachment; |
8
|
|
|
use PhpEmail\Validate; |
9
|
|
|
|
10
|
|
|
class FileAttachment extends AttachmentWithHeaders |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var string |
14
|
|
|
*/ |
15
|
|
|
private $file; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var string|null |
19
|
|
|
*/ |
20
|
|
|
private $content; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @param string $file |
24
|
|
|
* @param null|string $name If null, the class will determine a name for the attachment based on the file path. |
25
|
|
|
* @param null|string $contentId |
26
|
|
|
* @param null|string $contentType |
27
|
|
|
* @param null|string $charset |
28
|
|
|
*/ |
29
|
2 |
|
public function __construct( |
30
|
|
|
string $file, |
31
|
|
|
?string $name = null, |
32
|
|
|
?string $contentId = null, |
33
|
|
|
?string $contentType = null, |
34
|
|
|
string $charset = null |
35
|
|
|
) { |
36
|
2 |
|
Validate::that() |
37
|
2 |
|
->isFile('file', $file) |
38
|
2 |
|
->now(); |
39
|
|
|
|
40
|
2 |
|
$this->file = $file; |
41
|
2 |
|
$this->name = $name ?: basename($file); |
42
|
2 |
|
$this->charset = $charset; |
43
|
2 |
|
$this->contentId = $contentId; |
44
|
2 |
|
$this->contentType = $contentType; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* A static alias for the FileAttachment constructor. |
49
|
|
|
* |
50
|
|
|
* @param string $file |
51
|
|
|
* @param null|string $name If null, the class will determine a name for the attachment based on the file path. |
52
|
|
|
* @param null|string $contentId |
53
|
|
|
* @param null|string $contentType |
54
|
|
|
* @param null|string $charset |
55
|
|
|
* |
56
|
|
|
* @return FileAttachment |
57
|
|
|
*/ |
58
|
1 |
|
public static function fromFile( |
59
|
|
|
string $file, |
60
|
|
|
?string $name = null, |
61
|
|
|
?string $contentId = null, |
62
|
|
|
?string $contentType = null, |
63
|
|
|
string $charset = null |
64
|
|
|
): FileAttachment { |
65
|
1 |
|
return new self($file, $name, $contentId, $contentType, $charset); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @return string |
70
|
|
|
*/ |
71
|
2 |
|
public function getFile(): string |
72
|
|
|
{ |
73
|
2 |
|
return $this->file; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* {@inheritdoc} |
78
|
|
|
*/ |
79
|
2 |
|
public function getContent(): string |
80
|
|
|
{ |
81
|
2 |
|
if ($this->content === null) { |
82
|
2 |
|
$this->content = file_get_contents($this->file); |
83
|
|
|
} |
84
|
|
|
|
85
|
2 |
|
return $this->content; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* {@inheritdoc} |
90
|
|
|
*/ |
91
|
2 |
|
public function getBase64Content(): string |
92
|
|
|
{ |
93
|
2 |
|
return base64_encode($this->getContent()); |
94
|
|
|
} |
95
|
|
|
|
96
|
1 |
|
protected function determineContentType(): string |
97
|
|
|
{ |
98
|
1 |
|
return mime_content_type($this->file); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @return string |
103
|
|
|
*/ |
104
|
2 |
|
public function __toString(): string |
105
|
|
|
{ |
106
|
2 |
|
return json_encode([ |
107
|
2 |
|
'file' => $this->file, |
108
|
2 |
|
'name' => $this->name, |
109
|
2 |
|
'contentId' => $this->contentId, |
110
|
|
|
]); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|