1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of Respect/Validation. |
5
|
|
|
* |
6
|
|
|
* (c) Alexandre Gomes Gaigalas <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the "LICENSE.md" |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
declare(strict_types=1); |
13
|
|
|
|
14
|
|
|
namespace Respect\Validation\Rules; |
15
|
|
|
|
16
|
|
|
use Respect\Validation\Exceptions\ComponentException; |
17
|
|
|
use function array_column; |
18
|
|
|
use function array_keys; |
19
|
|
|
use function implode; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Validates whether the input is a country code in ISO 3166-1 standard. |
23
|
|
|
* |
24
|
|
|
* This rule supports the three sets of country codes (alpha-2, alpha-3, and numeric). |
25
|
|
|
* |
26
|
|
|
* @author Alexandre Gomes Gaigalas <[email protected]> |
27
|
|
|
* @author Felipe Martins <[email protected]> |
28
|
|
|
* @author Henrique Moody <[email protected]> |
29
|
|
|
* @author William Espindola <[email protected]> |
30
|
|
|
*/ |
31
|
|
|
final class CountryCode extends AbstractSearcher |
32
|
|
|
{ |
33
|
|
|
/** |
34
|
|
|
* The ISO representation of a country code. |
35
|
|
|
* |
36
|
|
|
* @var string |
37
|
|
|
*/ |
38
|
|
|
public const ALPHA2 = 'alpha-2'; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* The ISO3 representation of a country code. |
42
|
|
|
* |
43
|
|
|
* @var string |
44
|
|
|
*/ |
45
|
|
|
public const ALPHA3 = 'alpha-3'; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* The ISO-number representation of a country code. |
49
|
|
|
* |
50
|
|
|
* @var string |
51
|
|
|
*/ |
52
|
|
|
public const NUMERIC = 'numeric'; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Position of the indexes of each set in the list of country codes. |
56
|
|
|
* |
57
|
|
|
* @var array |
58
|
|
|
*/ |
59
|
|
|
private const SET_INDEXES = [ |
60
|
|
|
self::ALPHA2 => 0, |
61
|
|
|
self::ALPHA3 => 1, |
62
|
|
|
self::NUMERIC => 2, |
63
|
|
|
]; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @see http://download.geonames.org/export/dump/countryInfo.txt |
67
|
|
|
* |
68
|
|
|
* @var array |
69
|
|
|
*/ |
70
|
|
|
private const COUNTRY_CODES = [ |
71
|
|
|
['AD', 'AND', '020'], // Andorra |
72
|
|
|
['AE', 'ARE', '784'], // United Arab Emirates |
73
|
|
|
['AF', 'AFG', '004'], // Afghanistan |
74
|
|
|
['AG', 'ATG', '028'], // Antigua and Barbuda |
75
|
|
|
['AI', 'AIA', '660'], // Anguilla |
76
|
|
|
['AL', 'ALB', '008'], // Albania |
77
|
|
|
['AM', 'ARM', '051'], // Armenia |
78
|
|
|
['AN', 'ANT', '530'], // Netherlands Antilles |
79
|
|
|
['AO', 'AGO', '024'], // Angola |
80
|
|
|
['AQ', 'ATA', '010'], // Antarctica |
81
|
|
|
['AR', 'ARG', '032'], // Argentina |
82
|
|
|
['AS', 'ASM', '016'], // American Samoa |
83
|
|
|
['AT', 'AUT', '040'], // Austria |
84
|
|
|
['AU', 'AUS', '036'], // Australia |
85
|
|
|
['AW', 'ABW', '533'], // Aruba |
86
|
|
|
['AX', 'ALA', '248'], // Aland Islands |
87
|
|
|
['AZ', 'AZE', '031'], // Azerbaijan |
88
|
|
|
['BA', 'BIH', '070'], // Bosnia and Herzegovina |
89
|
|
|
['BB', 'BRB', '052'], // Barbados |
90
|
|
|
['BD', 'BGD', '050'], // Bangladesh |
91
|
|
|
['BE', 'BEL', '056'], // Belgium |
92
|
|
|
['BF', 'BFA', '854'], // Burkina Faso |
93
|
|
|
['BG', 'BGR', '100'], // Bulgaria |
94
|
|
|
['BH', 'BHR', '048'], // Bahrain |
95
|
|
|
['BI', 'BDI', '108'], // Burundi |
96
|
|
|
['BJ', 'BEN', '204'], // Benin |
97
|
|
|
['BL', 'BLM', '652'], // Saint Barthelemy |
98
|
|
|
['BM', 'BMU', '060'], // Bermuda |
99
|
|
|
['BN', 'BRN', '096'], // Brunei |
100
|
|
|
['BO', 'BOL', '068'], // Bolivia |
101
|
|
|
['BQ', 'BES', '535'], // Bonaire, Saint Eustatius and Saba |
102
|
|
|
['BR', 'BRA', '076'], // Brazil |
103
|
|
|
['BS', 'BHS', '044'], // Bahamas |
104
|
|
|
['BT', 'BTN', '064'], // Bhutan |
105
|
|
|
['BV', 'BVT', '074'], // Bouvet Island |
106
|
|
|
['BW', 'BWA', '072'], // Botswana |
107
|
|
|
['BY', 'BLR', '112'], // Belarus |
108
|
|
|
['BZ', 'BLZ', '084'], // Belize |
109
|
|
|
['CA', 'CAN', '124'], // Canada |
110
|
|
|
['CC', 'CCK', '166'], // Cocos Islands |
111
|
|
|
['CD', 'COD', '180'], // Democratic Republic of the Congo |
112
|
|
|
['CF', 'CAF', '140'], // Central African Republic |
113
|
|
|
['CG', 'COG', '178'], // Republic of the Congo |
114
|
|
|
['CH', 'CHE', '756'], // Switzerland |
115
|
|
|
['CI', 'CIV', '384'], // Ivory Coast |
116
|
|
|
['CK', 'COK', '184'], // Cook Islands |
117
|
|
|
['CL', 'CHL', '152'], // Chile |
118
|
|
|
['CM', 'CMR', '120'], // Cameroon |
119
|
|
|
['CN', 'CHN', '156'], // China |
120
|
|
|
['CO', 'COL', '170'], // Colombia |
121
|
|
|
['CR', 'CRI', '188'], // Costa Rica |
122
|
|
|
['CS', 'SCG', '891'], // Serbia and Montenegro |
123
|
|
|
['CU', 'CUB', '192'], // Cuba |
124
|
|
|
['CV', 'CPV', '132'], // Cape Verde |
125
|
|
|
['CW', 'CUW', '531'], // Curacao |
126
|
|
|
['CX', 'CXR', '162'], // Christmas Island |
127
|
|
|
['CY', 'CYP', '196'], // Cyprus |
128
|
|
|
['CZ', 'CZE', '203'], // Czech Republic |
129
|
|
|
['DE', 'DEU', '276'], // Germany |
130
|
|
|
['DJ', 'DJI', '262'], // Djibouti |
131
|
|
|
['DK', 'DNK', '208'], // Denmark |
132
|
|
|
['DM', 'DMA', '212'], // Dominica |
133
|
|
|
['DO', 'DOM', '214'], // Dominican Republic |
134
|
|
|
['DZ', 'DZA', '012'], // Algeria |
135
|
|
|
['EC', 'ECU', '218'], // Ecuador |
136
|
|
|
['EE', 'EST', '233'], // Estonia |
137
|
|
|
['EG', 'EGY', '818'], // Egypt |
138
|
|
|
['EH', 'ESH', '732'], // Western Sahara |
139
|
|
|
['ER', 'ERI', '232'], // Eritrea |
140
|
|
|
['ES', 'ESP', '724'], // Spain |
141
|
|
|
['ET', 'ETH', '231'], // Ethiopia |
142
|
|
|
['FI', 'FIN', '246'], // Finland |
143
|
|
|
['FJ', 'FJI', '242'], // Fiji |
144
|
|
|
['FK', 'FLK', '238'], // Falkland Islands |
145
|
|
|
['FM', 'FSM', '583'], // Micronesia |
146
|
|
|
['FO', 'FRO', '234'], // Faroe Islands |
147
|
|
|
['FR', 'FRA', '250'], // France |
148
|
|
|
['GA', 'GAB', '266'], // Gabon |
149
|
|
|
['GB', 'GBR', '826'], // United Kingdom |
150
|
|
|
['GD', 'GRD', '308'], // Grenada |
151
|
|
|
['GE', 'GEO', '268'], // Georgia |
152
|
|
|
['GF', 'GUF', '254'], // French Guiana |
153
|
|
|
['GG', 'GGY', '831'], // Guernsey |
154
|
|
|
['GH', 'GHA', '288'], // Ghana |
155
|
|
|
['GI', 'GIB', '292'], // Gibraltar |
156
|
|
|
['GL', 'GRL', '304'], // Greenland |
157
|
|
|
['GM', 'GMB', '270'], // Gambia |
158
|
|
|
['GN', 'GIN', '324'], // Guinea |
159
|
|
|
['GP', 'GLP', '312'], // Guadeloupe |
160
|
|
|
['GQ', 'GNQ', '226'], // Equatorial Guinea |
161
|
|
|
['GR', 'GRC', '300'], // Greece |
162
|
|
|
['GS', 'SGS', '239'], // South Georgia and the South Sandwich Islands |
163
|
|
|
['GT', 'GTM', '320'], // Guatemala |
164
|
|
|
['GU', 'GUM', '316'], // Guam |
165
|
|
|
['GW', 'GNB', '624'], // Guinea-Bissau |
166
|
|
|
['GY', 'GUY', '328'], // Guyana |
167
|
|
|
['HK', 'HKG', '344'], // Hong Kong |
168
|
|
|
['HM', 'HMD', '334'], // Heard Island and McDonald Islands |
169
|
|
|
['HN', 'HND', '340'], // Honduras |
170
|
|
|
['HR', 'HRV', '191'], // Croatia |
171
|
|
|
['HT', 'HTI', '332'], // Haiti |
172
|
|
|
['HU', 'HUN', '348'], // Hungary |
173
|
|
|
['ID', 'IDN', '360'], // Indonesia |
174
|
|
|
['IE', 'IRL', '372'], // Ireland |
175
|
|
|
['IL', 'ISR', '376'], // Israel |
176
|
|
|
['IM', 'IMN', '833'], // Isle of Man |
177
|
|
|
['IN', 'IND', '356'], // India |
178
|
|
|
['IO', 'IOT', '086'], // British Indian Ocean Territory |
179
|
|
|
['IQ', 'IRQ', '368'], // Iraq |
180
|
|
|
['IR', 'IRN', '364'], // Iran |
181
|
|
|
['IS', 'ISL', '352'], // Iceland |
182
|
|
|
['IT', 'ITA', '380'], // Italy |
183
|
|
|
['JE', 'JEY', '832'], // Jersey |
184
|
|
|
['JM', 'JAM', '388'], // Jamaica |
185
|
|
|
['JO', 'JOR', '400'], // Jordan |
186
|
|
|
['JP', 'JPN', '392'], // Japan |
187
|
|
|
['KE', 'KEN', '404'], // Kenya |
188
|
|
|
['KG', 'KGZ', '417'], // Kyrgyzstan |
189
|
|
|
['KH', 'KHM', '116'], // Cambodia |
190
|
|
|
['KI', 'KIR', '296'], // Kiribati |
191
|
|
|
['KM', 'COM', '174'], // Comoros |
192
|
|
|
['KN', 'KNA', '659'], // Saint Kitts and Nevis |
193
|
|
|
['KP', 'PRK', '408'], // North Korea |
194
|
|
|
['KR', 'KOR', '410'], // South Korea |
195
|
|
|
['KW', 'KWT', '414'], // Kuwait |
196
|
|
|
['KY', 'CYM', '136'], // Cayman Islands |
197
|
|
|
['KZ', 'KAZ', '398'], // Kazakhstan |
198
|
|
|
['LA', 'LAO', '418'], // Laos |
199
|
|
|
['LB', 'LBN', '422'], // Lebanon |
200
|
|
|
['LC', 'LCA', '662'], // Saint Lucia |
201
|
|
|
['LI', 'LIE', '438'], // Liechtenstein |
202
|
|
|
['LK', 'LKA', '144'], // Sri Lanka |
203
|
|
|
['LR', 'LBR', '430'], // Liberia |
204
|
|
|
['LS', 'LSO', '426'], // Lesotho |
205
|
|
|
['LT', 'LTU', '440'], // Lithuania |
206
|
|
|
['LU', 'LUX', '442'], // Luxembourg |
207
|
|
|
['LV', 'LVA', '428'], // Latvia |
208
|
|
|
['LY', 'LBY', '434'], // Libya |
209
|
|
|
['MA', 'MAR', '504'], // Morocco |
210
|
|
|
['MC', 'MCO', '492'], // Monaco |
211
|
|
|
['MD', 'MDA', '498'], // Moldova |
212
|
|
|
['ME', 'MNE', '499'], // Montenegro |
213
|
|
|
['MF', 'MAF', '663'], // Saint Martin |
214
|
|
|
['MG', 'MDG', '450'], // Madagascar |
215
|
|
|
['MH', 'MHL', '584'], // Marshall Islands |
216
|
|
|
['MK', 'MKD', '807'], // Macedonia |
217
|
|
|
['ML', 'MLI', '466'], // Mali |
218
|
|
|
['MM', 'MMR', '104'], // Myanmar |
219
|
|
|
['MN', 'MNG', '496'], // Mongolia |
220
|
|
|
['MO', 'MAC', '446'], // Macao |
221
|
|
|
['MP', 'MNP', '580'], // Northern Mariana Islands |
222
|
|
|
['MQ', 'MTQ', '474'], // Martinique |
223
|
|
|
['MR', 'MRT', '478'], // Mauritania |
224
|
|
|
['MS', 'MSR', '500'], // Montserrat |
225
|
|
|
['MT', 'MLT', '470'], // Malta |
226
|
|
|
['MU', 'MUS', '480'], // Mauritius |
227
|
|
|
['MV', 'MDV', '462'], // Maldives |
228
|
|
|
['MW', 'MWI', '454'], // Malawi |
229
|
|
|
['MX', 'MEX', '484'], // Mexico |
230
|
|
|
['MY', 'MYS', '458'], // Malaysia |
231
|
|
|
['MZ', 'MOZ', '508'], // Mozambique |
232
|
|
|
['NA', 'NAM', '516'], // Namibia |
233
|
|
|
['NC', 'NCL', '540'], // New Caledonia |
234
|
|
|
['NE', 'NER', '562'], // Niger |
235
|
|
|
['NF', 'NFK', '574'], // Norfolk Island |
236
|
|
|
['NG', 'NGA', '566'], // Nigeria |
237
|
|
|
['NI', 'NIC', '558'], // Nicaragua |
238
|
|
|
['NL', 'NLD', '528'], // Netherlands |
239
|
|
|
['NO', 'NOR', '578'], // Norway |
240
|
|
|
['NP', 'NPL', '524'], // Nepal |
241
|
|
|
['NR', 'NRU', '520'], // Nauru |
242
|
|
|
['NU', 'NIU', '570'], // Niue |
243
|
|
|
['NZ', 'NZL', '554'], // New Zealand |
244
|
|
|
['OM', 'OMN', '512'], // Oman |
245
|
|
|
['PA', 'PAN', '591'], // Panama |
246
|
|
|
['PE', 'PER', '604'], // Peru |
247
|
|
|
['PF', 'PYF', '258'], // French Polynesia |
248
|
|
|
['PG', 'PNG', '598'], // Papua New Guinea |
249
|
|
|
['PH', 'PHL', '608'], // Philippines |
250
|
|
|
['PK', 'PAK', '586'], // Pakistan |
251
|
|
|
['PL', 'POL', '616'], // Poland |
252
|
|
|
['PM', 'SPM', '666'], // Saint Pierre and Miquelon |
253
|
|
|
['PN', 'PCN', '612'], // Pitcairn |
254
|
|
|
['PR', 'PRI', '630'], // Puerto Rico |
255
|
|
|
['PS', 'PSE', '275'], // Palestinian Territory |
256
|
|
|
['PT', 'PRT', '620'], // Portugal |
257
|
|
|
['PW', 'PLW', '585'], // Palau |
258
|
|
|
['PY', 'PRY', '600'], // Paraguay |
259
|
|
|
['QA', 'QAT', '634'], // Qatar |
260
|
|
|
['RE', 'REU', '638'], // Reunion |
261
|
|
|
['RO', 'ROU', '642'], // Romania |
262
|
|
|
['RS', 'SRB', '688'], // Serbia |
263
|
|
|
['RU', 'RUS', '643'], // Russia |
264
|
|
|
['RW', 'RWA', '646'], // Rwanda |
265
|
|
|
['SA', 'SAU', '682'], // Saudi Arabia |
266
|
|
|
['SB', 'SLB', '090'], // Solomon Islands |
267
|
|
|
['SC', 'SYC', '690'], // Seychelles |
268
|
|
|
['SD', 'SDN', '729'], // Sudan |
269
|
|
|
['SE', 'SWE', '752'], // Sweden |
270
|
|
|
['SG', 'SGP', '702'], // Singapore |
271
|
|
|
['SH', 'SHN', '654'], // Saint Helena |
272
|
|
|
['SI', 'SVN', '705'], // Slovenia |
273
|
|
|
['SJ', 'SJM', '744'], // Svalbard and Jan Mayen |
274
|
|
|
['SK', 'SVK', '703'], // Slovakia |
275
|
|
|
['SL', 'SLE', '694'], // Sierra Leone |
276
|
|
|
['SM', 'SMR', '674'], // San Marino |
277
|
|
|
['SN', 'SEN', '686'], // Senegal |
278
|
|
|
['SO', 'SOM', '706'], // Somalia |
279
|
|
|
['SR', 'SUR', '740'], // Suriname |
280
|
|
|
['SS', 'SSD', '728'], // South Sudan |
281
|
|
|
['ST', 'STP', '678'], // Sao Tome and Principe |
282
|
|
|
['SV', 'SLV', '222'], // El Salvador |
283
|
|
|
['SX', 'SXM', '534'], // Sint Maarten |
284
|
|
|
['SY', 'SYR', '760'], // Syria |
285
|
|
|
['SZ', 'SWZ', '748'], // Swaziland |
286
|
|
|
['TC', 'TCA', '796'], // Turks and Caicos Islands |
287
|
|
|
['TD', 'TCD', '148'], // Chad |
288
|
|
|
['TF', 'ATF', '260'], // French Southern Territories |
289
|
|
|
['TG', 'TGO', '768'], // Togo |
290
|
|
|
['TH', 'THA', '764'], // Thailand |
291
|
|
|
['TJ', 'TJK', '762'], // Tajikistan |
292
|
|
|
['TK', 'TKL', '772'], // Tokelau |
293
|
|
|
['TL', 'TLS', '626'], // East Timor |
294
|
|
|
['TM', 'TKM', '795'], // Turkmenistan |
295
|
|
|
['TN', 'TUN', '788'], // Tunisia |
296
|
|
|
['TO', 'TON', '776'], // Tonga |
297
|
|
|
['TR', 'TUR', '792'], // Turkey |
298
|
|
|
['TT', 'TTO', '780'], // Trinidad and Tobago |
299
|
|
|
['TV', 'TUV', '798'], // Tuvalu |
300
|
|
|
['TW', 'TWN', '158'], // Taiwan |
301
|
|
|
['TZ', 'TZA', '834'], // Tanzania |
302
|
|
|
['UA', 'UKR', '804'], // Ukraine |
303
|
|
|
['UG', 'UGA', '800'], // Uganda |
304
|
|
|
['UM', 'UMI', '581'], // United States Minor Outlying Islands |
305
|
|
|
['US', 'USA', '840'], // United States |
306
|
|
|
['UY', 'URY', '858'], // Uruguay |
307
|
|
|
['UZ', 'UZB', '860'], // Uzbekistan |
308
|
|
|
['VA', 'VAT', '336'], // Vatican |
309
|
|
|
['VC', 'VCT', '670'], // Saint Vincent and the Grenadines |
310
|
|
|
['VE', 'VEN', '862'], // Venezuela |
311
|
|
|
['VG', 'VGB', '092'], // British Virgin Islands |
312
|
|
|
['VI', 'VIR', '850'], // U.S. Virgin Islands |
313
|
|
|
['VN', 'VNM', '704'], // Vietnam |
314
|
|
|
['VU', 'VUT', '548'], // Vanuatu |
315
|
|
|
['WF', 'WLF', '876'], // Wallis and Futuna |
316
|
|
|
['WS', 'WSM', '882'], // Samoa |
317
|
|
|
['XK', 'XKX', '0'], // Kosovo |
318
|
|
|
['YE', 'YEM', '887'], // Yemen |
319
|
|
|
['YT', 'MYT', '175'], // Mayotte |
320
|
|
|
['ZA', 'ZAF', '710'], // South Africa |
321
|
|
|
['ZM', 'ZMB', '894'], // Zambia |
322
|
|
|
['ZW', 'ZWE', '716'], // Zimbabwe |
323
|
|
|
]; |
324
|
|
|
|
325
|
|
|
/** |
326
|
|
|
* @var string |
327
|
|
|
*/ |
328
|
|
|
private $set; |
329
|
|
|
|
330
|
|
|
/** |
331
|
|
|
* Initializes the rule. |
332
|
|
|
* |
333
|
|
|
* @param string $set |
334
|
|
|
* |
335
|
|
|
* @throws ComponentException If $set is not a valid set |
336
|
|
|
*/ |
337
|
2 |
|
public function __construct(string $set = self::ALPHA2) |
338
|
|
|
{ |
339
|
2 |
|
if (!isset(self::SET_INDEXES[$set])) { |
340
|
1 |
|
throw new ComponentException( |
341
|
1 |
|
sprintf( |
342
|
1 |
|
'"%s" is not a valid set for ISO 3166-1 (Available: %s)', |
343
|
1 |
|
$set, |
344
|
1 |
|
implode(', ', array_keys(self::SET_INDEXES)) |
345
|
|
|
) |
346
|
|
|
); |
347
|
|
|
} |
348
|
|
|
|
349
|
1 |
|
$this->set = $set; |
350
|
1 |
|
} |
351
|
|
|
|
352
|
|
|
/** |
353
|
|
|
* {@inheritdoc} |
354
|
|
|
*/ |
355
|
13 |
|
protected function getDataSource(): array |
356
|
|
|
{ |
357
|
13 |
|
return array_column(self::COUNTRY_CODES, self::SET_INDEXES[$this->set]); |
358
|
|
|
} |
359
|
|
|
} |
360
|
|
|
|