131 lines
3.6 KiB
PHP
131 lines
3.6 KiB
PHP
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
class RegisterLocationService
|
|
{
|
|
private const SIGNUP_COUNTRY_TO_CSC_NAME = [
|
|
'Antigua and Barbuda' => 'Antigua And Barbuda',
|
|
'Eswatini' => 'Swaziland',
|
|
'Guernsey' => 'Guernsey and Alderney',
|
|
'Ivory Coast' => "Cote D'Ivoire (Ivory Coast)",
|
|
'North Macedonia' => 'Macedonia',
|
|
'Papua New Guinea' => 'Papua new Guinea',
|
|
'Saint Kitts and Nevis' => 'Saint Kitts And Nevis',
|
|
'Saint Vincent and the Grenadines' => 'Saint Vincent And The Grenadines',
|
|
'Svalbard and Jan Mayen Islands' => 'Svalbard And Jan Mayen Islands',
|
|
'Timor-Leste' => 'East Timor',
|
|
'Trinidad and Tobago' => 'Trinidad And Tobago',
|
|
'Turks and Caicos Islands' => 'Turks And Caicos Islands',
|
|
'Wallis and Futuna Islands' => 'Wallis And Futuna Islands',
|
|
];
|
|
|
|
private static $countries;
|
|
private static $states;
|
|
private static $cities;
|
|
|
|
public function getCitiesForCountryState(string $countryName, string $stateName): array
|
|
{
|
|
$countryName = trim($countryName);
|
|
$stateName = trim($stateName);
|
|
if ($countryName === '' || $stateName === '') {
|
|
return [];
|
|
}
|
|
|
|
$countryCode = $this->resolveCountryCode($countryName);
|
|
if (!$countryCode) {
|
|
return [];
|
|
}
|
|
|
|
$stateCode = $this->resolveStateCode($countryCode, $stateName);
|
|
if (!$stateCode) {
|
|
return [];
|
|
}
|
|
|
|
$names = [];
|
|
foreach ($this->loadCities() as $row) {
|
|
if (!is_array($row) || count($row) < 3) {
|
|
continue;
|
|
}
|
|
if (($row[1] ?? '') === $countryCode && ($row[2] ?? '') === $stateCode) {
|
|
$names[] = $row[0];
|
|
}
|
|
}
|
|
|
|
$names = array_values(array_unique($names));
|
|
sort($names, SORT_STRING);
|
|
|
|
return $names;
|
|
}
|
|
|
|
private function resolveCountryCode(string $countryName): ?string
|
|
{
|
|
$lookup = self::SIGNUP_COUNTRY_TO_CSC_NAME[$countryName] ?? $countryName;
|
|
|
|
foreach ($this->loadCountries() as $country) {
|
|
if (!is_array($country)) {
|
|
continue;
|
|
}
|
|
$name = $country['name'] ?? '';
|
|
if ($name === $lookup || $name === $countryName) {
|
|
return $country['isoCode'] ?? null;
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
private function resolveStateCode(string $countryCode, string $stateName): ?string
|
|
{
|
|
foreach ($this->loadStates() as $state) {
|
|
if (!is_array($state)) {
|
|
continue;
|
|
}
|
|
if (($state['countryCode'] ?? '') === $countryCode && ($state['name'] ?? '') === $stateName) {
|
|
return $state['isoCode'] ?? null;
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
private function loadCountries(): array
|
|
{
|
|
if (self::$countries === null) {
|
|
self::$countries = $this->readJson('country.json');
|
|
}
|
|
|
|
return self::$countries;
|
|
}
|
|
|
|
private function loadStates(): array
|
|
{
|
|
if (self::$states === null) {
|
|
self::$states = $this->readJson('state.json');
|
|
}
|
|
|
|
return self::$states;
|
|
}
|
|
|
|
private function loadCities(): array
|
|
{
|
|
if (self::$cities === null) {
|
|
self::$cities = $this->readJson('city.json');
|
|
}
|
|
|
|
return self::$cities;
|
|
}
|
|
|
|
private function readJson(string $filename): array
|
|
{
|
|
$path = storage_path('app/geo/' . $filename);
|
|
if (!is_readable($path)) {
|
|
return [];
|
|
}
|
|
|
|
$decoded = json_decode(file_get_contents($path), true);
|
|
|
|
return is_array($decoded) ? $decoded : [];
|
|
}
|
|
}
|