2026-06-14 15:29:29 +08:00
|
|
|
<?php
|
|
|
|
|
/**
|
|
|
|
|
* SHOW_SITE_COUNTRY 允许列表解析与比对
|
|
|
|
|
*/
|
2026-06-15 16:53:05 +08:00
|
|
|
require_once __DIR__ . '/CountryCodeNormalizer.php';
|
|
|
|
|
|
2026-06-14 15:29:29 +08:00
|
|
|
class CountryAllowlist
|
|
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* @param string $config 逗号分隔国家码,如 US,GB,CA
|
|
|
|
|
* @return string[]
|
|
|
|
|
*/
|
|
|
|
|
public static function parse($config)
|
|
|
|
|
{
|
|
|
|
|
$config = strtoupper(trim((string) $config));
|
|
|
|
|
if ($config === '') {
|
|
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
$parts = preg_split('/\s*,\s*/', $config);
|
|
|
|
|
$out = [];
|
|
|
|
|
foreach ($parts as $part) {
|
|
|
|
|
$part = trim($part);
|
|
|
|
|
if ($part !== '') {
|
|
|
|
|
$out[] = $part;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return array_values(array_unique($out));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string|null $countryCode
|
|
|
|
|
* @param string[] $allowlist
|
|
|
|
|
*/
|
|
|
|
|
public static function isAllowed($countryCode, array $allowlist)
|
|
|
|
|
{
|
|
|
|
|
if ($allowlist === []) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2026-06-15 16:53:05 +08:00
|
|
|
$normalized = CountryCodeNormalizer::toAlpha2($countryCode);
|
|
|
|
|
if ($normalized === null || $normalized === '') {
|
2026-06-14 15:29:29 +08:00
|
|
|
return false;
|
|
|
|
|
}
|
2026-06-15 16:53:05 +08:00
|
|
|
return in_array($normalized, $allowlist, true);
|
2026-06-14 15:29:29 +08:00
|
|
|
}
|
|
|
|
|
}
|