1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
You may not change or alter any portion of this comment or credits of supporting |
4
|
|
|
developers from this source code or any supporting source code which is considered |
5
|
|
|
copyrighted (c) material of the original comment or credit authors. |
6
|
|
|
|
7
|
|
|
This program is distributed in the hope that it will be useful, |
8
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
9
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Xoops\Core\Service\Data; |
13
|
|
|
|
14
|
|
|
use Xmf\Assert; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* The EmailAddress data object is a email address with optional display name |
18
|
|
|
* |
19
|
|
|
* This is an Immutable data object. That means any changes to the data (state) |
20
|
|
|
* return a new object, while the internal state of the original object is preserved. |
21
|
|
|
* |
22
|
|
|
* All data is validated for type and value, and an exception is generated when |
23
|
|
|
* data on any operation for a property when it is not valid. |
24
|
|
|
* |
25
|
|
|
* The EmailAttachment data object is used for mailer services |
26
|
|
|
* |
27
|
|
|
* @category Xoops\Core\Service\Data |
28
|
|
|
* @package Xoops\Core |
29
|
|
|
* @author Richard Griffith <[email protected]> |
30
|
|
|
* @copyright 2018 XOOPS Project (https://xoops.org) |
31
|
|
|
* @license GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html) |
32
|
|
|
* @link https://xoops.org |
33
|
|
|
*/ |
34
|
|
|
class EmailAttachment |
35
|
|
|
{ |
36
|
|
|
/** @var string $filename fully qualified file name of file to be attached */ |
37
|
|
|
protected $filename; |
38
|
|
|
|
39
|
|
|
/** @var string $mimeType mime-type of attached content */ |
40
|
|
|
protected $mimeType; |
41
|
|
|
|
42
|
|
|
/** @var string $stringBody content to be attached, if not from file */ |
43
|
|
|
protected $stringBody; |
44
|
|
|
|
45
|
|
|
/** @var string $name name or content id of this attachment */ |
46
|
|
|
protected $name; |
47
|
|
|
|
48
|
|
|
/** @var bool $inline treat attachment as inline if true, as download if false (default) */ |
49
|
|
|
protected $inline = false; |
50
|
|
|
|
51
|
|
|
/* assert messages */ |
52
|
|
|
protected const MESSAGE_FILE = 'File is invalid'; |
53
|
|
|
protected const MESSAGE_MIME = 'Mime Type is invalid'; |
54
|
|
|
protected const MESSAGE_NAME = 'Name is invalid'; |
55
|
|
|
protected const MESSAGE_BODY = 'Body string is invalid'; |
56
|
|
|
protected const MESSAGE_INLINE = 'Inline flag is invalid'; |
57
|
|
|
|
58
|
|
|
protected const MIME_REGEX = '/^[a-z0-9\-+.]+\/[a-z0-9\-+.]+$/'; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* EmailAttachment constructor. |
62
|
|
|
* |
63
|
|
|
* If an argument is null, the corresponding value will not be set. Values can be set |
64
|
|
|
* later with the with*() methods, but each will result in a new object. |
65
|
|
|
* |
66
|
|
|
* @param null|string $filename fully qualified filename of file to attach |
67
|
|
|
* @param null|string $mimeType mime-type of file |
68
|
|
|
* |
69
|
|
|
* @throws \InvalidArgumentException |
70
|
|
|
*/ |
71
|
18 |
|
public function __construct(?string $filename = null, ?string $mimeType = null) |
72
|
|
|
{ |
73
|
18 |
|
if (null!==$filename) { |
74
|
6 |
|
Assert::fileExists($filename, static::MESSAGE_FILE); |
75
|
5 |
|
$this->filename = $filename; |
76
|
|
|
} |
77
|
18 |
|
if (null!==$mimeType) { |
78
|
1 |
|
Assert::regex($mimeType, static::MIME_REGEX, static::MESSAGE_MIME); |
79
|
1 |
|
$this->mimeType = $mimeType; |
80
|
|
|
} |
81
|
18 |
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* withFilename |
85
|
|
|
* |
86
|
|
|
* @param string $filename fully qualified filename |
87
|
|
|
* |
88
|
|
|
* @return EmailAttachment |
89
|
|
|
* |
90
|
|
|
* @throws \InvalidArgumentException |
91
|
|
|
*/ |
92
|
1 |
|
public function withFilename(string $filename) : EmailAttachment |
93
|
|
|
{ |
94
|
1 |
|
Assert::fileExists($filename, static::MESSAGE_FILE); |
95
|
1 |
|
$new = clone $this; |
96
|
1 |
|
$new->filename = $filename; |
97
|
1 |
|
return $new; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* withMimeType |
102
|
|
|
* |
103
|
|
|
* @param string $mimeType mime type of the filename contents or stringBody |
104
|
|
|
* |
105
|
|
|
* @return EmailAttachment |
106
|
|
|
* |
107
|
|
|
* @throws \InvalidArgumentException |
108
|
|
|
*/ |
109
|
1 |
|
public function withMimeType(string $mimeType) : EmailAttachment |
110
|
|
|
{ |
111
|
1 |
|
Assert::regex($mimeType, static::MIME_REGEX, static::MESSAGE_MIME); |
112
|
1 |
|
$new = clone $this; |
113
|
1 |
|
$new->mimeType = $mimeType; |
114
|
1 |
|
return $new; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* withName |
119
|
|
|
* |
120
|
|
|
* @param string $name name or content id for attachment |
121
|
|
|
* |
122
|
|
|
* @return EmailAttachment |
123
|
|
|
* |
124
|
|
|
* @throws \InvalidArgumentException |
125
|
|
|
*/ |
126
|
1 |
|
public function withName(string $name) : EmailAttachment |
127
|
|
|
{ |
128
|
1 |
|
Assert::stringNotEmpty($name, static::MESSAGE_NAME); |
129
|
1 |
|
$new = clone $this; |
130
|
1 |
|
$new->name = $name; |
131
|
1 |
|
return $new; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* withStringBody |
136
|
|
|
* |
137
|
|
|
* @param string $stringBody alternate body used instead of file contents |
138
|
|
|
* |
139
|
|
|
* @return EmailAttachment |
140
|
|
|
* |
141
|
|
|
* @throws \InvalidArgumentException |
142
|
|
|
*/ |
143
|
3 |
|
public function withStringBody(string $stringBody) : EmailAttachment |
144
|
|
|
{ |
145
|
3 |
|
Assert::stringNotEmpty($stringBody, static::MESSAGE_BODY); |
146
|
3 |
|
$new = clone $this; |
147
|
3 |
|
$new->stringBody = $stringBody; |
148
|
3 |
|
return $new; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* withInlineAttribute |
153
|
|
|
* |
154
|
|
|
* @param bool $inline true to treat attachment as inline, false for download |
155
|
|
|
* |
156
|
|
|
* @return EmailAttachment |
157
|
|
|
* |
158
|
|
|
* @throws \InvalidArgumentException |
159
|
|
|
*/ |
160
|
1 |
|
public function withInlineAttribute(bool $inline = true) : EmailAttachment |
161
|
|
|
{ |
162
|
1 |
|
Assert::boolean($inline, static::MESSAGE_INLINE); |
163
|
1 |
|
$new = clone $this; |
164
|
1 |
|
$new->inline = $inline; |
165
|
1 |
|
return $new; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* getFilename |
170
|
|
|
* |
171
|
|
|
* @return string an file name |
172
|
|
|
* |
173
|
|
|
* @throws \LogicException (property was not properly set before used) |
174
|
|
|
*/ |
175
|
10 |
|
public function getFilename() : ?string |
176
|
|
|
{ |
177
|
10 |
|
if (null === $this->stringBody) { |
178
|
|
|
try { |
179
|
9 |
|
Assert::notNull($this->filename, static::MESSAGE_FILE); |
180
|
6 |
|
Assert::fileExists($this->filename, static::MESSAGE_FILE); |
181
|
3 |
|
} catch (\InvalidArgumentException $e) { |
182
|
3 |
|
throw new \LogicException($e->getMessage(), $e->getCode(), $e); |
183
|
|
|
} |
184
|
|
|
} |
185
|
7 |
|
return $this->filename; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* getMimeType |
190
|
|
|
* |
191
|
|
|
* @return string|null mime type of filename contents or stringBody |
192
|
|
|
* |
193
|
|
|
* @throws \LogicException (property was not properly set before used) |
194
|
|
|
*/ |
195
|
3 |
|
public function getMimeType() : ?string |
196
|
|
|
{ |
197
|
|
|
try { |
198
|
3 |
|
Assert::nullOrRegex($this->mimeType, static::MIME_REGEX, static::MESSAGE_MIME); |
199
|
1 |
|
} catch (\InvalidArgumentException $e) { |
200
|
1 |
|
throw new \LogicException($e->getMessage(), $e->getCode(), $e); |
201
|
|
|
} |
202
|
2 |
|
return $this->mimeType; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* getName |
207
|
|
|
* |
208
|
|
|
* @return string|null name or content id for attachment |
209
|
|
|
* |
210
|
|
|
* @throws \LogicException (property was not properly set before used) |
211
|
|
|
*/ |
212
|
2 |
|
public function getName() : ?string |
213
|
|
|
{ |
214
|
|
|
try { |
215
|
2 |
|
Assert::nullOrStringNotEmpty($this->name, static::MESSAGE_NAME); |
216
|
1 |
|
} catch (\InvalidArgumentException $e) { |
217
|
1 |
|
throw new \LogicException($e->getMessage(), $e->getCode(), $e); |
218
|
|
|
} |
219
|
1 |
|
return $this->name; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* getStringBody |
224
|
|
|
* |
225
|
|
|
* @return string|null string body attachment contents |
226
|
|
|
* |
227
|
|
|
* @throws \LogicException (property was not properly set before used) |
228
|
|
|
*/ |
229
|
6 |
|
public function getStringBody() : ?string |
230
|
|
|
{ |
231
|
6 |
|
if (null === $this->filename) { |
232
|
|
|
try { |
233
|
4 |
|
Assert::stringNotEmpty($this->stringBody, static::MESSAGE_BODY); |
234
|
1 |
|
} catch (\InvalidArgumentException $e) { |
235
|
1 |
|
throw new \LogicException($e->getMessage(), $e->getCode(), $e); |
236
|
|
|
} |
237
|
|
|
} |
238
|
5 |
|
return $this->stringBody; |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* getInlineAttribute |
243
|
|
|
* |
244
|
|
|
* @return bool attachment inline attribute, true for inline, false for download |
245
|
|
|
*/ |
246
|
1 |
|
public function getInlineAttribute() : bool |
247
|
|
|
{ |
248
|
1 |
|
return (bool) $this->inline; |
249
|
|
|
} |
250
|
|
|
} |
251
|
|
|
|