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