openclassify/app/Support/CountryCodeManager.php
2026-03-03 13:59:46 +03:00

169 lines
5.0 KiB
PHP

<?php
namespace App\Support;
use Illuminate\Support\Collection;
use Tapp\FilamentCountryCodeField\Enums\CountriesEnum;
class CountryCodeManager
{
public static function defaultCountryCode(): string
{
return self::normalizeCountryCode(config('app.default_country_code', '+90'));
}
public static function defaultCountryIso2(): string
{
return self::iso2FromCountryCode(self::defaultCountryCode()) ?? 'TR';
}
public static function normalizeCountryCode(null | string $value): string
{
$value = trim((string) $value);
if ($value === '') {
return '+90';
}
if (self::isValidCountryCode($value)) {
return $value;
}
return self::countryCodeFromIso2($value) ?? '+90';
}
public static function isValidCountryCode(null | string $value): bool
{
if (! filled($value)) {
return false;
}
return self::countries()->contains(fn (array $country): bool => $country['country_code'] === trim((string) $value));
}
public static function countryCodeFromIso2(null | string $iso2): ?string
{
$iso2 = strtoupper(trim((string) $iso2));
if ($iso2 === '') {
return null;
}
return self::countries()
->first(fn (array $country): bool => $country['iso2'] === $iso2)['country_code'] ?? null;
}
public static function iso2FromCountryCode(null | string $countryCode): ?string
{
$countryCode = trim((string) $countryCode);
if ($countryCode === '') {
return null;
}
return self::countries()
->first(fn (array $country): bool => $country['country_code'] === $countryCode)['iso2'] ?? null;
}
public static function labelFromCountryCode(null | string $countryCode): ?string
{
$countryCode = trim((string) $countryCode);
if ($countryCode === '') {
return null;
}
return self::countries()
->first(fn (array $country): bool => $country['country_code'] === $countryCode)['english_label'] ?? null;
}
public static function countryCodeFromLabelOrCode(null | string $value): ?string
{
$value = trim((string) $value);
if ($value === '') {
return null;
}
if (self::isValidCountryCode($value)) {
return $value;
}
$fromIso = self::countryCodeFromIso2($value);
if ($fromIso) {
return $fromIso;
}
$normalizedInput = self::normalizeLabel($value);
return self::countries()
->first(function (array $country) use ($normalizedInput): bool {
$normalizedLabel = self::normalizeLabel($country['label']);
$normalizedEnglish = self::normalizeLabel($country['english_label']);
return $normalizedInput === $normalizedLabel
|| $normalizedInput === $normalizedEnglish
|| str_contains($normalizedLabel, $normalizedInput)
|| str_contains($normalizedEnglish, $normalizedInput)
|| str_contains($normalizedInput, $normalizedLabel)
|| str_contains($normalizedInput, $normalizedEnglish);
})['country_code'] ?? null;
}
public static function normalizeStoredCountry(null | string $value): ?string
{
$value = trim((string) $value);
if ($value === '') {
return null;
}
$countryCode = self::countryCodeFromLabelOrCode($value);
if (! $countryCode) {
return $value;
}
return self::labelFromCountryCode($countryCode) ?? $value;
}
/**
* @return Collection<int, array{country_code: string, iso2: string, label: string, english_label: string}>
*/
private static function countries(): Collection
{
static $countries;
if ($countries instanceof Collection) {
return $countries;
}
$countries = collect(CountriesEnum::cases())
->map(function (CountriesEnum $country): array {
$countryKey = $country->value;
$iso2 = strtoupper(explode('_', $countryKey)[0] ?? $countryKey);
$label = (string) trans("filament-country-code-field::countries.{$countryKey}");
$englishLabel = (string) trans("filament-country-code-field::countries.{$countryKey}", [], 'en');
return [
'country_code' => $country->getCountryCode(),
'iso2' => $iso2,
'label' => $label,
'english_label' => $englishLabel,
];
})
->values();
return $countries;
}
private static function normalizeLabel(string $value): string
{
$value = mb_strtolower(trim($value));
$value = preg_replace('/[^a-z0-9]+/u', ' ', $value) ?? $value;
return trim(preg_replace('/\s+/', ' ', $value) ?? $value);
}
}