|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yiisoft\Mailer; |
|
6
|
|
|
|
|
7
|
|
|
use Exception; |
|
8
|
|
|
use RuntimeException; |
|
9
|
|
|
|
|
10
|
|
|
use function bin2hex; |
|
11
|
|
|
use function is_file; |
|
12
|
|
|
use function random_bytes; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* `File` is a data object that stores data for attaching a file to a mail message. |
|
16
|
|
|
*/ |
|
17
|
|
|
final class File |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @var string|null The file ID. |
|
21
|
|
|
*/ |
|
22
|
|
|
private ?string $id = null; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @param string|null $name The name that should be used to attach the file. |
|
26
|
|
|
* @param string|null $path The full path to the file. |
|
27
|
|
|
* @param string|null $content The content that should be used to attach the file. |
|
28
|
|
|
* @param string|null $contentType MIME type that should be used to attach the file. |
|
29
|
|
|
*/ |
|
30
|
|
|
private function __construct( |
|
31
|
|
|
private ?string $name, |
|
32
|
|
|
private ?string $path, |
|
33
|
|
|
private ?string $content, |
|
34
|
|
|
private ?string $contentType |
|
35
|
|
|
) { |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Creates a new file instance from the specified content. |
|
40
|
|
|
* |
|
41
|
|
|
* @param string $content The content that should be used to attach the file. |
|
42
|
|
|
* @param string|null $name The name that should be used to attach the file. |
|
43
|
|
|
* @param string|null $contentType MIME type that should be used to attach the file. |
|
44
|
|
|
*/ |
|
45
|
|
|
public static function fromContent(string $content, string $name = null, string $contentType = null): self |
|
46
|
|
|
{ |
|
47
|
|
|
return new self($name, null, $content, $contentType); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
3 |
|
/** |
|
51
|
|
|
* Creates a new file instance from the specified full path to the file. |
|
52
|
3 |
|
* |
|
53
|
3 |
|
* @param string $path The full path to the file. |
|
54
|
3 |
|
* @param string|null $name The name that should be used to attach the file. |
|
55
|
3 |
|
* @param string|null $contentType MIME type that should be used to attach the file. |
|
56
|
|
|
* |
|
57
|
|
|
* @throws RuntimeException If the specified file does not exist. |
|
58
|
|
|
*/ |
|
59
|
|
|
public static function fromPath(string $path, string $name = null, string $contentType = null): self |
|
60
|
|
|
{ |
|
61
|
|
|
if (!is_file($path)) { |
|
62
|
|
|
throw new RuntimeException("The file {$path} does not exist."); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
return new self($name, $path, null, $contentType); |
|
66
|
|
|
} |
|
67
|
1 |
|
|
|
68
|
|
|
/** |
|
69
|
1 |
|
* Returns the file ID. |
|
70
|
|
|
* |
|
71
|
|
|
* @throws Exception {@see https://www.php.net/manual/en/function.random-bytes.php} |
|
72
|
|
|
* |
|
73
|
|
|
* @return string The file ID. |
|
74
|
|
|
*/ |
|
75
|
|
|
public function id(): string |
|
76
|
|
|
{ |
|
77
|
|
|
if ($this->id === null) { |
|
78
|
|
|
$this->id = bin2hex(random_bytes(16)) . '@app'; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
return $this->id; |
|
|
|
|
|
|
82
|
|
|
} |
|
83
|
4 |
|
|
|
84
|
|
|
/** |
|
85
|
4 |
|
* Returns the file CID source. |
|
86
|
2 |
|
* |
|
87
|
|
|
* @throws Exception {@see https://www.php.net/manual/en/function.random-bytes.php} |
|
88
|
|
|
* |
|
89
|
2 |
|
* @return string The file CID source. |
|
90
|
|
|
*/ |
|
91
|
|
|
public function cid(): string |
|
92
|
|
|
{ |
|
93
|
|
|
return "cid:{$this->id()}"; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Returns the name that should be used to attach the file. |
|
98
|
|
|
* |
|
99
|
3 |
|
* @return string|null The name that should be used to attach the file. |
|
100
|
|
|
*/ |
|
101
|
3 |
|
public function name(): ?string |
|
102
|
3 |
|
{ |
|
103
|
|
|
return $this->name; |
|
104
|
|
|
} |
|
105
|
3 |
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Returns the full path to the file. |
|
108
|
|
|
* |
|
109
|
|
|
* @return string|null The full path to the file. |
|
110
|
|
|
*/ |
|
111
|
|
|
public function path(): ?string |
|
112
|
|
|
{ |
|
113
|
|
|
return $this->path; |
|
114
|
|
|
} |
|
115
|
3 |
|
|
|
116
|
|
|
/** |
|
117
|
3 |
|
* Returns the content that should be used to attach the file. |
|
118
|
|
|
* |
|
119
|
|
|
* @return string|null The content that should be used to attach the file. |
|
120
|
|
|
*/ |
|
121
|
|
|
public function content(): ?string |
|
122
|
|
|
{ |
|
123
|
|
|
return $this->content; |
|
124
|
|
|
} |
|
125
|
2 |
|
|
|
126
|
|
|
/** |
|
127
|
2 |
|
* Returns the MIME type that should be used to attach the file. |
|
128
|
|
|
* |
|
129
|
|
|
* @return string|null MIME type that should be used to attach the file. |
|
130
|
|
|
*/ |
|
131
|
|
|
public function contentType(): ?string |
|
132
|
|
|
{ |
|
133
|
|
|
return $this->contentType; |
|
134
|
|
|
} |
|
135
|
|
|
} |
|
136
|
|
|
|