1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Kerox\Messenger\Helper; |
4
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
6
|
|
|
use Kerox\Messenger\Model\Common\Button\AbstractButton; |
7
|
|
|
|
8
|
|
|
trait ValidatorTrait |
9
|
|
|
{ |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @param string $value |
13
|
|
|
* @return void |
14
|
|
|
* @throws \InvalidArgumentException |
15
|
|
|
*/ |
16
|
2 |
|
protected function isValidColor(string $value) |
17
|
|
|
{ |
18
|
2 |
|
if (!preg_match('/^#[A-Fa-f0-9]{6}$/', $value)) { |
19
|
1 |
|
throw new InvalidArgumentException("The color must be expressed in #rrggbb format."); |
20
|
|
|
} |
21
|
1 |
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @param string $value |
25
|
|
|
* @param int $length |
26
|
|
|
* @return void |
27
|
|
|
* @throws \InvalidArgumentException |
28
|
|
|
*/ |
29
|
31 |
|
protected function isValidString(string $value, int $length = 20) |
30
|
|
|
{ |
31
|
31 |
|
if (mb_strlen($value) > $length) { |
32
|
1 |
|
throw new InvalidArgumentException("String should not exceed {$length} characters."); |
33
|
|
|
} |
34
|
30 |
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param string $value |
38
|
|
|
* @return void |
39
|
|
|
* @throws \InvalidArgumentException |
40
|
|
|
*/ |
41
|
33 |
|
protected function isValidUrl(string $value) |
42
|
|
|
{ |
43
|
33 |
|
if (!preg_match('/^https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&\/=]*)$/', $value)) { |
44
|
1 |
|
throw new InvalidArgumentException("{$value} is not a valid url."); |
45
|
|
|
} |
46
|
32 |
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param string $value |
50
|
|
|
* @return void |
51
|
|
|
* @throws \InvalidArgumentException |
52
|
|
|
*/ |
53
|
8 |
|
protected function isValidLocale(string $value) |
54
|
|
|
{ |
55
|
8 |
|
if (!preg_match('/^[a-z]{2}_[A-Z]{2}$/', $value)) { |
56
|
1 |
|
throw new InvalidArgumentException("{$value} is not valid. Locale must be in ISO-639-1 and ISO-3166-1 format like fr_FR."); |
57
|
|
|
} |
58
|
7 |
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param string $value |
62
|
|
|
* @return void |
63
|
|
|
* @throws \InvalidArgumentException |
64
|
|
|
*/ |
65
|
2 |
|
protected function isValidCountry(string $value) |
66
|
|
|
{ |
67
|
2 |
|
if (!preg_match('/^[A-Z]{2}$/', $value)) { |
68
|
1 |
|
throw new InvalidArgumentException("{$value} is not valid. Country must be in ISO 3166 Alpha-2 format like FR."); |
69
|
|
|
} |
70
|
1 |
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param string $value |
74
|
|
|
* @return void |
75
|
|
|
* @throws \InvalidArgumentException |
76
|
|
|
*/ |
77
|
11 |
|
protected function isValidDateTime(string $value) |
78
|
|
|
{ |
79
|
11 |
|
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)) { |
80
|
1 |
|
throw new InvalidArgumentException("{$value} is not valid. DateTime must be in ISO-8601 AAAA-MM-JJThh:mm format"); |
81
|
|
|
} |
82
|
10 |
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param array $array |
86
|
|
|
* @param int $maxSize |
87
|
|
|
* @param int $minSize |
88
|
|
|
* @return void |
89
|
|
|
* @throws \InvalidArgumentException |
90
|
|
|
*/ |
91
|
26 |
|
protected function isValidArray(array $array, int $maxSize, int $minSize = null) |
92
|
|
|
{ |
93
|
26 |
|
$countArray = count($array); |
94
|
26 |
|
if ($minSize !== null && $countArray < $minSize) { |
95
|
1 |
|
throw new InvalidArgumentException("The minimum number of items for this array is {$minSize}."); |
96
|
|
|
} |
97
|
25 |
|
if ($countArray > $maxSize) { |
98
|
3 |
|
throw new InvalidArgumentException("The maximum number of items for this array is {$maxSize}."); |
99
|
|
|
} |
100
|
24 |
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param string $value |
104
|
|
|
* @return void |
105
|
|
|
* @throws \InvalidArgumentException |
106
|
|
|
*/ |
107
|
6 |
|
protected function isValidCurrency(string $value) |
108
|
|
|
{ |
109
|
6 |
|
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)) { |
110
|
1 |
|
throw new InvalidArgumentException("{$value} is not a valid currency. Currency must be in ISO-4217-3 format."); |
111
|
|
|
} |
112
|
5 |
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @param string $filename |
116
|
|
|
* @param array $allowedExtension |
117
|
|
|
* @return void |
118
|
|
|
* @throws \InvalidArgumentException |
119
|
|
|
*/ |
120
|
3 |
|
protected function isValidExtension(string $filename, array $allowedExtension) |
121
|
|
|
{ |
122
|
3 |
|
$ext = pathinfo($filename, PATHINFO_EXTENSION); |
123
|
3 |
|
if (empty($ext) || !in_array($ext, $allowedExtension)) { |
124
|
1 |
|
throw new InvalidArgumentException("{$filename} doesn't have a valid extension. Allowed extensions are " . implode(', ', $allowedExtension)); |
125
|
|
|
} |
126
|
2 |
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @param \Kerox\Messenger\Model\Common\Button\AbstractButton[] $buttons |
130
|
|
|
* @param array $allowedButtonsType |
131
|
|
|
* @return void |
132
|
|
|
*/ |
133
|
6 |
|
protected function isValidButtons(array $buttons, array $allowedButtonsType) |
134
|
|
|
{ |
135
|
|
|
/** @var \Kerox\Messenger\Model\Common\Button\AbstractButton $button */ |
136
|
6 |
|
foreach ($buttons as $button) { |
137
|
6 |
|
if (!$button instanceof AbstractButton) { |
138
|
|
|
throw new \InvalidArgumentException('Array can only contain instance of AbstractButton.'); |
139
|
|
|
} |
140
|
|
|
|
141
|
6 |
|
if (!in_array($button->getType(), $allowedButtonsType)) { |
142
|
6 |
|
throw new \InvalidArgumentException('Buttons can only be an instance of ' . implode(', ', $allowedButtonsType)); |
143
|
|
|
} |
144
|
|
|
} |
145
|
5 |
|
} |
146
|
|
|
} |
147
|
|
|
|