fix: clarify resource region fields
parent
069c7f1b56
commit
f2c624b2db
@ -0,0 +1,57 @@
|
||||
<?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'],
|
||||
];
|
||||
}
|
||||
}
|
||||
@ -1,2 +1,2 @@
|
||||
{* Shared Venix CSS/JS cache key. Increment whenever a shared browser asset changes. *}
|
||||
{assign var=venix_asset_version value='2026100313' scope='global'}
|
||||
{assign var=venix_asset_version value='2026100314' scope='global'}
|
||||
|
||||
@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
use App\Services\Cloud\CloudCountryCatalog;
|
||||
use App\Services\Cloud\CloudError;
|
||||
|
||||
require dirname(__DIR__) . '/app/Services/Cloud/CloudError.php';
|
||||
require dirname(__DIR__) . '/app/Services/Cloud/CloudCountryCatalog.php';
|
||||
|
||||
$assert = static function ($condition, string $message): void {
|
||||
if (!$condition) {
|
||||
throw new RuntimeException($message);
|
||||
}
|
||||
};
|
||||
|
||||
$options = CloudCountryCatalog::options();
|
||||
$codes = array_column($options, 'code');
|
||||
$assert($codes === ['CN', 'HK', 'JP', 'US', 'SG'],
|
||||
'Country options must remain ordered, explicit and unique.');
|
||||
$assert(CloudCountryCatalog::normalize(' jp ') === 'JP',
|
||||
'Country normalization must trim and uppercase a complete code.');
|
||||
|
||||
foreach (['JPN', 'ZZ', '', null, 81] as $invalid) {
|
||||
try {
|
||||
CloudCountryCatalog::normalize($invalid);
|
||||
throw new RuntimeException('Invalid country input was accepted: ' . var_export($invalid, true));
|
||||
} catch (CloudError $error) {
|
||||
$assert($error->httpStatus === 422,
|
||||
'Invalid country input must remain a validation error.');
|
||||
}
|
||||
}
|
||||
|
||||
$known = CloudCountryCatalog::option('hk');
|
||||
$assert($known['code'] === 'HK' && $known['name'] === '中国香港'
|
||||
&& $known['flag'] === '🇭🇰',
|
||||
'Known country metadata did not round-trip through the shared catalog.');
|
||||
$unsupported = CloudCountryCatalog::option('de');
|
||||
$assert($unsupported['code'] === 'DE' && $unsupported['flag'] === '🌐',
|
||||
'Historical unsupported codes must remain readable without crashing the resource page.');
|
||||
|
||||
echo "Cloud country catalog checks passed.\n";
|
||||
Loading…
Reference in New Issue