Files
Investidor-V2/app/Services/CountryCatalogue.php
T
nala e662230df7
tests / ci (push) Canceled after 0s
oportunidades 3
2026-10-09 15:00:37 +02:00

36 lines
1.2 KiB
PHP

<?php
namespace App\Services;
use Illuminate\Validation\Rule;
/** Shared ISO identifiers; independent of administrative territories and IP data. */
final class CountryCatalogue
{
/** @return list<array{code: string, name: string, name_en: string}> */
public function all(): array
{
$countries = [];
$lines = file(resource_path('data/iso3166.tab'), FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
if ($lines === false) {
throw new \RuntimeException('Catálogo ISO local indisponível.');
}
foreach ($lines as $line) {
if (str_starts_with($line, '#')) {
continue;
}
[$code, $english] = explode("\t", $line, 2);
$name = class_exists(\Locale::class) ? \Locale::getDisplayRegion('und_'.$code, 'pt') : $english;
$countries[] = ['code' => $code, 'name' => is_string($name) && $name !== '' ? $name : $english, 'name_en' => $english];
}
usort($countries, fn ($a, $b) => strnatcasecmp($a['name'], $b['name']));
return $countries;
}
public function rule(): \Illuminate\Validation\Rules\In
{
return Rule::in(array_column($this->all(), 'code'));
}
}