@@ -3,19 +3,25 @@
|
||||
namespace App\Http\Controllers\Api\V1;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Services\CountryCatalogue;
|
||||
use Illuminate\Database\Query\Builder;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Pagination\LengthAwarePaginator;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Str;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
|
||||
class ReferenceCatalogController extends Controller
|
||||
{
|
||||
private const CATALOGS = ['authorities', 'jurisdictions', 'economic-activities'];
|
||||
private const CATALOGS = ['authorities', 'jurisdictions', 'economic-activities', 'countries'];
|
||||
|
||||
public function index(Request $request, string $catalog): JsonResponse
|
||||
{
|
||||
$catalog = $this->validatedCatalog($catalog);
|
||||
if ($catalog === 'countries') {
|
||||
return $this->countries($request);
|
||||
}
|
||||
|
||||
$rules = [
|
||||
'q' => ['nullable', 'string', 'max:100'],
|
||||
@@ -89,6 +95,21 @@ class ReferenceCatalogController extends Controller
|
||||
->header('ETag', 'W/"catalog-'.substr(hash('sha256', $catalog.'|'.$version->version_no.'|'.json_encode($data, JSON_THROW_ON_ERROR)), 0, 32).'"');
|
||||
}
|
||||
|
||||
private function countries(Request $request): JsonResponse
|
||||
{
|
||||
$data = $request->validate(['q' => 'nullable|string|max:100', 'page' => 'nullable|integer|min:1', 'per_page' => 'nullable|integer|min:1|max:100', 'parent_id' => 'prohibited', 'version' => 'prohibited']);
|
||||
$term = strtolower(Str::ascii(trim($data['q'] ?? '')));
|
||||
$items = collect(app(CountryCatalogue::class)->all())
|
||||
->filter(fn (array $country) => $term === '' || str_contains(strtolower(Str::ascii(implode(' ', $country))), $term))
|
||||
->values()->map(fn (array $country) => ['id' => $country['code'], 'value' => $country['code'], 'code' => $country['code'], 'name' => $country['name'], 'label' => $country['name']]);
|
||||
$page = (int) ($data['page'] ?? 1);
|
||||
$perPage = (int) ($data['per_page'] ?? 50);
|
||||
$result = new LengthAwarePaginator($items->forPage($page, $perPage)->values(), $items->count(), $perPage, $page, ['path' => $request->url(), 'query' => $request->query()]);
|
||||
|
||||
return response()->json([...$result->toArray(), 'meta' => ['catalog' => 'countries', 'source' => 'Local ISO 3166 catalogue', 'sha256' => hash_file('sha256', resource_path('data/iso3166.tab'))]])
|
||||
->header('Cache-Control', 'public, max-age=300');
|
||||
}
|
||||
|
||||
/** @param array<string,mixed> $data */
|
||||
private function authorities(string $versionId, array $data): Builder
|
||||
{
|
||||
@@ -171,7 +192,7 @@ class ReferenceCatalogController extends Controller
|
||||
});
|
||||
}
|
||||
|
||||
/** @return 'authorities'|'jurisdictions'|'economic-activities' */
|
||||
/** @return 'authorities'|'jurisdictions'|'economic-activities'|'countries' */
|
||||
private function validatedCatalog(string $catalog): string
|
||||
{
|
||||
abort_unless(in_array($catalog, self::CATALOGS, true), 404);
|
||||
|
||||
Reference in New Issue
Block a user