steefdw / standards-enums
The Standards Enums package provides a simple and consistent way to work with countries, languages, currencies, and locales using enums.Each Enum comes with methods for accessing related information, like the name, flag, description, and more.Additionally, helper methods are provided to easily get a
Requires
- php: >=8.1
Requires (Dev)
- ext-mbstring: *
- friendsofphp/php-cs-fixer: ^3.11
- pdepend/pdepend: 2.14.0
- phpmd/phpmd: ^2.13
- phpstan/phpstan: ^1.10.13
- phpunit/phpunit: ^9.5.25
- squizlabs/php_codesniffer: ^3.7.1
README
The Standards Enums package provides a simple and consistent way to work with countries, languages, currencies, and locales using enums.
Each Enum comes with methods for accessing related information, like the name, flag, description, and more. Additionally, helper methods are provided to easily get all possible values or sort them by their name in a given language. These methods can be used with all Enum classes, regardless of whether they represent languages, countries, currencies, or locales.
Standards Enums provides you access to lists of various ISO standards:
Enum in this package | ISO standard |
---|---|
Countries | ISO 3166-1 |
Currencies | ISO 4217 |
Languages | ISO 639 |
Locales | (not ISO) |
Much of the code in this package is automatically generated with the data from https://salsa.debian.org/iso-codes-team/iso-codes.
Documentation
- Full documentation: https://steefdw.gitlab.io/standards-enums
- Short documentation: this readme.
Installation
composer require steefdw/standards-enums
Usage
Countries example:
$country = CountryAlpha2::tryFrom('nl'); // CountryAlpha2::NL
$countryName = $country->getName()->value; // 'Netherlands'
$countryAlpha2 = $countryName->getAlpha2()->value; // 'nl'
$countryAlpha3 = $countryAlpha2->getAlpha3()->value; // 'NLD'
$countryFlag = $countryAlpha3->getFlag()->value; // '🇳🇱'
$countryNumber = $countryFlag->getNumeric()->value; // '528'
$countryCurrencies = $country->getCurrencies(); // ['EUR' => CurrencyName::EUR]
// these methods work for all country Enums
Currencies example:
$currency = CurrencyName::tryFrom('Euro'); // CurrencyName::EUR
$currencyName = $currency->getName()->value; // 'Euro'
$currencyAlpha3 = $currency->getAlpha3()->value; // 'EUR'
$currencyNumber = $currency->getNumeric()->value; // '978'
$currencyCountries = $currency->getCountries(); // [
// 'AD' => CountryName::AD, // Andorra
// 'AT' => CountryName::AT, // Austria
// 'BE' => CountryName::BE, // Belgium
// ...]
// these methods work for all currency Enums
Languages example:
$language = LanguageAlpha2::tryFrom('nl'); // LanguageAlpha2::NLD
$languageName = $language->getName()->value; // 'Dutch; Flemish'
$languageAlpha2 = $languageName->getAlpha2()->value; // 'nl'
$languageAlpha3 = $languageAlpha2->getAlpha3()->value; // 'nld'
// these methods work for all language Enums
Locales example:
$locale = LocaleCode::tryFromName('nl_BE'); // LocaleCode::NL_BE
$description = $locale->getDescription(); // LocaleDescription::NL_BE
$description->value; // 'Dutch (Belgium)'
$locale->getLanguage()->value; // 'Dutch; Flemish'
$locale->getCountry()->value; // 'Belgium'
// these methods work for all locale Enums
See the usage pages for more information:
Please refer to these sections of the documentation for more detailed information on how to use each enum type.
- Using the countries enums
- Using the currencies enums
- Using the languages enums
- Using the locales enums
Testing
composer test
Or:
make test
Contributing
- make sure the code validation succeeds. You can run it with
make validate
. - make sure all tests succeed. You can run them with
make test
. - make sure all code is tested.
License
The MIT License. Please see Licence File for more information.