tourze / gb-t-12406
GB/T 12406
Installs: 267
Dependents: 1
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
pkg:composer/tourze/gb-t-12406
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-10-31 07:40:27 UTC
README
A PHP package that implements the GB/T 12406 standard for currency codes, based on PHP 8.1+ enum, providing type-safe and convenient currency code handling.
Features
- Complete GB/T 12406 Implementation: All 285 international currency codes
- Type-safe PHP 8.1+ Enum: Full IDE autocompletion and type safety
- Multilingual Labels: Chinese names for all currency codes
- Flexible Data Export: Multiple format support (array, select options)
- Dynamic Configuration: Environment-based option filtering
- Historical Currency Support: Includes deprecated currencies (DEM, FRF, etc.)
- Framework Agnostic: Works with any PHP framework or standalone
Installation
Requirements:
- PHP 8.1 or above
- Dependency: tourze/enum-extra>= 0.0.5
Install via Composer:
composer require tourze/gb-t-12406
Quick Start
use Tourze\GBT12406\Currency; // Get currency code $currency = Currency::CNY; // Get currency Chinese name echo $currency->getLabel(); // Output: 人民币 // Get all available currencies $allCurrencies = Currency::cases(); // Returns all 285 currencies // Convert currency to array item $item = Currency::CNY->toArray(); // [ // 'value' => 'CNY', // 'label' => '人民币' // ] // Convert to select item (for frontend) $selectItem = Currency::CNY->toSelectItem(); // [ // 'value' => 'CNY', // 'label' => '人民币', // 'text' => '人民币', // 'name' => '人民币' // ]
API Reference
Basic Methods
- getLabel(): string- Get Chinese name of the currency
- toArray(): array- Convert to simple key-value array
- toSelectItem(): array- Convert to select option with multiple aliases
- Currency::cases(): array- Get all available currency enums
- Currency::from(string $value): Currency- Create enum from currency code
- Currency::tryFrom(string $value): ?Currency- Safe creation, returns null if invalid
Advanced Features
Generate Options List
// Generate all available options $options = Currency::genOptions(); // Returns array of ['value' => 'CODE', 'label' => 'Name'] items
Dynamic Option Filtering
// Disable specific currencies from options $_ENV['enum-display:' . Currency::class . '-CNY'] = false; $_ENV['enum-display:' . Currency::class . '-USD'] = false; $filteredOptions = Currency::genOptions(); // CNY and USD will be excluded from the list
Common Currency Examples
Currency::CNY->getLabel(); // "人民币" Currency::USD->getLabel(); // "美元" Currency::EUR->getLabel(); // "欧元" Currency::JPY->getLabel(); // "日元" Currency::GBP->getLabel(); // "英镑"
Extended Features
This package relies on tourze/enum-extra for:
- Labelableinterface for label support
- Itemableinterface for array conversion
- Selectableinterface for option generation
- Dynamic configuration via environment variables
Contributing
Feel free to submit issues and PRs. Please follow PSR standards and ensure tests pass with PHPUnit before submitting.
License
MIT License © tourze
Changelog
See [CHANGELOG.md] or git history for details.