tourze / gb-t-2659
GB/T 2659 world countries and regions name code
Installs: 1 094
Dependents: 5
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
pkg:composer/tourze/gb-t-2659
Requires
- php: ^8.1
- tourze/enum-extra: 0.1.*
Requires (Dev)
- phpstan/phpstan: ^2.1
- phpunit/phpunit: ^10.0
This package is auto-updated.
Last update: 2025-11-01 19:16:36 UTC
README
A PHP implementation of GB/T 2659 country and region codes (2-letter) based on PHP 8.1 enums.
Features
- Complete GB/T 2659 implementation: All 249 countries and regions included
- Type-safe enums: Built on PHP 8.1 backed enums for maximum type safety
- Bilingual support: Chinese labels with English documentation
- Form integration: Ready-to-use with dropdown lists and form components
- Extensible: Implements
Labelable,Itemable, andSelectableinterfaces - Utility methods: Lowercase conversion, array transformation, and more
Installation
Requirements:
- PHP >= 8.1
tourze/enum-extrapackage
Install via Composer:
composer require tourze/gb-t-2659
Quick Start
<?php use Tourze\GBT2659\Alpha2Code; // Basic usage $china = Alpha2Code::CN; echo $china->value; // Output: CN echo $china->getLabel(); // Output: 中国 echo $china->toLowerCase(); // Output: cn // Create from string $usa = Alpha2Code::from('US'); echo $usa->getLabel(); // Output: 美国 // Safe creation with tryFrom $code = Alpha2Code::tryFrom('UK'); if ($code === null) { echo "Invalid code"; } // Get all countries $allCountries = Alpha2Code::cases(); echo count($allCountries); // Output: 249
Advanced Usage
Form Integration
// Generate options for dropdown $options = Alpha2Code::genOptions(); // Returns: [['value' => 'CN', 'label' => '中国'], ...] // Convert to array $array = Alpha2Code::CN->toArray(); // Returns: ['value' => 'CN', 'label' => '中国'] // Convert to select item $item = Alpha2Code::CN->toSelectItem(); // Returns: ['value' => 'CN', 'label' => '中国', 'text' => '中国', 'name' => '中国']
Country Filtering
// Filter Asian countries $asianCountries = [ Alpha2Code::CN, Alpha2Code::JP, Alpha2Code::KR, Alpha2Code::IN, Alpha2Code::SG, Alpha2Code::TH ]; $userCountry = Alpha2Code::from('JP'); if (in_array($userCountry, $asianCountries)) { echo "User is from Asia"; }
API Reference
Methods
Alpha2Code::cases()- Get all enum casesAlpha2Code::from(string $value)- Create enum from string (throws on invalid)Alpha2Code::tryFrom(string $value)- Create enum from string (returns null on invalid)getLabel()- Get Chinese country nametoLowerCase()- Get lowercase country codetoArray()- Convert to array with value and labeltoSelectItem()- Convert to select item formatgenOptions()- Generate all options for forms
Properties
value- The 2-letter country codename- The enum case name (same as value)
Testing
Run tests with PHPUnit:
./vendor/bin/phpunit packages/gb-t-2659/tests
Contributing
- Check existing issues and PRs before submitting new ones
- Follow PSR-12 coding standards
- Add tests for new features
- Update documentation when needed
License
The MIT License (MIT). Please see License File for more information.
Changelog
See Git commit history for detailed changelog information.