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