Created
February 25, 2025 13:09
-
-
Save richardnbanks/3d885fde23b146720cc1094bbe003ba7 to your computer and use it in GitHub Desktop.
Generate a country code enum for Lighthouse PHP using a list of countries from league/iso3166
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require __DIR__ . '/vendor/autoload.php'; | |
// composer require league/iso3166 | |
$countries = []; | |
foreach ((new League\ISO3166\ISO3166)->all() as $country) { | |
$countries[$country['alpha2']] = $country['name']; | |
} | |
$enumDefinition = "<?php\n\nnamespace App\Enums;\n\nuse GraphQL\Type\Definition\Description;\n\n#[Description(description: 'List of Countries')]\nenum CountryCode {\n"; | |
foreach ($countries as $key => $value) { | |
$caseName = ucfirst($key); // Or any other transformation | |
$description = str_replace("'", "\'", $value); | |
$enumDefinition .= " #[Description(description: '$description')]\n"; | |
$enumDefinition .= " case {$caseName};\n\n"; | |
} | |
$enumDefinition .= "}\n"; | |
file_put_contents('CountryCode.php', $enumDefinition); // Save to a file |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment