PHP code example of tourze / gb-t-4880

1. Go to this page and download the library: Download tourze/gb-t-4880 library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

tourze / gb-t-4880 example snippets


use Tourze\GBT4880\Alpha2Code;

// Get language code
$code = Alpha2Code::Chinese->value; // 'zh'

// Get Chinese label
$name = Alpha2Code::Chinese->getLabel(); // '汉语'

// Check if languages are the same
$isSame = Alpha2Code::Chinese === Alpha2Code::from('zh'); // true

// Create from string code
$language = Alpha2Code::from('en');
echo $language->getLabel(); // '英语'

// Safe creation (returns null for invalid codes)
$language = Alpha2Code::tryFrom('invalid');
if ($language === null) {
    echo "Invalid language code";
}

// Generate options for select elements
$options = Alpha2Code::genOptions();
// Returns: [['value' => 'zh', 'label' => '汉语'], ...]

// Convert to array
$data = Alpha2Code::French->toArray();
// Returns: ['value' => 'fr', 'label' => '法语']

// Convert to select item
$item = Alpha2Code::German->toSelectItem();
// Returns: ['value' => 'de', 'label' => '德语']

// Get all language codes
foreach (Alpha2Code::cases() as $language) {
    echo $language->value . ' => ' . $language->getLabel() . PHP_EOL;
}

// Filter languages (example: find all languages containing '语')
$languages = array_filter(Alpha2Code::cases(), function($lang) {
    return str_contains($lang->getLabel(), '语');
});

// Works with form builders
$selectOptions = Alpha2Code::genOptions();

// Works with validation
$isValid = Alpha2Code::tryFrom($userInput) !== null;

// Works with serialization
$jsonData = json_encode(Alpha2Code::Chinese->toArray());