53 lines
1.5 KiB
PHP
Executable File
53 lines
1.5 KiB
PHP
Executable File
<?php
|
|
|
|
namespace Modules\Countries\Support;
|
|
|
|
use Illuminate\Support\Collection;
|
|
use Modules\Countries\Models\Country;
|
|
use Modules\Countries\Models\CountrySubdivision;
|
|
|
|
class CountryRepository
|
|
{
|
|
/**
|
|
* Active countries for form selects.
|
|
*
|
|
* @return Collection<int, Country>
|
|
*/
|
|
public function activeForSelect(): Collection
|
|
{
|
|
return Country::query()->active()->ordered()
|
|
->get(['id', 'alpha2', 'alpha3', 'numeric', 'name', 'display_name', 'common_name', 'flag_emoji', 'region', 'subregion']);
|
|
}
|
|
|
|
public function findByAlpha2(string $alpha2): ?Country
|
|
{
|
|
return Country::query()->where('alpha2', Iso3166::normaliseAlpha2($alpha2))->first();
|
|
}
|
|
|
|
public function findByIsoCode(string $code): ?Country
|
|
{
|
|
return Country::query()->forIsoCode($code)->first();
|
|
}
|
|
|
|
public function findSubdivision(string $code): ?CountrySubdivision
|
|
{
|
|
return CountrySubdivision::query()->where('code', strtoupper(trim($code)))->first();
|
|
}
|
|
|
|
/**
|
|
* Active subdivisions for a country select.
|
|
*
|
|
* @return Collection<int, CountrySubdivision>
|
|
*/
|
|
public function activeSubdivisionsForCountry(string $alpha2): Collection
|
|
{
|
|
$alpha2 = Iso3166::normaliseAlpha2($alpha2);
|
|
|
|
return CountrySubdivision::query()
|
|
->where('country_alpha2', $alpha2)
|
|
->active()
|
|
->ordered()
|
|
->get(['id', 'country_alpha2', 'code', 'subdivision_code', 'name', 'local_name', 'type']);
|
|
}
|
|
}
|