@@ -0,0 +1,35 @@
|
||||
<?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'));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user