<?php
// Generate an ISO-3166-1 alpha-2 => English name map using intl, so no list is
// hand-maintained. Sweeps AA..ZZ and keeps codes intl actually recognises.
$map = [];
for ($a = 65; $a <= 90; $a++) {
    for ($b = 65; $b <= 90; $b++) {
        $code = chr($a) . chr($b);
        $name = \Locale::getDisplayRegion('-' . $code, 'en');
        // Unknown codes echo back the code itself.
        if ($name && $name !== $code && !preg_match('/^[A-Z]{2}$/', $name)) {
            $map[$code] = $name;
        }
    }
}
ksort($map);
$out = "<?php\n"
     . "/**\n"
     . " * ISO 3166-1 alpha-2 country code => English display name.\n"
     . " *\n"
     . " * GENERATED FILE — do not edit by hand. Produced from the PHP intl\n"
     . " * extension (ICU) so the names match what browsers and OSes display.\n"
     . " * Used as a fallback on hosts where ext-intl is unavailable; where intl\n"
     . " * IS present AF_Country_Names asks ICU directly instead.\n"
     . " *\n"
     . " * Regenerate: php bin/generate-country-names.php\n"
     . " */\n\n"
     . "return " . var_export($map, true) . ";\n";
file_put_contents($argv[1], $out);
echo "wrote " . count($map) . " countries to " . $argv[1] . "\n";
