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
|
|
|
use Xoops\Core\Service\Data\EmailAttachment; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* The EmailAttachmentSet data object is a traversable list of EmailAttachment objects |
19
|
|
|
* |
20
|
|
|
* This is an Immutable data object. That means any changes to the data (state) |
21
|
|
|
* return a new object, while the internal state of the original object is preserved. |
22
|
|
|
* |
23
|
|
|
* All data is validated for type and value, and an exception is generated when |
24
|
|
|
* data on any operation for a property when it is not valid. |
25
|
|
|
* |
26
|
|
|
* The EmailAttachmentSet data object is used for mailer services |
27
|
|
|
* |
28
|
|
|
* @category Xoops\Core\Service\Data |
29
|
|
|
* @package Xoops\Core |
30
|
|
|
* @author Richard Griffith <[email protected]> |
31
|
|
|
* @copyright 2018 XOOPS Project (https://xoops.org) |
32
|
|
|
* @license GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html) |
33
|
|
|
* @link https://xoops.org |
34
|
|
|
*/ |
35
|
|
|
class EmailAttachmentSet |
36
|
|
|
{ |
37
|
|
|
/** @var EmailAttachment[] $attachments an array of EmailAttachment objects */ |
38
|
|
|
protected $attachments; |
39
|
|
|
|
40
|
|
|
/* assert messages */ |
41
|
|
|
protected const MESSAGE_ATTACHMENT = 'EmailAttachment is invalid'; |
42
|
|
|
protected const MESSAGE_LIST = 'EmailAttachment list is empty'; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* constructor. |
46
|
|
|
* |
47
|
|
|
* If an argument is null, the corresponding value will not be set. Values can be set |
48
|
|
|
* later with the with*() methods, but each will result in a new object. |
49
|
|
|
* |
50
|
|
|
* @param null|EmailAttachment[] $addresses an array of EmailAddress objects |
51
|
|
|
* |
52
|
|
|
* @throws \InvalidArgumentException |
53
|
|
|
* @throws \LogicException |
54
|
|
|
*/ |
55
|
9 |
|
public function __construct(?array $attachments = null) |
56
|
|
|
{ |
57
|
9 |
|
if (null!==$attachments) { |
58
|
3 |
|
Assert::allIsInstanceOf($attachments, EmailAttachment::class, static::MESSAGE_ATTACHMENT); |
59
|
|
|
try { |
60
|
|
|
/** @var EmailAttachment $attachment */ |
61
|
3 |
|
foreach ($attachments as $attachment) { |
62
|
3 |
|
$attachment->getFilename(); |
63
|
2 |
|
$attachment->getStringBody(); |
64
|
|
|
} |
65
|
1 |
|
} catch (\LogicException $e) { |
66
|
1 |
|
throw new \InvalidArgumentException($e->getMessage(), $e->getCode(), $e); |
67
|
|
|
} |
68
|
2 |
|
$this->attachments = $attachments; |
69
|
|
|
} |
70
|
9 |
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* withAddedAttachments - return a new object with the supplied EmailAddress array added |
74
|
|
|
* |
75
|
|
|
* @param EmailAttachment[] $attachments an array of EmailAttachment objects |
76
|
|
|
* |
77
|
|
|
* @return EmailAttachmentSet |
78
|
|
|
* |
79
|
|
|
* @throws \InvalidArgumentException |
80
|
|
|
*/ |
81
|
3 |
|
public function withAddedAttachments(array $attachments) : EmailAttachmentSet |
82
|
|
|
{ |
83
|
3 |
|
Assert::allIsInstanceOf($attachments, EmailAttachment::class, static::MESSAGE_ATTACHMENT); |
84
|
|
|
try { |
85
|
|
|
/** @var EmailAttachment $attachment */ |
86
|
3 |
|
foreach ($attachments as $attachment) { |
87
|
3 |
|
$attachment->getFilename(); |
88
|
2 |
|
$attachment->getStringBody(); |
89
|
|
|
} |
90
|
2 |
|
$existingAttachments = (null === $this->attachments) ? [] : $this->getAttachments(); |
91
|
1 |
|
} catch (\LogicException $e) { |
92
|
1 |
|
throw new \InvalidArgumentException($e->getMessage(), $e->getCode(), $e); |
93
|
|
|
} |
94
|
2 |
|
$new = clone $this; |
95
|
2 |
|
$new->attachments = array_merge($existingAttachments, $attachments); |
96
|
2 |
|
return $new; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* getAttachments |
101
|
|
|
* |
102
|
|
|
* @return EmailAttachment[] an array of EmailAttachment objects |
103
|
|
|
* |
104
|
|
|
* @throws \LogicException (property was not properly set before used) |
105
|
|
|
*/ |
106
|
6 |
|
public function getAttachments() : array |
107
|
|
|
{ |
108
|
|
|
try { |
109
|
6 |
|
Assert::notNull($this->attachments, static::MESSAGE_LIST); |
110
|
4 |
|
Assert::allIsInstanceOf($this->attachments, EmailAttachment::class, static::MESSAGE_ATTACHMENT); |
111
|
|
|
/** @var EmailAttachment $attachment */ |
112
|
4 |
|
foreach ($this->attachments as $attachment) { |
113
|
4 |
|
$attachment->getFilename(); |
114
|
4 |
|
$attachment->getStringBody(); |
115
|
|
|
} |
116
|
4 |
|
} catch (\InvalidArgumentException $e) { |
117
|
4 |
|
throw new \LogicException($e->getMessage(), $e->getCode(), $e); |
118
|
|
|
} |
119
|
4 |
|
return $this->attachments; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* getEachAttachment - return each EmailAttachment in the list |
124
|
|
|
* |
125
|
|
|
* @return \Generator|EmailAttachment[] |
126
|
|
|
* |
127
|
|
|
* @throws \LogicException (property was not properly set before used) |
128
|
|
|
*/ |
129
|
2 |
|
public function getEachAttachment() : \Generator |
130
|
|
|
{ |
131
|
2 |
|
$this->getAttachments(); |
132
|
1 |
|
foreach ($this->attachments as $attachment) { |
133
|
1 |
|
yield $attachment; |
134
|
|
|
} |
135
|
1 |
|
} |
136
|
|
|
} |
137
|
|
|
|