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