1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Ecodev\Felix\Api\Scalar; |
6
|
|
|
|
7
|
|
|
use GraphQL\Language\AST\Node; |
8
|
|
|
|
9
|
|
|
class CountryCode2Type extends AbstractStringBasedType |
10
|
|
|
{ |
11
|
|
|
public ?string $description = 'ISO 3166-1 alpha-2 two letter country code'; |
12
|
|
|
|
13
|
|
|
public const countryCodes = [ |
14
|
|
|
'AD', // Andorra |
15
|
|
|
'AE', // United Arab Emirates |
16
|
|
|
'AF', // Afghanistan |
17
|
|
|
'AG', // Antigua and Barbuda |
18
|
|
|
'AI', // Anguilla |
19
|
|
|
'AL', // Albania |
20
|
|
|
'AM', // Armenia |
21
|
|
|
'AO', // Angola |
22
|
|
|
'AQ', // Antarctica |
23
|
|
|
'AR', // Argentina |
24
|
|
|
'AS', // American Samoa |
25
|
|
|
'AT', // Austria |
26
|
|
|
'AU', // Australia |
27
|
|
|
'AW', // Aruba |
28
|
|
|
'AX', // Åland Islands |
29
|
|
|
'AZ', // Azerbaijan |
30
|
|
|
'BA', // Bosnia and Herzegovina |
31
|
|
|
'BB', // Barbados |
32
|
|
|
'BD', // Bangladesh |
33
|
|
|
'BE', // Belgium |
34
|
|
|
'BF', // Burkina Faso |
35
|
|
|
'BG', // Bulgaria |
36
|
|
|
'BH', // Bahrain |
37
|
|
|
'BI', // Burundi |
38
|
|
|
'BJ', // Benin |
39
|
|
|
'BL', // Saint Barthélemy |
40
|
|
|
'BM', // Bermuda |
41
|
|
|
'BN', // Brunei Darussalam |
42
|
|
|
'BO', // Bolivia (Plurinational State of) |
43
|
|
|
'BQ', // Bonaire, Sint Eustatius and Saba |
44
|
|
|
'BR', // Brazil |
45
|
|
|
'BS', // Bahamas |
46
|
|
|
'BT', // Bhutan |
47
|
|
|
'BV', // Bouvet Island |
48
|
|
|
'BW', // Botswana |
49
|
|
|
'BY', // Belarus |
50
|
|
|
'BZ', // Belize |
51
|
|
|
'CA', // Canada |
52
|
|
|
'CC', // Cocos (Keeling) Islands |
53
|
|
|
'CD', // Congo (Democratic Republic of the) |
54
|
|
|
'CF', // Central African Republic |
55
|
|
|
'CG', // Congo |
56
|
|
|
'CH', // Switzerland |
57
|
|
|
'CI', // Côte d'Ivoire |
58
|
|
|
'CK', // Cook Islands |
59
|
|
|
'CL', // Chile |
60
|
|
|
'CM', // Cameroon |
61
|
|
|
'CN', // China |
62
|
|
|
'CO', // Colombia |
63
|
|
|
'CR', // Costa Rica |
64
|
|
|
'CU', // Cuba |
65
|
|
|
'CV', // Cabo Verde |
66
|
|
|
'CW', // Curaçao |
67
|
|
|
'CX', // Christmas Island |
68
|
|
|
'CY', // Cyprus |
69
|
|
|
'CZ', // Czech Republic |
70
|
|
|
'DE', // Germany |
71
|
|
|
'DJ', // Djibouti |
72
|
|
|
'DK', // Denmark |
73
|
|
|
'DM', // Dominica |
74
|
|
|
'DO', // Dominican Republic |
75
|
|
|
'DZ', // Algeria |
76
|
|
|
'EC', // Ecuador |
77
|
|
|
'EE', // Estonia |
78
|
|
|
'EG', // Egypt |
79
|
|
|
'EH', // Western Sahara |
80
|
|
|
'ER', // Eritrea |
81
|
|
|
'ES', // Spain |
82
|
|
|
'ET', // Ethiopia |
83
|
|
|
'FI', // Finland |
84
|
|
|
'FJ', // Fiji |
85
|
|
|
'FK', // Falkland Islands (Malvinas) |
86
|
|
|
'FM', // Micronesia (Federated States of) |
87
|
|
|
'FO', // Faroe Islands |
88
|
|
|
'FR', // France |
89
|
|
|
'GA', // Gabon |
90
|
|
|
'GB', // United Kingdom of Great Britain and Northern Ireland |
91
|
|
|
'GD', // Grenada |
92
|
|
|
'GE', // Georgia |
93
|
|
|
'GF', // French Guiana |
94
|
|
|
'GG', // Guernsey |
95
|
|
|
'GH', // Ghana |
96
|
|
|
'GI', // Gibraltar |
97
|
|
|
'GL', // Greenland |
98
|
|
|
'GM', // Gambia |
99
|
|
|
'GN', // Guinea |
100
|
|
|
'GP', // Guadeloupe |
101
|
|
|
'GQ', // Equatorial Guinea |
102
|
|
|
'GR', // Greece |
103
|
|
|
'GS', // South Georgia and the South Sandwich Islands |
104
|
|
|
'GT', // Guatemala |
105
|
|
|
'GU', // Guam |
106
|
|
|
'GW', // Guinea-Bissau |
107
|
|
|
'GY', // Guyana |
108
|
|
|
'HK', // Hong Kong |
109
|
|
|
'HM', // Heard Island and McDonald Islands |
110
|
|
|
'HN', // Honduras |
111
|
|
|
'HR', // Croatia |
112
|
|
|
'HT', // Haiti |
113
|
|
|
'HU', // Hungary |
114
|
|
|
'ID', // Indonesia |
115
|
|
|
'IE', // Ireland |
116
|
|
|
'IL', // Israel |
117
|
|
|
'IM', // Isle of Man |
118
|
|
|
'IN', // India |
119
|
|
|
'IO', // British Indian Ocean Territory |
120
|
|
|
'IQ', // Iraq |
121
|
|
|
'IR', // Iran (Islamic Republic of) |
122
|
|
|
'IS', // Iceland |
123
|
|
|
'IT', // Italy |
124
|
|
|
'JE', // Jersey |
125
|
|
|
'JM', // Jamaica |
126
|
|
|
'JO', // Jordan |
127
|
|
|
'JP', // Japan |
128
|
|
|
'KE', // Kenya |
129
|
|
|
'KG', // Kyrgyzstan |
130
|
|
|
'KH', // Cambodia |
131
|
|
|
'KI', // Kiribati |
132
|
|
|
'KM', // Comoros |
133
|
|
|
'KN', // Saint Kitts and Nevis |
134
|
|
|
'KP', // Korea (Democratic People's Republic of) |
135
|
|
|
'KR', // Korea (Republic of) |
136
|
|
|
'KW', // Kuwait |
137
|
|
|
'KY', // Cayman Islands |
138
|
|
|
'KZ', // Kazakhstan |
139
|
|
|
'LA', // Lao People's Democratic Republic |
140
|
|
|
'LB', // Lebanon |
141
|
|
|
'LC', // Saint Lucia |
142
|
|
|
'LI', // Liechtenstein |
143
|
|
|
'LK', // Sri Lanka |
144
|
|
|
'LR', // Liberia |
145
|
|
|
'LS', // Lesotho |
146
|
|
|
'LT', // Lithuania |
147
|
|
|
'LU', // Luxembourg |
148
|
|
|
'LV', // Latvia |
149
|
|
|
'LY', // Libya |
150
|
|
|
'MA', // Morocco |
151
|
|
|
'MC', // Monaco |
152
|
|
|
'MD', // Moldova (Republic of) |
153
|
|
|
'ME', // Montenegro |
154
|
|
|
'MF', // Saint Martin (French part) |
155
|
|
|
'MG', // Madagascar |
156
|
|
|
'MH', // Marshall Islands |
157
|
|
|
'MK', // North Macedonia |
158
|
|
|
'ML', // Mali |
159
|
|
|
'MM', // Myanmar |
160
|
|
|
'MN', // Mongolia |
161
|
|
|
'MO', // Macao |
162
|
|
|
'MP', // Northern Mariana Islands |
163
|
|
|
'MQ', // Martinique |
164
|
|
|
'MR', // Mauritania |
165
|
|
|
'MS', // Montserrat |
166
|
|
|
'MT', // Malta |
167
|
|
|
'MU', // Mauritius |
168
|
|
|
'MV', // Maldives |
169
|
|
|
'MW', // Malawi |
170
|
|
|
'MX', // Mexico |
171
|
|
|
'MY', // Malaysia |
172
|
|
|
'MZ', // Mozambique |
173
|
|
|
'NA', // Namibia |
174
|
|
|
'NC', // New Caledonia |
175
|
|
|
'NE', // Niger |
176
|
|
|
'NF', // Norfolk Island |
177
|
|
|
'NG', // Nigeria |
178
|
|
|
'NI', // Nicaragua |
179
|
|
|
'NL', // Netherlands |
180
|
|
|
'NO', // Norway |
181
|
|
|
'NP', // Nepal |
182
|
|
|
'NR', // Nauru |
183
|
|
|
'NU', // Niue |
184
|
|
|
'NZ', // New Zealand |
185
|
|
|
'OM', // Oman |
186
|
|
|
'PA', // Panama |
187
|
|
|
'PE', // Peru |
188
|
|
|
'PF', // French Polynesia |
189
|
|
|
'PG', // Papua New Guinea |
190
|
|
|
'PH', // Philippines |
191
|
|
|
'PK', // Pakistan |
192
|
|
|
'PL', // Poland |
193
|
|
|
'PM', // Saint Pierre and Miquelon |
194
|
|
|
'PN', // Pitcairn |
195
|
|
|
]; |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* Parse lowercase value and convert to standard uppercase. |
199
|
|
|
*/ |
200
|
6 |
|
public function parseValue(mixed $value): ?string |
201
|
|
|
{ |
202
|
6 |
|
$parsed = parent::parseValue($value); |
203
|
2 |
|
if (is_string($parsed)) { |
204
|
2 |
|
$parsed = mb_strtoupper($parsed); |
205
|
|
|
} |
206
|
|
|
|
207
|
2 |
|
return $parsed; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* Parse literal lowercase and convert to standard uppercase. |
212
|
|
|
*/ |
213
|
7 |
|
public function parseLiteral(Node $valueNode, ?array $variables = null): ?string |
214
|
|
|
{ |
215
|
7 |
|
$parsed = parent::parseLiteral($valueNode, $variables); |
216
|
2 |
|
if (is_string($parsed)) { |
|
|
|
|
217
|
2 |
|
$parsed = mb_strtoupper($parsed); |
218
|
|
|
} |
219
|
|
|
|
220
|
2 |
|
return $parsed; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* Serializes an internal value and convert to uppercase. |
225
|
|
|
*/ |
226
|
6 |
|
public function serialize(mixed $value): mixed |
227
|
|
|
{ |
228
|
|
|
// Assuming internal representation is always correct: |
229
|
6 |
|
if (is_string($value)) { |
230
|
6 |
|
$value = mb_strtoupper($value); |
231
|
|
|
} |
232
|
|
|
|
233
|
6 |
|
return $value; |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* Validate a country code. |
238
|
|
|
*/ |
239
|
12 |
|
protected function isValid(?string $value): bool |
240
|
|
|
{ |
241
|
12 |
|
return is_string($value) && mb_strlen($value) === 2 && in_array(mb_strtoupper($value), self::countryCodes, true); |
242
|
|
|
} |
243
|
|
|
} |
244
|
|
|
|