|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Kerox\Messenger\Helper; |
|
6
|
|
|
|
|
7
|
|
|
use InvalidArgumentException; |
|
8
|
|
|
use Kerox\Messenger\Model\Common\Button\AbstractButton; |
|
9
|
|
|
use Kerox\Messenger\Model\Message; |
|
10
|
|
|
use Kerox\Messenger\Model\Message\Attachment; |
|
11
|
|
|
use Kerox\Messenger\Model\Message\Attachment\Template\GenericTemplate; |
|
12
|
|
|
use Kerox\Messenger\SendInterface; |
|
13
|
|
|
|
|
14
|
|
|
trait ValidatorTrait |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @param string $value |
|
18
|
|
|
* |
|
19
|
|
|
* @throws \InvalidArgumentException |
|
20
|
|
|
*/ |
|
21
|
2 |
|
protected function isValidColor(string $value): void |
|
22
|
|
|
{ |
|
23
|
2 |
|
if (!preg_match('/^#[A-Fa-f0-9]{6}$/', $value)) { |
|
24
|
1 |
|
throw new InvalidArgumentException('The color must be expressed in #rrggbb format.'); |
|
25
|
|
|
} |
|
26
|
1 |
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @param string $value |
|
30
|
|
|
* @param int $length |
|
31
|
|
|
* |
|
32
|
|
|
* @throws \InvalidArgumentException |
|
33
|
|
|
*/ |
|
34
|
38 |
|
protected function isValidString(string $value, int $length = 20): void |
|
35
|
|
|
{ |
|
36
|
38 |
|
if (mb_strlen($value) > $length) { |
|
37
|
1 |
|
throw new InvalidArgumentException("String should not exceed {$length} characters."); |
|
38
|
|
|
} |
|
39
|
37 |
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @param string $value |
|
43
|
|
|
* |
|
44
|
|
|
* @throws \InvalidArgumentException |
|
45
|
|
|
*/ |
|
46
|
32 |
|
protected function isValidUrl(string $value): void |
|
47
|
|
|
{ |
|
48
|
32 |
|
if (!preg_match( |
|
49
|
32 |
|
'/^https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&\/=]*)$/', |
|
50
|
32 |
|
$value |
|
51
|
|
|
)) { |
|
52
|
1 |
|
throw new InvalidArgumentException("{$value} is not a valid url."); |
|
53
|
|
|
} |
|
54
|
31 |
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @param string $value |
|
58
|
|
|
* |
|
59
|
|
|
* @throws \InvalidArgumentException |
|
60
|
|
|
*/ |
|
61
|
8 |
|
protected function isValidLocale(string $value): void |
|
62
|
|
|
{ |
|
63
|
8 |
|
if (!preg_match('/^[a-z]{2}_[A-Z]{2}$/', $value)) { |
|
64
|
1 |
|
throw new InvalidArgumentException( |
|
65
|
1 |
|
"{$value} is not valid. Locale must be in ISO-639-1 and ISO-3166-1 format like fr_FR." |
|
66
|
|
|
); |
|
67
|
|
|
} |
|
68
|
7 |
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @param string $value |
|
72
|
|
|
* |
|
73
|
|
|
* @throws \InvalidArgumentException |
|
74
|
|
|
*/ |
|
75
|
2 |
|
protected function isValidCountry(string $value): void |
|
76
|
|
|
{ |
|
77
|
2 |
|
if (!preg_match('/^[A-Z]{2}$/', $value)) { |
|
78
|
1 |
|
throw new InvalidArgumentException( |
|
79
|
1 |
|
"{$value} is not valid. Country must be in ISO 3166 Alpha-2 format like FR." |
|
80
|
|
|
); |
|
81
|
|
|
} |
|
82
|
1 |
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @param string $value |
|
86
|
|
|
* |
|
87
|
|
|
* @throws \InvalidArgumentException |
|
88
|
|
|
*/ |
|
89
|
11 |
|
protected function isValidDateTime(string $value): void |
|
90
|
|
|
{ |
|
91
|
11 |
|
if (!preg_match('/^(\d{4})-(0[1-9]|1[0-2])-([12]\d|0[1-9]|3[01])T(0\d|1\d|2[0-3]):([0-5]\d)$/', $value)) { |
|
92
|
1 |
|
throw new InvalidArgumentException( |
|
93
|
1 |
|
"{$value} is not valid. DateTime must be in ISO-8601 AAAA-MM-JJThh:mm format." |
|
94
|
|
|
); |
|
95
|
|
|
} |
|
96
|
10 |
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* @param array $array |
|
100
|
|
|
* @param int $maxSize |
|
101
|
|
|
* @param int $minSize |
|
102
|
|
|
* |
|
103
|
|
|
* @throws \InvalidArgumentException |
|
104
|
|
|
*/ |
|
105
|
27 |
|
protected function isValidArray(array $array, int $maxSize, ?int $minSize = null): void |
|
106
|
|
|
{ |
|
107
|
27 |
|
$countArray = \count($array); |
|
108
|
27 |
|
if ($minSize !== null && $countArray < $minSize) { |
|
109
|
1 |
|
throw new InvalidArgumentException("The minimum number of items for this array is {$minSize}."); |
|
110
|
|
|
} |
|
111
|
26 |
|
if ($countArray > $maxSize) { |
|
112
|
3 |
|
throw new InvalidArgumentException("The maximum number of items for this array is {$maxSize}."); |
|
113
|
|
|
} |
|
114
|
25 |
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* @param string $value |
|
118
|
|
|
* |
|
119
|
|
|
* @throws \InvalidArgumentException |
|
120
|
|
|
*/ |
|
121
|
7 |
|
protected function isValidCurrency(string $value): void |
|
122
|
|
|
{ |
|
123
|
7 |
|
$allowedCurrency = $this->getAllowedCurrency(); |
|
124
|
|
|
|
|
125
|
7 |
|
$regex = '/^' . implode('|', $allowedCurrency) . '$/'; |
|
126
|
7 |
|
if (!preg_match($regex, $value)) { |
|
127
|
1 |
|
throw new InvalidArgumentException(sprintf( |
|
128
|
1 |
|
'%s is not a valid currency. Currency must be in ISO-4217-3 format.', |
|
129
|
1 |
|
$value |
|
130
|
|
|
)); |
|
131
|
|
|
} |
|
132
|
6 |
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* @param string $filename |
|
136
|
|
|
* @param array $allowedExtension |
|
137
|
|
|
* |
|
138
|
|
|
* @throws \InvalidArgumentException |
|
139
|
|
|
*/ |
|
140
|
3 |
|
protected function isValidExtension(string $filename, array $allowedExtension): void |
|
141
|
|
|
{ |
|
142
|
3 |
|
$ext = pathinfo($filename, PATHINFO_EXTENSION); |
|
143
|
3 |
|
if (empty($ext) || !\in_array($ext, $allowedExtension, true)) { |
|
144
|
1 |
|
throw new InvalidArgumentException(sprintf( |
|
145
|
1 |
|
"%s doesn't have a valid extension. Allowed extensions are %s.", |
|
146
|
1 |
|
$filename, |
|
147
|
1 |
|
implode(', ', $allowedExtension) |
|
148
|
|
|
)); |
|
149
|
|
|
} |
|
150
|
2 |
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* @param \Kerox\Messenger\Model\Common\Button\AbstractButton[] $buttons |
|
154
|
|
|
* @param array $allowedButtonsType |
|
155
|
|
|
* |
|
156
|
|
|
* @throws \InvalidArgumentException |
|
157
|
|
|
*/ |
|
158
|
8 |
|
protected function isValidButtons(array $buttons, array $allowedButtonsType): void |
|
159
|
|
|
{ |
|
160
|
|
|
/** @var \Kerox\Messenger\Model\Common\Button\AbstractButton $button */ |
|
161
|
8 |
|
foreach ($buttons as $button) { |
|
162
|
8 |
|
if (!$button instanceof AbstractButton) { |
|
163
|
1 |
|
throw new \InvalidArgumentException( |
|
164
|
1 |
|
sprintf('Array can only contain instance of %s.', AbstractButton::class) |
|
165
|
|
|
); |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
7 |
|
if (!\in_array($button->getType(), $allowedButtonsType, true)) { |
|
169
|
3 |
|
throw new \InvalidArgumentException(sprintf( |
|
170
|
3 |
|
'Buttons can only be an instance of %s.', |
|
171
|
7 |
|
implode(', ', $allowedButtonsType) |
|
172
|
|
|
)); |
|
173
|
|
|
} |
|
174
|
|
|
} |
|
175
|
5 |
|
} |
|
176
|
|
|
|
|
177
|
|
|
/** |
|
178
|
|
|
* @param $message |
|
179
|
|
|
* |
|
180
|
|
|
* @throws \Exception |
|
181
|
|
|
* |
|
182
|
|
|
* @return \Kerox\Messenger\Model\Message |
|
183
|
|
|
*/ |
|
184
|
11 |
|
protected function isValidMessage($message): Message |
|
185
|
|
|
{ |
|
186
|
11 |
|
if ($message instanceof Message) { |
|
187
|
1 |
|
return $message; |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
10 |
|
if (\is_string($message) || $message instanceof Attachment) { |
|
191
|
9 |
|
return Message::create($message); |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
1 |
|
throw new \InvalidArgumentException( |
|
195
|
1 |
|
sprintf('message must be a string or an instance of %s or %s.', Message::class, Attachment::class) |
|
196
|
|
|
); |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
/** |
|
200
|
|
|
* @param string $action |
|
201
|
|
|
* |
|
202
|
|
|
* @throws \InvalidArgumentException |
|
203
|
|
|
*/ |
|
204
|
2 |
|
protected function isValidSenderAction(string $action): void |
|
205
|
|
|
{ |
|
206
|
2 |
|
$allowedSenderAction = $this->getAllowedSenderAction(); |
|
207
|
2 |
|
if (!\in_array($action, $allowedSenderAction, true)) { |
|
208
|
1 |
|
throw new \InvalidArgumentException(sprintf( |
|
209
|
1 |
|
'action must be either %s.', |
|
210
|
1 |
|
implode(', ', $allowedSenderAction) |
|
211
|
|
|
)); |
|
212
|
|
|
} |
|
213
|
1 |
|
} |
|
214
|
|
|
|
|
215
|
|
|
/** |
|
216
|
|
|
* @param string $notificationType |
|
217
|
|
|
* |
|
218
|
|
|
* @throws \InvalidArgumentException |
|
219
|
|
|
*/ |
|
220
|
5 |
|
protected function isValidNotificationType(string $notificationType): void |
|
221
|
|
|
{ |
|
222
|
5 |
|
$allowedNotificationType = $this->getAllowedNotificationType(); |
|
223
|
5 |
|
if (!\in_array($notificationType, $allowedNotificationType, true)) { |
|
224
|
1 |
|
throw new \InvalidArgumentException(sprintf( |
|
225
|
1 |
|
'notificationType must be either %s.', |
|
226
|
1 |
|
implode(', ', $allowedNotificationType) |
|
227
|
|
|
)); |
|
228
|
|
|
} |
|
229
|
4 |
|
} |
|
230
|
|
|
|
|
231
|
|
|
/** |
|
232
|
|
|
* @param string $tag |
|
233
|
|
|
* @param \Kerox\Messenger\Model\Message $message |
|
234
|
|
|
* |
|
235
|
|
|
* @throws \InvalidArgumentException |
|
236
|
|
|
*/ |
|
237
|
3 |
|
protected function isValidTag(string $tag, Message $message = null): void |
|
238
|
|
|
{ |
|
239
|
3 |
|
$allowedTag = $this->getAllowedTag(); |
|
240
|
3 |
|
if (!\in_array($tag, $allowedTag, true)) { |
|
241
|
1 |
|
throw new \InvalidArgumentException(sprintf( |
|
242
|
1 |
|
'tag must be either %s.', |
|
243
|
1 |
|
implode(', ', $allowedTag) |
|
244
|
|
|
)); |
|
245
|
|
|
} |
|
246
|
|
|
|
|
247
|
2 |
|
if ($tag === SendInterface::TAG_ISSUE_RESOLUTION && $message !== null && !$message instanceof GenericTemplate) { |
|
248
|
1 |
|
throw new \InvalidArgumentException(sprintf( |
|
249
|
1 |
|
'message must be an instance of %s if tag is set to %s.', |
|
250
|
1 |
|
GenericTemplate::class, |
|
251
|
1 |
|
SendInterface::TAG_ISSUE_RESOLUTION |
|
252
|
|
|
)); |
|
253
|
|
|
} |
|
254
|
1 |
|
} |
|
255
|
|
|
|
|
256
|
|
|
/** |
|
257
|
|
|
* @return array |
|
258
|
|
|
*/ |
|
259
|
2 |
|
public function getAllowedSenderAction(): array |
|
260
|
|
|
{ |
|
261
|
|
|
return [ |
|
262
|
2 |
|
SendInterface::SENDER_ACTION_TYPING_ON, |
|
263
|
2 |
|
SendInterface::SENDER_ACTION_TYPING_OFF, |
|
264
|
2 |
|
SendInterface::SENDER_ACTION_MARK_SEEN, |
|
265
|
|
|
]; |
|
266
|
|
|
} |
|
267
|
|
|
|
|
268
|
|
|
/** |
|
269
|
|
|
* @return array |
|
270
|
|
|
*/ |
|
271
|
5 |
|
public function getAllowedNotificationType(): array |
|
272
|
|
|
{ |
|
273
|
|
|
return [ |
|
274
|
5 |
|
SendInterface::NOTIFICATION_TYPE_REGULAR, |
|
275
|
5 |
|
SendInterface::NOTIFICATION_TYPE_SILENT_PUSH, |
|
276
|
5 |
|
SendInterface::NOTIFICATION_TYPE_NO_PUSH, |
|
277
|
|
|
]; |
|
278
|
|
|
} |
|
279
|
|
|
|
|
280
|
|
|
/** |
|
281
|
|
|
* @return array |
|
282
|
|
|
*/ |
|
283
|
3 |
|
public function getAllowedTag(): array |
|
284
|
|
|
{ |
|
285
|
|
|
return [ |
|
286
|
3 |
|
SendInterface::TAG_COMMUNITY_ALERT, |
|
287
|
3 |
|
SendInterface::TAG_CONFIRMED_EVENT_REMINDER, |
|
288
|
3 |
|
SendInterface::TAG_NON_PROMOTIONAL_SUBSCRIPTION, |
|
289
|
3 |
|
SendInterface::TAG_PAIRING_UPDATE, |
|
290
|
3 |
|
SendInterface::TAG_APPLICATION_UPDATE, |
|
291
|
3 |
|
SendInterface::TAG_ACCOUNT_UPDATE, |
|
292
|
3 |
|
SendInterface::TAG_PAYMENT_UPDATE, |
|
293
|
3 |
|
SendInterface::TAG_PERSONAL_FINANCE_UPDATE, |
|
294
|
3 |
|
SendInterface::TAG_SHIPPING_UPDATE, |
|
295
|
3 |
|
SendInterface::TAG_RESERVATION_UPDATE, |
|
296
|
3 |
|
SendInterface::TAG_ISSUE_RESOLUTION, |
|
297
|
3 |
|
SendInterface::TAG_APPOINTMENT_UPDATE, |
|
298
|
3 |
|
SendInterface::TAG_GAME_EVENT, |
|
299
|
3 |
|
SendInterface::TAG_TRANSPORTATION_UPDATE, |
|
300
|
3 |
|
SendInterface::TAG_FEATURE_FUNCTIONALITY_UPDATE, |
|
301
|
3 |
|
SendInterface::TAG_TICKET_UPDATE, |
|
302
|
|
|
]; |
|
303
|
|
|
} |
|
304
|
|
|
|
|
305
|
|
|
/** |
|
306
|
|
|
* @return array |
|
307
|
|
|
*/ |
|
308
|
7 |
|
public function getAllowedCurrency(): array |
|
309
|
|
|
{ |
|
310
|
|
|
return [ |
|
311
|
7 |
|
'SGD', |
|
312
|
|
|
'RON', |
|
313
|
|
|
'EUR', |
|
314
|
|
|
'TRY', |
|
315
|
|
|
'SEK', |
|
316
|
|
|
'ZAR', |
|
317
|
|
|
'HKD', |
|
318
|
|
|
'CHF', |
|
319
|
|
|
'NIO', |
|
320
|
|
|
'JPY', |
|
321
|
|
|
'ISK', |
|
322
|
|
|
'TWD', |
|
323
|
|
|
'NZD', |
|
324
|
|
|
'CZK', |
|
325
|
|
|
'AUD', |
|
326
|
|
|
'THB', |
|
327
|
|
|
'BOB', |
|
328
|
|
|
'BRL', |
|
329
|
|
|
'MXN', |
|
330
|
|
|
'USD', |
|
331
|
|
|
'ILS', |
|
332
|
|
|
'HNL', |
|
333
|
|
|
'MOP', |
|
334
|
|
|
'COP', |
|
335
|
|
|
'UYU', |
|
336
|
|
|
'CRC', |
|
337
|
|
|
'DKK', |
|
338
|
|
|
'QAR', |
|
339
|
|
|
'PYG', |
|
340
|
|
|
'CAD', |
|
341
|
|
|
'INR', |
|
342
|
|
|
'KRW', |
|
343
|
|
|
'GTQ', |
|
344
|
|
|
'AED', |
|
345
|
|
|
'VEF', |
|
346
|
|
|
'SAR', |
|
347
|
|
|
'NOK', |
|
348
|
|
|
'CNY', |
|
349
|
|
|
'ARS', |
|
350
|
|
|
'PLN', |
|
351
|
|
|
'GBP', |
|
352
|
|
|
'PEN', |
|
353
|
|
|
'PHP', |
|
354
|
|
|
'VND', |
|
355
|
|
|
'RUB', |
|
356
|
|
|
'HUF', |
|
357
|
|
|
'MYR', |
|
358
|
|
|
'CLP', |
|
359
|
|
|
'IDR', |
|
360
|
|
|
]; |
|
361
|
|
|
} |
|
362
|
|
|
} |
|
363
|
|
|
|