feat: menric
This commit is contained in:
@@ -0,0 +1,75 @@
|
||||
<?php
|
||||
|
||||
namespace App\Support;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
/**
|
||||
* Cloudflare CF-IPCountry: ISO 3166-1 alpha-2, plus T1 (Tor) and XX (unknown).
|
||||
*/
|
||||
final class CfIpCountry
|
||||
{
|
||||
public static function fromRequest(Request $request): ?string
|
||||
{
|
||||
return self::normalize($request->headers->get('CF-IPCountry'));
|
||||
}
|
||||
|
||||
public static function normalize(?string $raw): ?string
|
||||
{
|
||||
$code = strtoupper(trim((string) $raw));
|
||||
if ($code === 'T1') {
|
||||
return 'T1';
|
||||
}
|
||||
if (preg_match('/^[A-Z]{2}$/', $code) !== 1) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $code;
|
||||
}
|
||||
|
||||
public static function label(?string $code): string
|
||||
{
|
||||
$code = self::normalize($code);
|
||||
if ($code === null) {
|
||||
return '';
|
||||
}
|
||||
|
||||
return self::names()[$code] ?? $code;
|
||||
}
|
||||
|
||||
/** @return array<string, string> */
|
||||
public static function names(): array
|
||||
{
|
||||
return [
|
||||
'AE' => '阿联酋',
|
||||
'AU' => '澳大利亚',
|
||||
'BR' => '巴西',
|
||||
'CA' => '加拿大',
|
||||
'CN' => '中国',
|
||||
'DE' => '德国',
|
||||
'ES' => '西班牙',
|
||||
'FR' => '法国',
|
||||
'GB' => '英国',
|
||||
'HK' => '香港',
|
||||
'ID' => '印尼',
|
||||
'IN' => '印度',
|
||||
'IT' => '意大利',
|
||||
'JP' => '日本',
|
||||
'KR' => '韩国',
|
||||
'MO' => '澳门',
|
||||
'MY' => '马来西亚',
|
||||
'NL' => '荷兰',
|
||||
'PH' => '菲律宾',
|
||||
'RU' => '俄罗斯',
|
||||
'SA' => '沙特',
|
||||
'SG' => '新加坡',
|
||||
'TH' => '泰国',
|
||||
'TR' => '土耳其',
|
||||
'TW' => '台湾',
|
||||
'US' => '美国',
|
||||
'VN' => '越南',
|
||||
'T1' => 'Tor',
|
||||
'XX' => '未知',
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user