-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate.php
180 lines (164 loc) · 4.53 KB
/
generate.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
<?php
$file = new SplFileObject(__DIR__ . '/country-codes/data/country-codes.csv');
try {
$header = $file->fgetcsv();
$marcColumn = array_search('MARC', $header);
$isoColumn = array_search('ISO3166-1-Alpha-2', $header);
$byMarcCode = [];
while (count($data = $file->fgetcsv()) > 1) {
[$marcColumn => $marc, $isoColumn => $iso] = $data;
foreach (explode(',', $marc) as $marc) {
$byMarcCode[mb_strtoupper($marc)] = mb_strtoupper($iso);
}
}
} finally {
$file = null;
}
$byMarcName = [
// USA States
'ALABAMA' => 'US',
'ALASKA' => 'US',
'AMERICAN SAMOA' => 'US',
'ARIZONA' => 'US',
'ARKANSAS' => 'US',
'CALIFORNIA' => 'US',
'COLORADO' => 'US',
'CONNECTICUT' => 'US',
'DELAWARE' => 'US',
'DISTRICT OF COLUMBIA' => 'US',
'FEDERATED STATES OF MICRONESIA' => 'US',
'FLORIDA' => 'US',
'GEORGIA' => 'US',
'GUAM' => 'US',
'HAWAII' => 'US',
'IDAHO' => 'US',
'ILLINOIS' => 'US',
'INDIANA' => 'US',
'IOWA' => 'US',
'KANSAS' => 'US',
'KENTUCKY' => 'US',
'LOUISIANA' => 'US',
'MAINE' => 'US',
'MARSHALL ISLANDS' => 'US',
'MARYLAND' => 'US',
'MASSACHUSETTS' => 'US',
'MICHIGAN' => 'US',
'MINNESOTA' => 'US',
'MISSISSIPPI' => 'US',
'MISSOURI' => 'US',
'MONTANA' => 'US',
'NEBRASKA' => 'US',
'NEVADA' => 'US',
'NEW HAMPSHIRE' => 'US',
'NEW JERSEY' => 'US',
'NEW MEXICO' => 'US',
'NEW YORK (STATE)' => 'US',
'NORTH CAROLINA' => 'US',
'NORTH DAKOTA' => 'US',
'NORTHERN MARIANA ISLANDS' => 'US',
'OHIO' => 'US',
'OKLAHOMA' => 'US',
'OREGON' => 'US',
'PALAU' => 'US',
'PENNSYLVANIA' => 'US',
'PUERTO RICO' => 'US',
'RHODE ISLAND' => 'US',
'SOUTH CAROLINA' => 'US',
'SOUTH DAKOTA' => 'US',
'TENNESSEE' => 'US',
'TEXAS' => 'US',
'UTAH' => 'US',
'VERMONT' => 'US',
'VIRGIN ISLANDS' => 'US',
'VIRGINIA' => 'US',
'WASHINGTON (STATE)' => 'US',
'WEST VIRGINIA' => 'US',
'WISCONSIN' => 'US',
'WYOMING' => 'US',
// Canadian States
'ALBERTA' => 'CA',
'BRITISH COLUMBIA' => 'CA',
'MANITOBA' => 'CA',
'NEW BRUNSWICK' => 'CA',
'NEWFOUNDLAND AND LABRADOR' => 'CA',
'NORTHWEST TERRITORIES' => 'CA',
'NOVA SCOTIA' => 'CA',
'NUNAVUT' => 'CA',
'ONTARIO' => 'CA',
'PRINCE EDWARD ISLAND' => 'CA',
'QUÉBEC (PROVINCE)' => 'CA',
'SASKATCHEWAN' => 'CA',
'YUKON TERRITORY' => 'CA',
// Others
'SPANISH NORTH AFRICA' => 'ES',
'SPRATLY ISLAND' => 'VN',
// United Kingdom
'ENGLAND' => 'GB',
'NORTHERN IRELAND' => 'GB',
'SCOTLAND' => 'GB',
'WALES' => 'GB',
// Territories UK
'ISLE OF MAN' => 'GB',
'JERSEY' => 'GB',
'GUERNSEY' => 'GB',
// Australian States
'AUSTRALIAN CAPITAL TERRITORY' => 'AU',
'CORAL SEA ISLANDS TERRITORY' => 'AU',
'NEW SOUTH WALES' => 'AU',
'NORTHERN TERRITORY' => 'AU',
'QUEENSLAND' => 'AU',
'SOUTH AUSTRALIA' => 'AU',
'TASMANIA' => 'AU',
'VICTORIA' => 'AU',
'WESTERN AUSTRALIA' => 'AU',
// Disputed territories
// Iraq-Saudi Arabia Neutral Zone
// Kosovo
// Paracel Islands
// Inexistent locations
// Various places
// No place
];
$file = new SplFileObject(__DIR__ . '/marc-countries.csv');
$nameMappings = $codeMappings = [];
try {
$file->fgetcsv();
while ([$code, $name] = $file->fgetcsv()) {
$code = mb_strtoupper($code);
$name = mb_strtoupper($name);
if ($iso = $byMarcCode[$code] ?? $byMarcName[$name] ?? null) {
$codeMappings[$code] = $nameMappings[$name] = $iso;
$isoCodeMappings[$iso] = $code;
$isoNameMappings[$iso] = $name;
}
else {
echo 'Ignored: ', $code, ' (', $name ,')', "\n";
}
}
} finally {
$file = null;
}
$output = [
'MarcNameToIso' => $nameMappings,
'MarcCodeToIso' => $codeMappings,
'IsoToMarcCode' => $isoCodeMappings,
'IsoToMarcName' => $isoNameMappings
];
$template = '<?php
declare(strict_types=1);
namespace JonasRaoni\MarcToIso;
class %s
{
public const MAPPINGS = [%s
];
public static function get(?string $input): ?string
{
return self::MAPPINGS[mb_strtoupper($input ?? '')] ?? null;
}
}
';
$outputFolder = __DIR__ . '/../src/';
is_dir($outputFolder) || mkdir($outputFolder);
foreach ($output as $file => $source) {
file_put_contents($outputFolder . $file . '.php', sprintf($template, $file, str_replace(['array (', "\n"], ['', "\n "], trim(var_export($source, true), ",)\n"))));
}