1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @license https://github.com/f500/swiftmailer-sparkpost/blob/master/LICENSE Proprietary |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
namespace SwiftSparkPost; |
8
|
|
|
|
9
|
|
|
use Swift_Message; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @copyright Future500 B.V. |
13
|
|
|
* @author Jasper N. Brouwer <[email protected]> |
14
|
|
|
*/ |
15
|
|
|
final class Message extends Swift_Message |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var string |
19
|
|
|
*/ |
20
|
|
|
private $campaignId; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var array |
24
|
|
|
*/ |
25
|
|
|
private $perRecipientTags; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var array |
29
|
|
|
*/ |
30
|
|
|
private $metadata; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var array |
34
|
|
|
*/ |
35
|
|
|
private $perRecipientMetadata; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var array |
39
|
|
|
*/ |
40
|
|
|
private $substitutionData; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var array |
44
|
|
|
*/ |
45
|
|
|
private $perRecipientSubstitutionData; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var array |
49
|
|
|
*/ |
50
|
|
|
private $options; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Create a new Message. |
54
|
|
|
* |
55
|
|
|
* @param string $subject |
56
|
|
|
* @param string $body |
57
|
|
|
* @param string $contentType |
58
|
|
|
* @param string $charset |
59
|
|
|
* |
60
|
|
|
* @return $this |
61
|
|
|
*/ |
62
|
30 |
|
public static function newInstance($subject = null, $body = null, $contentType = null, $charset = null) |
63
|
|
|
{ |
64
|
30 |
|
return new self($subject, $body, $contentType, $charset); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* {@inheritdoc} |
69
|
|
|
*/ |
70
|
69 |
|
public function __construct($subject = null, $body = null, $contentType = null, $charset = null) |
71
|
|
|
{ |
72
|
69 |
|
parent::__construct($subject, $body, $contentType, $charset); |
73
|
|
|
|
74
|
69 |
|
$this->campaignId = ''; |
75
|
69 |
|
$this->perRecipientTags = []; |
76
|
69 |
|
$this->metadata = []; |
77
|
69 |
|
$this->perRecipientMetadata = []; |
78
|
69 |
|
$this->substitutionData = []; |
79
|
69 |
|
$this->perRecipientSubstitutionData = []; |
80
|
|
|
|
81
|
69 |
|
$this->options = [ |
82
|
23 |
|
'transactional' => true, |
83
|
23 |
|
'inline_css' => true, |
84
|
|
|
]; |
85
|
69 |
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @return string |
89
|
|
|
*/ |
90
|
27 |
|
public function getCampaignId() |
91
|
|
|
{ |
92
|
27 |
|
return $this->campaignId; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param string $campaignId |
97
|
|
|
* |
98
|
|
|
* @return Message |
99
|
|
|
*/ |
100
|
6 |
|
public function setCampaignId($campaignId) |
101
|
|
|
{ |
102
|
6 |
|
$this->campaignId = $campaignId; |
103
|
|
|
|
104
|
6 |
|
return $this; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @return array |
109
|
|
|
*/ |
110
|
27 |
|
public function getPerRecipientTags() |
111
|
|
|
{ |
112
|
27 |
|
return $this->perRecipientTags; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @param string $recipient |
117
|
|
|
* @param array $tags |
118
|
|
|
* |
119
|
|
|
* @return Message |
120
|
|
|
*/ |
121
|
6 |
|
public function setPerRecipientTags($recipient, array $tags) |
122
|
|
|
{ |
123
|
6 |
|
$this->perRecipientTags[(string) $recipient] = $this->sanitizeTags($tags); |
124
|
|
|
|
125
|
6 |
|
return $this; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @return array |
130
|
|
|
*/ |
131
|
27 |
|
public function getMetadata() |
132
|
|
|
{ |
133
|
27 |
|
return $this->metadata; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @param array $metadata |
138
|
|
|
* |
139
|
|
|
* @return Message |
140
|
|
|
*/ |
141
|
12 |
|
public function setMetadata(array $metadata) |
142
|
|
|
{ |
143
|
12 |
|
$this->metadata = $this->sanitizeMetadata($metadata); |
144
|
|
|
|
145
|
6 |
|
return $this; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @return array |
150
|
|
|
*/ |
151
|
27 |
|
public function getPerRecipientMetadata() |
152
|
|
|
{ |
153
|
27 |
|
return $this->perRecipientMetadata; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @param string $recipient |
158
|
|
|
* @param array $metadata |
159
|
|
|
* |
160
|
|
|
* @return Message |
161
|
|
|
*/ |
162
|
12 |
|
public function setPerRecipientMetadata($recipient, array $metadata) |
163
|
|
|
{ |
164
|
12 |
|
$this->perRecipientMetadata[(string) $recipient] = $this->sanitizeMetadata($metadata); |
165
|
|
|
|
166
|
6 |
|
return $this; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* @return array |
171
|
|
|
*/ |
172
|
27 |
|
public function getSubstitutionData() |
173
|
|
|
{ |
174
|
27 |
|
return $this->substitutionData; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* @param array $substitutionData |
179
|
|
|
* |
180
|
|
|
* @return Message |
181
|
|
|
*/ |
182
|
6 |
|
public function setSubstitutionData(array $substitutionData) |
183
|
|
|
{ |
184
|
6 |
|
$this->substitutionData = $this->sanitizeubstitutionData($substitutionData); |
185
|
|
|
|
186
|
6 |
|
return $this; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* @return array |
191
|
|
|
*/ |
192
|
27 |
|
public function getPerRecipientSubstitutionData() |
193
|
|
|
{ |
194
|
27 |
|
return $this->perRecipientSubstitutionData; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* @param string $recipient |
199
|
|
|
* @param array $substitutionData |
200
|
|
|
* |
201
|
|
|
* @return Message |
202
|
|
|
*/ |
203
|
6 |
|
public function setPerRecipientSubstitutionData($recipient, array $substitutionData) |
204
|
|
|
{ |
205
|
6 |
|
$this->perRecipientSubstitutionData[(string) $recipient] = $this->sanitizeubstitutionData($substitutionData); |
206
|
|
|
|
207
|
6 |
|
return $this; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* @return array |
212
|
|
|
*/ |
213
|
27 |
|
public function getOptions() |
214
|
|
|
{ |
215
|
27 |
|
return $this->options; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* @param array $options |
220
|
|
|
* |
221
|
|
|
* @return Message |
222
|
|
|
*/ |
223
|
9 |
|
public function setOptions(array $options) |
224
|
|
|
{ |
225
|
9 |
|
$this->options = $this->sanitizeOptions($options); |
226
|
|
|
|
227
|
6 |
|
return $this; |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* @param array $tags |
232
|
|
|
* |
233
|
|
|
* @return array |
234
|
|
|
*/ |
235
|
6 |
|
private function sanitizeTags(array $tags) |
236
|
|
|
{ |
237
|
6 |
|
$sanitized = []; |
238
|
|
|
|
239
|
6 |
|
foreach ($tags as $tag) { |
240
|
6 |
|
$sanitized[] = (string) $tag; |
241
|
2 |
|
} |
242
|
|
|
|
243
|
6 |
|
return $sanitized; |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* @param array $metadata |
248
|
|
|
* |
249
|
|
|
* @return array |
250
|
|
|
*/ |
251
|
18 |
|
private function sanitizeMetadata(array $metadata) |
252
|
|
|
{ |
253
|
18 |
|
array_walk_recursive( |
254
|
6 |
|
$metadata, |
255
|
18 |
|
function ($value) { |
256
|
18 |
|
if (is_object($value) or is_resource($value)) { |
|
|
|
|
257
|
12 |
|
throw new Exception('Metadata cannot contain objects or resources'); |
258
|
|
|
} |
259
|
14 |
|
} |
260
|
6 |
|
); |
261
|
|
|
|
262
|
6 |
|
return $metadata; |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
/** |
266
|
|
|
* @param array $substitutionData |
267
|
|
|
* |
268
|
|
|
* @return array |
269
|
|
|
*/ |
270
|
6 |
|
private function sanitizeubstitutionData(array $substitutionData) |
271
|
|
|
{ |
272
|
6 |
|
$sanitized = []; |
273
|
|
|
|
274
|
6 |
|
foreach ($substitutionData as $key => $value) { |
275
|
6 |
|
$sanitized[(string) $key] = (string) $value; |
276
|
2 |
|
} |
277
|
|
|
|
278
|
6 |
|
return $sanitized; |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
/** |
282
|
|
|
* @param array $options |
283
|
|
|
* |
284
|
|
|
* @return array |
285
|
|
|
* @throws Exception |
286
|
|
|
*/ |
287
|
9 |
|
private function sanitizeOptions(array $options) |
288
|
|
|
{ |
289
|
9 |
|
$sanitized = []; |
290
|
|
|
|
291
|
9 |
|
foreach ($options as $key => $value) { |
292
|
|
|
switch ($key) { |
293
|
9 |
|
case 'open_tracking': |
294
|
9 |
|
case 'click_tracking': |
295
|
9 |
|
case 'transactional': |
296
|
9 |
|
case 'sandbox': |
297
|
9 |
|
case 'skip_suppression': |
298
|
9 |
|
case 'inline_css': |
299
|
6 |
|
$sanitized[$key] = (bool) $value; |
300
|
6 |
|
break; |
301
|
9 |
|
case 'ip_pool': |
302
|
6 |
|
$sanitized[$key] = (string) $value; |
303
|
6 |
|
break; |
304
|
1 |
|
default: |
305
|
7 |
|
throw new Exception(sprintf('Unknown SparkPost option "%s"', $key)); |
306
|
1 |
|
} |
307
|
2 |
|
} |
308
|
|
|
|
309
|
6 |
|
return $sanitized; |
310
|
|
|
} |
311
|
|
|
} |
312
|
|
|
|
PHP has two types of connecting operators (logical operators, and boolean operators):
and
&&
or
||
The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like
&&
, or||
.Let’s take a look at a few examples:
Logical Operators are used for Control-Flow
One case where you explicitly want to use logical operators is for control-flow such as this:
Since
die
introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined withthrow
at this point:These limitations lead to logical operators rarely being of use in current PHP code.