-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathZIPCodeValidator.php
232 lines (218 loc) · 6.15 KB
/
ZIPCodeValidator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
<?php
class ZIPCodeValidator
{
private static array $EUCountriesName = [
'Austria',
'Belgium',
'Bulgaria',
'Cyprus',
'Czechia',
'Germany',
'Denmark',
'Estonia',
'Greece',
'Spain',
'Finland',
'France',
'Croatia',
'Hungary',
'Ireland',
'Italy',
'Lithuania',
'Luxembourg',
'Latvia',
'Malta',
'Netherlands',
'Poland',
'Portugal',
'Romania',
'Sweden',
'Slovenia',
'Slovakia',
];
private static array $EUCountriesCode = [
'AT', // ? Austria
'BE', // ? Belgium
'BG', // ? Bulgaria
'CY', // ? Cyprus
'CZ', // ? Czechia
'DE', // ? Germany
'DK', // ? Denmark
'EE', // ? Estonia
'EL', // ? Greece
'ES', // ? Spain
'FI', // ? Finland
'FR', // ? France
'HR', // ? Croatia
'HU', // ? Hungary
'IE', // ? Ireland
'IT', // ? Italy
'LT', // ? Lithuania
'LU', // ? Luxembourg
'LV', // ? Latvia
'MT', // ? Malta
'NL', // ? Netherlands
'PL', // ? Poland
'PT', // ? Portugal
'RO', // ? Romania
'SE', // ? Sweden
'SI', // ? Slovenia
'SK' // ? Slovakia
];
private static array $EUCountyNameByCode = [
'AT' => 'Austria',
'BE' => 'Belgium',
'BG' => 'Bulgaria',
'CY' => 'Cyprus',
'CZ' => 'Czechia',
'DE' => 'Germany',
'DK' => 'Denmark',
'EE' => 'Estonia',
'EL' => 'Greece',
'ES' => 'Spain',
'FI' => 'Finland',
'FR' => 'France',
'HR' => 'Croatia',
'HU' => 'Hungary',
'IE' => 'Ireland',
'IT' => 'Italy',
'LT' => 'Lithuania',
'LU' => 'Luxembourg',
'LV' => 'Latvia',
'MT' => 'Malta',
'NL' => 'Netherlands',
'PL' => 'Poland',
'PT' => 'Portugal',
'RO' => 'Romania',
'SE' => 'Sweden',
'SI' => 'Slovenia',
'SK' => 'Slovakia'
];
private static array $EUCountyCodeByName = [
'Austria' => 'AT',
'Belgium' => 'BE',
'Bulgaria' => 'BG',
'Cyprus' => 'CY',
'Czechia' => 'CZ',
'Germany' => 'DE',
'Denmark' => 'DK',
'Estonia' => 'EE',
'Greece' => 'EL',
'Spain' => 'ES',
'Finland' => 'FI',
'France' => 'FR',
'Croatia' => 'HR',
'Hungary' => 'HU',
'Ireland' => 'IE',
'Italy' => 'IT',
'Lithuania' => 'LT',
'Luxembourg' => 'LU',
'Latvia' => 'LV',
'Malta' => 'MT',
'Netherlands' => 'NL',
'Poland' => 'PL',
'Portugal' => 'PT',
'Romania' => 'RO',
'Sweden' => 'SE',
'Slovenia' => 'SI',
'Slovakia' => 'SK',
];
public static array $ZipCodeRegex = [
"AT" => "/^\d{4}$/i",
"BE" => "/^\d{4}$/i",
"BG" => "/^\d{4}$/i",
"CY" => "/^\d{4}$/i",
"CZ" => "/^\d{3}\s\d{2}$/i",
"DE" => "/^\d{5}$/i",
"DK" => "/^\d{4}$/i",
"EE" => "/^\d{5}$/i",
"EL" => "/^\d{5}$/i",
"ES" => "/^\d{5}$/i",
"FI" => "/^\d{5}$/i}",
"FR" => "/^\d{2}\s\d{3}$/i",
"HR" => "/^\d{5}$/i",
"HU" => "/^\d{4}$/i",
"IT" => "/^\d{5}$/i",
"LT" => "/^\d{5}$/i",
"LU" => "/^\d{4}$/i",
"LV" => "/^\d{4}$/i",
"MT" => "/^[A-Z]{3}[ ]?\d{2,4}$/i",
"NL" => "/^\d{4}[ ]?[A-Z]{2}$/i",
"PL" => "/^\d{2}-\d{3}$/i",
"PT" => "/^\d{4}([\-]\d{3})?$/i",
"RO" => "/^\d{6}$/i",
"SE" => "/^\d{3}[ ]?\d{2}$/i",
"SK" => "/^\d{3}[ ]?\d{2}$/i",
"SI" => "/^\d{4}$/i",
// ? Not EU Countries
"CH" => "/^\d{4}$/i",
];
/**
* Helper function to get all EU country's codes as an array
* @return string[]
*/
public static function getEUCountriesCode(): array {
return self::$EUCountriesCode;
}
/**
* Helper function to get all EU country's names as an array
* @return string[]
*/
public static function getEUCountriesName(): array {
return self::$EUCountriesName;
}
/**
* Helper function to check if country's code from EU
* @param string $countryCode
* @return bool
*/
public static function isCountryCodeFromEU(string $countryCode): bool {
$countriesCode = self::getEUCountriesCode();
return in_array($countryCode, $countriesCode);
}
/**
* Helper function to check if country's code from EU
* @param string $countryName
* @return bool
*/
public static function isCountryNameFromEU(string $countryName): bool {
$countriesName = self::getEUCountriesName();
return in_array($countryName, $countriesName);
}
/**
* Helper function to get country name by country's code
* @param string $countryCode
* @return string|null
*/
public static function getCountryNameByCode(string $countryCode): ?string {
return self::$EUCountyNameByCode[$countryCode] ?? null;
}
/**
* Helper function to get country code by country's name
* @param string $countryName
* @return string|null
*/
public static function getCountryCodeByName(string $countryName): ?string {
return self::$EUCountyCodeByName[$countryName] ?? null;
}
/**
* Helper function to get country code regex by country's code
* @param string $countryCode
* @return string|null
*/
public static function getCountryCodeRegexByCode(string $countryCode): ?string {
return self::$ZipCodeRegex[$countryCode] ?? null;
}
/**
* Helper function to validate country zip code
* In EU und Switzerland
* @param string $countryCode
* @param string $zipCode
* @return bool
*/
public static function validateZIPCode(string $countryCode, string $zipCode): bool{
$regexCode = self::getCountryCodeRegexByCode($countryCode);
if($regexCode) return preg_match($regexCode, $zipCode);
else return false;
}
}