prinsfrank/standards
Daily-updated PHP 8.1+ enum collection of international standards (ISO, IANA, SIX, Library of Congress, etc.). Easy Composer install, strong typing for codes like countries, currencies, languages, and more—kept current via automated upstream sync.
Installation:
composer require prinsfrank/standards
Ensure PHP 8.1+ is used.
First Use Case: Validate and work with standardized data types. For example, enforce valid country codes in a form handler:
use PrinsFrank\Standards\Country\CountryAlpha2;
public function store(Request $request)
{
$country = CountryAlpha2::from($request->input('country'));
// $country now enforces valid ISO 3166-1 Alpha-2 codes
}
Key Entry Points:
Country, Currency, Language).from() method to instantiate enums from raw values (e.g., CountryAlpha2::from('US')).Type Safety: Use enums as method parameters or return types to enforce valid values:
public function getCountryName(CountryAlpha2 $country): string
{
return $country->name;
}
Data Conversion: Leverage relationships between enums (e.g., country codes ↔ numeric values):
$countryAlpha3 = CountryAlpha3::from('USA');
$countryNumeric = $countryAlpha3->toCountryNumeric(); // CountryNumeric::USA
Localization: Fetch localized names or formats (e.g., currency, numbers):
$country = CountryAlpha2::France;
$formattedNumber = $country->formatNumber(1000.5, LanguageAlpha2::French);
// Outputs: "1 000,5" (French locale)
Group Membership Checks: Validate if a country belongs to a group (e.g., EU, NATO):
if (CountryAlpha2::Germany->isMemberOf(EU::class)) {
// Logic for EU countries
}
Phone Number Handling: Format phone numbers based on country-specific rules:
$phoneNumber = CountryAlpha2::Japan->formatPhoneNumber('0312345678');
// Outputs: "+81 3-1234-5678"
FormRequest validation:
public function rules(): array
{
return [
'country' => ['required', Rule::enum(CountryAlpha2::class)],
];
}
CountryAlpha2::US->value) for standardized responses.CountryAlpha3::US->value) in DB columns with string type.$this->app->bind(
CountryService::class,
fn() => new CountryService(CountryAlpha2::class)
);
Case Sensitivity:
Alpha2, Alpha3) are case-sensitive (e.g., CountryAlpha2::from('nl') fails; use 'NL').Alpha2, Alpha3) are case-sensitive (e.g., LanguageAlpha2::from('en') works; 'EN' fails).Backed Enum Values:
CountryAlpha2::US->value returns 'US'). Avoid comparing names directly:
// ❌ Avoid (compares names, not values)
if ($country->name === 'United States') { ... }
// ✅ Correct (compares values)
if ($country->value === 'US') { ... }
Non-Bidirectional Conversions:
LanguageTag → Country is supported, but Country → LanguageTag requires additional logic).Flag Emojis:
$country->getFlagEmoji()) may not render correctly on Windows. Use workarounds.Deprecated Entries:
CountryAlpha2::Czechoslovakia) may exist but are deprecated. Check isDeprecated() if needed.try-catch for invalid inputs:
try {
$country = CountryAlpha2::from('ZZ'); // Throws InvalidArgumentException
} catch (\InvalidArgumentException $e) {
// Handle invalid country code
}
foreach (CountryAlpha2::cases() as $country) {
echo $country->value . "\n";
}
Custom Groups:
Extend GroupInterface to create custom country groups:
class CustomGroup implements GroupInterface {
public function isMember(CountryAlpha2 $country): bool { ... }
}
Localization Overrides: Override default names/translations by extending enums:
final class CustomCountryName extends CountryName {
public static function from(string $value): self {
return parent::from($value)->withName('Custom Name');
}
}
Adding New Standards:
Fork the package and add new enums by following the existing pattern (e.g., Country, Currency). Submit a PR to upstream.
Performance:
$cache = new \Symfony\Component\Cache\Adapter\ArrayAdapter();
$country = $cache->get('country_US', fn() => CountryAlpha2::US);
Testing:
assertSame() for enum comparisons in tests:
$this->assertSame(CountryAlpha2::US, CountryAlpha2::from('US'));
CountryAlpha2::Antarctica).How can I help you explore Laravel packages today?