You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
virt/web/app/Services/Cloud/CloudCountryCatalog.php

58 lines
1.7 KiB
PHP

<?php
namespace App\Services\Cloud;
/** Shared country/region choices used by region and host administration. */
final class CloudCountryCatalog
{
private const COUNTRIES = [
'CN' => ['name' => '中国', 'flag' => '🇨🇳'],
'HK' => ['name' => '中国香港', 'flag' => '🇭🇰'],
'JP' => ['name' => '日本', 'flag' => '🇯🇵'],
'US' => ['name' => '美国', 'flag' => '🇺🇸'],
'SG' => ['name' => '新加坡', 'flag' => '🇸🇬'],
];
public static function options(): array
{
$options = [];
foreach (self::COUNTRIES as $code => $country) {
$options[] = [
'code' => $code,
'name' => $country['name'],
'flag' => $country['flag'],
];
}
return $options;
}
public static function normalize($value): string
{
if (!is_string($value)) {
throw new CloudError('国家/地区无效,请从列表中选择');
}
$code = strtoupper(trim($value));
if (!array_key_exists($code, self::COUNTRIES)) {
throw new CloudError('国家/地区无效,请从列表中选择');
}
return $code;
}
public static function option($value): array
{
$code = is_string($value) ? strtoupper(trim($value)) : '';
if (!array_key_exists($code, self::COUNTRIES)) {
return [
'code' => $code,
'name' => '未支持的国家 / 地区',
'flag' => '🌐',
];
}
return [
'code' => $code,
'name' => self::COUNTRIES[$code]['name'],
'flag' => self::COUNTRIES[$code]['flag'],
];
}
}