forked from brick/money
-
Notifications
You must be signed in to change notification settings - Fork 0
/
import-currencies.php
448 lines (412 loc) · 12.1 KB
/
import-currencies.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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
<?php
/**
* This script creates an array of currencies from a trusted source.
* The result of the script is exported in PHP format to be used by the library at runtime.
*
* This script is meant to be run by project maintainers, on a regular basis.
*/
declare(strict_types=1);
use Brick\VarExporter\VarExporter;
require __DIR__ . '/vendor/autoload.php';
/**
* Country names are present in the ISO 4217 currency file, but not country codes.
* This list maps country names to ISO 3166 country codes. It must be up to date or this script will error.
* If country names present in the ISO currency file do not map to an actual country as defined by ISO 3166,
* for example "EUROPEAN UNION", the corresponding value must be NULL.
*/
$countryCodes= [
'AFGHANISTAN' => 'AF',
'ÅLAND ISLANDS' => 'AX',
'ALBANIA' => 'AL',
'ALGERIA' => 'DZ',
'AMERICAN SAMOA' => 'AS',
'ANDORRA' => 'AD',
'ANGOLA' => 'AO',
'ANGUILLA' => 'AI',
'ANTARCTICA' => 'AQ',
'ANTIGUA AND BARBUDA' => 'AG',
'ARGENTINA' => 'AR',
'ARMENIA' => 'AM',
'ARUBA' => 'AW',
'AUSTRALIA' => 'AU',
'AUSTRIA' => 'AT',
'AZERBAIJAN' => 'AZ',
'BAHAMAS (THE)' => 'BS',
'BAHRAIN' => 'BH',
'BANGLADESH' => 'BD',
'BARBADOS' => 'BB',
'BELARUS' => 'BY',
'BELGIUM' => 'BE',
'BELIZE' => 'BZ',
'BENIN' => 'BJ',
'BERMUDA' => 'BM',
'BHUTAN' => 'BT',
'BOLIVIA (PLURINATIONAL STATE OF)' => 'BO',
'BONAIRE, SINT EUSTATIUS AND SABA' => 'BQ',
'BOSNIA AND HERZEGOVINA' => 'BA',
'BOTSWANA' => 'BW',
'BOUVET ISLAND' => 'BV',
'BRAZIL' => 'BR',
'BRITISH INDIAN OCEAN TERRITORY (THE)' => 'IO',
'BRUNEI DARUSSALAM' => 'BN',
'BULGARIA' => 'BG',
'BURKINA FASO' => 'BF',
'BURUNDI' => 'BI',
'CABO VERDE' => 'CV',
'CAMBODIA' => 'KH',
'CAMEROON' => 'CM',
'CANADA' => 'CA',
'CAYMAN ISLANDS (THE)' => 'KY',
'CENTRAL AFRICAN REPUBLIC (THE)' => 'CF',
'CHAD' => 'TD',
'CHILE' => 'CL',
'CHINA' => 'CN',
'CHRISTMAS ISLAND' => 'CX',
'COCOS (KEELING) ISLANDS (THE)' => 'CC',
'COLOMBIA' => 'CO',
'COMOROS (THE)' => 'KM',
'CONGO (THE DEMOCRATIC REPUBLIC OF THE)' => 'CD',
'CONGO (THE)' => 'CG',
'COOK ISLANDS (THE)' => 'CK',
'COSTA RICA' => 'CR',
'CÔTE D\'IVOIRE' => 'CI',
'CROATIA' => 'HR',
'CUBA' => 'CU',
'CURAÇAO' => 'CW',
'CYPRUS' => 'CY',
'CZECHIA' => 'CZ',
'DENMARK' => 'DK',
'DJIBOUTI' => 'DJ',
'DOMINICA' => 'DM',
'DOMINICAN REPUBLIC (THE)' => 'DO',
'ECUADOR' => 'EC',
'EGYPT' => 'EG',
'EL SALVADOR' => 'SV',
'EQUATORIAL GUINEA' => 'GQ',
'ERITREA' => 'ER',
'ESTONIA' => 'EE',
'ESWATINI' => 'SZ',
'ETHIOPIA' => 'ET',
'EUROPEAN UNION' => null,
'FALKLAND ISLANDS (THE) [MALVINAS]' => 'FK',
'FAROE ISLANDS (THE)' => 'FO',
'FIJI' => 'FJ',
'FINLAND' => 'FI',
'FRANCE' => 'FR',
'FRENCH GUIANA' => 'GF',
'FRENCH POLYNESIA' => 'PF',
'FRENCH SOUTHERN TERRITORIES (THE)' => 'TF',
'GABON' => 'GA',
'GAMBIA (THE)' => 'GM',
'GEORGIA' => 'GE',
'GERMANY' => 'DE',
'GHANA' => 'GH',
'GIBRALTAR' => 'GI',
'GREECE' => 'GR',
'GREENLAND' => 'GL',
'GRENADA' => 'GD',
'GUADELOUPE' => 'GP',
'GUAM' => 'GU',
'GUATEMALA' => 'GT',
'GUERNSEY' => 'GG',
'GUINEA' => 'GN',
'GUINEA-BISSAU' => 'GW',
'GUYANA' => 'GY',
'HAITI' => 'HT',
'HEARD ISLAND AND McDONALD ISLANDS' => 'HM',
'HOLY SEE (THE)' => 'VA',
'HONDURAS' => 'HN',
'HONG KONG' => 'HK',
'HUNGARY' => 'HU',
'ICELAND' => 'IS',
'INDIA' => 'IN',
'INDONESIA' => 'ID',
'IRAN (ISLAMIC REPUBLIC OF)' => 'IR',
'IRAQ' => 'IQ',
'IRELAND' => 'IE',
'ISLE OF MAN' => 'IM',
'ISRAEL' => 'IL',
'ITALY' => 'IT',
'JAMAICA' => 'JM',
'JAPAN' => 'JP',
'JERSEY' => 'JE',
'JORDAN' => 'JO',
'KAZAKHSTAN' => 'KZ',
'KENYA' => 'KE',
'KIRIBATI' => 'KI',
'KOREA (THE DEMOCRATIC PEOPLE’S REPUBLIC OF)' => 'KP',
'KOREA (THE REPUBLIC OF)' => 'KR',
'KUWAIT' => 'KW',
'KYRGYZSTAN' => 'KG',
'LAO PEOPLE’S DEMOCRATIC REPUBLIC (THE)' => 'LA',
'LATVIA' => 'LV',
'LEBANON' => 'LB',
'LESOTHO' => 'LS',
'LIBERIA' => 'LR',
'LIBYA' => 'LY',
'LIECHTENSTEIN' => 'LI',
'LITHUANIA' => 'LT',
'LUXEMBOURG' => 'LU',
'MACAO' => 'MO',
'MADAGASCAR' => 'MG',
'MALAWI' => 'MW',
'MALAYSIA' => 'MY',
'MALDIVES' => 'MV',
'MALI' => 'ML',
'MALTA' => 'MT',
'MARSHALL ISLANDS (THE)' => 'MH',
'MARTINIQUE' => 'MQ',
'MAURITANIA' => 'MR',
'MAURITIUS' => 'MU',
'MAYOTTE' => 'YT',
'MEXICO' => 'MX',
'MICRONESIA (FEDERATED STATES OF)' => 'FM',
'MOLDOVA (THE REPUBLIC OF)' => 'MD',
'MONACO' => 'MC',
'MONGOLIA' => 'MN',
'MONTENEGRO' => 'ME',
'MONTSERRAT' => 'MS',
'MOROCCO' => 'MA',
'MOZAMBIQUE' => 'MZ',
'MYANMAR' => 'MM',
'NAMIBIA' => 'NA',
'NAURU' => 'NR',
'NEPAL' => 'NP',
'NETHERLANDS (THE)' => 'NL',
'NEW CALEDONIA' => 'NC',
'NEW ZEALAND' => 'NZ',
'NICARAGUA' => 'NI',
'NIGER (THE)' => 'NE',
'NIGERIA' => 'NG',
'NIUE' => 'NU',
'NORFOLK ISLAND' => 'NF',
'NORTH MACEDONIA' => 'MK',
'NORTHERN MARIANA ISLANDS (THE)' => 'MP',
'NORWAY' => 'NO',
'OMAN' => 'OM',
'PAKISTAN' => 'PK',
'PALAU' => 'PW',
'PALESTINE, STATE OF' => 'PS',
'PANAMA' => 'PA',
'PAPUA NEW GUINEA' => 'PG',
'PARAGUAY' => 'PY',
'PERU' => 'PE',
'PHILIPPINES (THE)' => 'PH',
'PITCAIRN' => 'PN',
'POLAND' => 'PL',
'PORTUGAL' => 'PT',
'PUERTO RICO' => 'PR',
'QATAR' => 'QA',
'RÉUNION' => 'RE',
'ROMANIA' => 'RO',
'RUSSIAN FEDERATION (THE)' => 'RU',
'RWANDA' => 'RW',
'SAINT BARTHÉLEMY' => 'BL',
'SAINT HELENA, ASCENSION AND TRISTAN DA CUNHA' => 'SH',
'SAINT KITTS AND NEVIS' => 'KN',
'SAINT LUCIA' => 'LC',
'SAINT MARTIN (FRENCH PART)' => 'MF',
'SAINT PIERRE AND MIQUELON' => 'PM',
'SAINT VINCENT AND THE GRENADINES' => 'VC',
'SAMOA' => 'WS',
'SAN MARINO' => 'SM',
'SAO TOME AND PRINCIPE' => 'ST',
'SAUDI ARABIA' => 'SA',
'SENEGAL' => 'SN',
'SERBIA' => 'RS',
'SEYCHELLES' => 'SC',
'SIERRA LEONE' => 'SL',
'SINGAPORE' => 'SG',
'SINT MAARTEN (DUTCH PART)' => 'SX',
'SLOVAKIA' => 'SK',
'SLOVENIA' => 'SI',
'SOLOMON ISLANDS' => 'SB',
'SOMALIA' => 'SO',
'SOUTH AFRICA' => 'ZA',
'SOUTH GEORGIA AND THE SOUTH SANDWICH ISLANDS' => 'GS',
'SOUTH SUDAN' => 'SS',
'SPAIN' => 'ES',
'SRI LANKA' => 'LK',
'SUDAN (THE)' => 'SD',
'SURINAME' => 'SR',
'SVALBARD AND JAN MAYEN' => 'SJ',
'SWEDEN' => 'SE',
'SWITZERLAND' => 'CH',
'SYRIAN ARAB REPUBLIC' => 'SY',
'TAIWAN (PROVINCE OF CHINA)' => 'TW',
'TAJIKISTAN' => 'TJ',
'TANZANIA, UNITED REPUBLIC OF' => 'TZ',
'THAILAND' => 'TH',
'TIMOR-LESTE' => 'TL',
'TOGO' => 'TG',
'TOKELAU' => 'TK',
'TONGA' => 'TO',
'TRINIDAD AND TOBAGO' => 'TT',
'TUNISIA' => 'TN',
'TURKEY' => 'TR',
'TURKMENISTAN' => 'TM',
'TURKS AND CAICOS ISLANDS (THE)' => 'TC',
'TUVALU' => 'TV',
'UGANDA' => 'UG',
'UKRAINE' => 'UA',
'UNITED ARAB EMIRATES (THE)' => 'AE',
'UNITED KINGDOM OF GREAT BRITAIN AND NORTHERN IRELAND (THE)' => 'GB',
'UNITED STATES MINOR OUTLYING ISLANDS (THE)' => 'UM',
'UNITED STATES OF AMERICA (THE)' => 'US',
'URUGUAY' => 'UY',
'UZBEKISTAN' => 'UZ',
'VANUATU' => 'VU',
'VENEZUELA (BOLIVARIAN REPUBLIC OF)' => 'VE',
'VIET NAM' => 'VN',
'VIRGIN ISLANDS (BRITISH)' => 'VG',
'VIRGIN ISLANDS (U.S.)' => 'VI',
'WALLIS AND FUTUNA' => 'WF',
'WESTERN SAHARA' => 'EH',
'YEMEN' => 'YE',
'ZAMBIA' => 'ZM',
'ZIMBABWE' => 'ZW',
];
$data = file_get_contents('https://www.currency-iso.org/dam/downloads/lists/list_one.xml');
$document = new DOMDocument();
$document->loadXML($data);
$countries = $document->getElementsByTagName('CcyNtry');
$currencies = [];
$numericToCurrency = [];
$countryToCurrency = [];
$countryNamesFound = [];
foreach ($countries as $country) {
/** @var DOMElement $country */
$countryName = getDomElementString($country, 'CtryNm');
$currencyName = getDomElementString($country, 'CcyNm');
$currencyCode = getDomElementString($country, 'Ccy');
$numericCode = getDomElementString($country, 'CcyNbr');
$minorUnits = getDomElementString($country, 'CcyMnrUnts');
$countryNamesFound[$countryName] = true;
$isFund = $country->getElementsByTagName('CcyNm')->item(0)->getAttribute('IsFund') === 'true';
if ($currencyName === null || $currencyCode === null && $numericCode === null && $minorUnits == null) {
continue;
}
if ($minorUnits === 'N.A.') {
continue;
}
if (! array_key_exists($countryName, $countryCodes)) {
throw new \RuntimeException('No country code found for ' . $countryName);
}
$countryCode = $countryCodes[$countryName];
if ($countryCode !== null) {
if (! $isFund) {
$countryToCurrency[$countryCode][] = $currencyCode;
}
}
$currencyName = checkCurrencyName($currencyName);
$currencyCode = checkCurrencyCode($currencyCode);
$numericCode = checkNumericCode($numericCode);
$minorUnits = checkMinorUnits($minorUnits);
$value = [$currencyCode, $numericCode, $currencyName, $minorUnits];
if (isset($currencies[$currencyCode])) {
if ($currencies[$currencyCode] !== $value) {
throw new \RuntimeException('Inconsistent values found for currency code ' . $currencyCode);
}
} else {
$currencies[$currencyCode] = $value;
$numericToCurrency[$numericCode] = $currencyCode;
}
}
foreach ($countryCodes as $countryName => $countryCode) {
if (! isset($countryNamesFound[$countryName])) {
echo 'Warning: ' . $countryName . ' not found in ISO file.', PHP_EOL;
}
}
ksort($currencies);
ksort($numericToCurrency);
ksort($countryToCurrency);
exportToFile(__DIR__ . '/data/iso-currencies.php', $currencies);
exportToFile(__DIR__ . '/data/numeric-to-currency.php', $numericToCurrency);
exportToFile(__DIR__ . '/data/country-to-currency.php', $countryToCurrency);
printf('Exported %d currencies in %d countries.' . PHP_EOL, count($currencies), count($countryToCurrency));
/**
* @param string $file
* @param array $data
*
* @return void
*/
function exportToFile(string $file, array $data) : void
{
$data = '<?php ' . VarExporter::export($data, VarExporter::ADD_RETURN | VarExporter::INLINE_NUMERIC_SCALAR_ARRAY);
if (file_get_contents($file) === $data) {
printf("%s: no change\n", $file);
} else {
file_put_contents($file, $data);
printf("%s: UPDATED\n", $file);
}
}
/**
* @param DOMElement $element
* @param string $name
*
* @return string|null
*/
function getDomElementString(DOMElement $element, string $name) : ?string
{
foreach ($element->getElementsByTagName($name) as $child) {
/** @var $child DOMElement */
return $child->textContent;
}
return null;
}
/**
* @param string $name
*
* @return string
*
* @throws RuntimeException
*/
function checkCurrencyName(string $name) : string
{
if ($name === '' || ! mb_check_encoding($name, 'UTF-8')) {
throw new \RuntimeException('Invalid currency name: ' . $name);
}
return $name;
}
/**
* @param string $currencyCode
*
* @return string
*
* @throws RuntimeException
*/
function checkCurrencyCode(string $currencyCode) : string
{
if (preg_match('/^[A-Z]{3}$/', $currencyCode) !== 1) {
throw new \RuntimeException('Invalid currency code: ' . $currencyCode);
}
return $currencyCode;
}
/**
* @param string $numericCode
*
* @return int
*
* @throws RuntimeException
*/
function checkNumericCode(string $numericCode) : int
{
if (preg_match('/^[0-9]{3}$/', $numericCode) !== 1) {
throw new \RuntimeException('Invalid numeric code: ' . $numericCode);
}
return (int) ltrim($numericCode, '0');
}
/**
* @param string $minorUnits
*
* @return int
*
* @throws RuntimeException
*/
function checkMinorUnits(string $minorUnits) : int
{
if (preg_match('/^[0-9]{1}$/', $minorUnits) !== 1) {
throw new \RuntimeException('Invalid minor unit: ' . $minorUnits);
}
return (int) $minorUnits;
}