Something went wrong. Try again.
Easy Address Field is the easiest way to add an address to your entries.
Something went wrong. Try again.
12345678910111213141516171819202122232425262728293031323334353637383940414243<?php
namespace studioespresso\easyaddressfield\Services;
use Giggsey\Locale\Locale;use Illuminate\Container\Attributes\Singleton;use League\ISO3166\ISO3166;use Throwable;
#[Singleton]class CountriesService{ /** * @return array<string, string> Country names keyed by alpha-2 code, sorted by name */ public function getCountriesAsArray(): array { $locale = app()->getLocale(); $countries = []; foreach ((new ISO3166)->all() as $country) { $countries[$country['alpha2']] = Locale::getDisplayRegion('-'.$country['alpha2'], $locale) ?: $country['name']; } asort($countries);
return $countries; }
public function getCountryNameByAlpha2(string $code, string $locale): ?string { try { $country = (new ISO3166)->alpha2($code); } catch (Throwable) { return null; }
try { return Locale::getDisplayRegion('-'.$country['alpha2'], $locale) ?: $country['name']; } catch (Throwable) { return $country['name']; } }}