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
|
|
|
|
12
|
|
|
trait ValidatorTrait |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
2 |
|
* @param string $value |
16
|
|
|
* |
17
|
2 |
|
* @throws \InvalidArgumentException |
18
|
1 |
|
*/ |
19
|
|
|
protected function isValidColor(string $value): void |
20
|
1 |
|
{ |
21
|
|
|
if (!preg_match('/^#[A-Fa-f0-9]{6}$/', $value)) { |
22
|
|
|
throw new InvalidArgumentException('The color must be expressed in #rrggbb format.'); |
23
|
|
|
} |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param string $value |
28
|
31 |
|
* @param int $length |
29
|
|
|
* |
30
|
31 |
|
* @throws \InvalidArgumentException |
31
|
1 |
|
*/ |
32
|
|
|
protected function isValidString(string $value, int $length = 20): void |
33
|
30 |
|
{ |
34
|
|
|
if (mb_strlen($value) > $length) { |
35
|
|
|
throw new InvalidArgumentException("String should not exceed {$length} characters."); |
36
|
|
|
} |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
28 |
|
* @param string $value |
41
|
|
|
* |
42
|
28 |
|
* @throws \InvalidArgumentException |
43
|
1 |
|
*/ |
44
|
|
|
protected function isValidUrl(string $value): void |
45
|
27 |
|
{ |
46
|
|
|
if (!preg_match('/^https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&\/=]*)$/', $value)) { |
47
|
|
|
throw new InvalidArgumentException("{$value} is not a valid url."); |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
8 |
|
* @param string $value |
53
|
|
|
* |
54
|
8 |
|
* @throws \InvalidArgumentException |
55
|
1 |
|
*/ |
56
|
|
|
protected function isValidLocale(string $value): void |
57
|
7 |
|
{ |
58
|
|
|
if (!preg_match('/^[a-z]{2}_[A-Z]{2}$/', $value)) { |
59
|
|
|
throw new InvalidArgumentException( |
60
|
|
|
"{$value} is not valid. Locale must be in ISO-639-1 and ISO-3166-1 format like fr_FR." |
61
|
|
|
); |
62
|
|
|
} |
63
|
|
|
} |
64
|
2 |
|
|
65
|
|
|
/** |
66
|
2 |
|
* @param string $value |
67
|
1 |
|
* |
68
|
|
|
* @throws \InvalidArgumentException |
69
|
1 |
|
*/ |
70
|
|
|
protected function isValidCountry(string $value): void |
71
|
|
|
{ |
72
|
|
|
if (!preg_match('/^[A-Z]{2}$/', $value)) { |
73
|
|
|
throw new InvalidArgumentException( |
74
|
|
|
"{$value} is not valid. Country must be in ISO 3166 Alpha-2 format like FR." |
75
|
|
|
); |
76
|
11 |
|
} |
77
|
|
|
} |
78
|
11 |
|
|
79
|
1 |
|
/** |
80
|
|
|
* @param string $value |
81
|
10 |
|
* |
82
|
|
|
* @throws \InvalidArgumentException |
83
|
|
|
*/ |
84
|
|
|
protected function isValidDateTime(string $value): void |
85
|
|
|
{ |
86
|
|
|
if (!preg_match('/^(\d{4})-(0[1-9]|1[0-2])-([12]\d|0[1-9]|3[01])T(0[0-9]|1\d|2[0-3]):([0-5]\d)$/', $value)) { |
87
|
|
|
throw new InvalidArgumentException( |
88
|
|
|
"{$value} is not valid. DateTime must be in ISO-8601 AAAA-MM-JJThh:mm format" |
89
|
|
|
); |
90
|
24 |
|
} |
91
|
|
|
} |
92
|
24 |
|
|
93
|
24 |
|
/** |
94
|
1 |
|
* @param array $array |
95
|
|
|
* @param int $maxSize |
96
|
23 |
|
* @param int $minSize |
97
|
3 |
|
* |
98
|
|
|
* @throws \InvalidArgumentException |
99
|
22 |
|
*/ |
100
|
|
|
protected function isValidArray(array $array, int $maxSize, ?int $minSize = null): void |
101
|
|
|
{ |
102
|
|
|
$countArray = \count($array); |
103
|
|
|
if ($minSize !== null && $countArray < $minSize) { |
104
|
|
|
throw new InvalidArgumentException("The minimum number of items for this array is {$minSize}."); |
105
|
|
|
} |
106
|
6 |
|
if ($countArray > $maxSize) { |
107
|
|
|
throw new InvalidArgumentException("The maximum number of items for this array is {$maxSize}."); |
108
|
6 |
|
} |
109
|
1 |
|
} |
110
|
|
|
|
111
|
5 |
|
/** |
112
|
|
|
* @param string $value |
113
|
|
|
* |
114
|
|
|
* @throws \InvalidArgumentException |
115
|
|
|
*/ |
116
|
|
|
protected function isValidCurrency(string $value): void |
117
|
|
|
{ |
118
|
|
|
if (!preg_match('/^SGD|RON|EUR|TRY|SEK|ZAR|HKD|CHF|NIO|JPY|ISK|TWD|NZD|CZK|AUD|THB|BOB|BRL|MXN|USD|ILS|HNL|MOP|COP|UYU|CRC|DKK|QAR|PYG|CAD|INR|KRW|GTQ|AED|VEF|SAR|NOK|CNY|ARS|PLN|GBP|PEN|PHP|VND|RUB|HUF|MYR|CLP|IDR$/', $value)) { |
119
|
3 |
|
throw new InvalidArgumentException( |
120
|
|
|
"{$value} is not a valid currency. Currency must be in ISO-4217-3 format." |
121
|
3 |
|
); |
122
|
3 |
|
} |
123
|
1 |
|
} |
124
|
|
|
|
125
|
2 |
|
/** |
126
|
|
|
* @param string $filename |
127
|
|
|
* @param array $allowedExtension |
128
|
|
|
* |
129
|
|
|
* @throws \InvalidArgumentException |
130
|
|
|
*/ |
131
|
6 |
|
protected function isValidExtension(string $filename, array $allowedExtension): void |
132
|
|
|
{ |
133
|
|
|
$ext = pathinfo($filename, PATHINFO_EXTENSION); |
134
|
6 |
|
if (empty($ext) || !\in_array($ext, $allowedExtension, true)) { |
135
|
6 |
|
throw new InvalidArgumentException( |
136
|
1 |
|
"{$filename} doesn't have a valid extension. Allowed extensions are " . implode(', ', $allowedExtension) |
137
|
|
|
); |
138
|
|
|
} |
139
|
5 |
|
} |
140
|
5 |
|
|
141
|
|
|
/** |
142
|
|
|
* @param \Kerox\Messenger\Model\Common\Button\AbstractButton[] $buttons |
143
|
4 |
|
* @param array $allowedButtonsType |
144
|
|
|
* |
145
|
|
|
* @throws \InvalidArgumentException |
146
|
|
|
*/ |
147
|
|
|
protected function isValidButtons(array $buttons, array $allowedButtonsType): void |
148
|
|
|
{ |
149
|
|
|
/** @var \Kerox\Messenger\Model\Common\Button\AbstractButton $button */ |
150
|
|
|
foreach ($buttons as $button) { |
151
|
|
|
if (!$button instanceof AbstractButton) { |
152
|
|
|
throw new \InvalidArgumentException('Array can only contain instance of AbstractButton.'); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
if (!\in_array($button->getType(), $allowedButtonsType, true)) { |
156
|
|
|
throw new \InvalidArgumentException( |
157
|
|
|
'Buttons can only be an instance of ' . implode(', ', $allowedButtonsType) |
158
|
|
|
); |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* @param $message |
165
|
|
|
* |
166
|
|
|
* @throws \Exception |
167
|
|
|
* |
168
|
|
|
* @return \Kerox\Messenger\Model\Message |
169
|
|
|
*/ |
170
|
|
|
private function isValidMessage($message): Message |
171
|
|
|
{ |
172
|
|
|
if ($message instanceof Message) { |
173
|
|
|
return $message; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
if (\is_string($message) || $message instanceof Attachment) { |
177
|
|
|
return Message::create($message); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
throw new \InvalidArgumentException('$message must be a string or an instance of Message or Attachment'); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* @param string $notificationType |
185
|
|
|
* @param array $allowedNotificationType |
186
|
|
|
* |
187
|
|
|
* @throws \InvalidArgumentException |
188
|
|
|
*/ |
189
|
|
|
protected function isValidNotificationType(string $notificationType, array $allowedNotificationType): void |
190
|
|
|
{ |
191
|
|
|
if (!\in_array($notificationType, $allowedNotificationType, true)) { |
192
|
|
|
throw new \InvalidArgumentException('$notificationType must be either ' . implode(', ', $allowedNotificationType)); |
193
|
|
|
} |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* @param string $tag |
198
|
|
|
* @param array $allowedTag |
199
|
|
|
* |
200
|
|
|
* @throws \InvalidArgumentException |
201
|
|
|
*/ |
202
|
|
|
protected function isValidTag(string $tag, array $allowedTag): void |
203
|
|
|
{ |
204
|
|
|
if (!\in_array($tag, $allowedTag, true)) { |
205
|
|
|
throw new \InvalidArgumentException('$tag must be either ' . implode(', ', $allowedTag)); |
206
|
|
|
} |
207
|
|
|
} |
208
|
|
|
} |
209
|
|
|
|