163763244SGreg Roach<?php 263763244SGreg Roach 363763244SGreg Roach/** 463763244SGreg Roach * webtrees: online genealogy 5d11be702SGreg Roach * Copyright (C) 2023 webtrees development team 663763244SGreg Roach * This program is free software: you can redistribute it and/or modify 763763244SGreg Roach * it under the terms of the GNU General Public License as published by 863763244SGreg Roach * the Free Software Foundation, either version 3 of the License, or 963763244SGreg Roach * (at your option) any later version. 1063763244SGreg Roach * This program is distributed in the hope that it will be useful, 1163763244SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of 1263763244SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 1363763244SGreg Roach * GNU General Public License for more details. 1463763244SGreg Roach * You should have received a copy of the GNU General Public License 1589f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>. 1663763244SGreg Roach */ 1763763244SGreg Roach 1863763244SGreg Roachdeclare(strict_types=1); 1963763244SGreg Roach 2063763244SGreg Roachnamespace Fisharebest\Webtrees\Http\RequestHandlers; 2163763244SGreg Roach 2263763244SGreg Roachuse Fisharebest\Webtrees\Services\SearchService; 2363763244SGreg Roachuse Illuminate\Support\Collection; 2463763244SGreg Roachuse Psr\Http\Message\ResponseInterface; 2563763244SGreg Roachuse Psr\Http\Message\ServerRequestInterface; 2663763244SGreg Roachuse Psr\Http\Server\RequestHandlerInterface; 2763763244SGreg Roach 2863763244SGreg Roachuse function response; 2963763244SGreg Roach 3063763244SGreg Roach/** 3163763244SGreg Roach * Autocomplete handler 3263763244SGreg Roach */ 3363763244SGreg Roachabstract class AbstractAutocompleteHandler implements RequestHandlerInterface 3463763244SGreg Roach{ 3563763244SGreg Roach // The client software only shows the first few results 3663763244SGreg Roach protected const LIMIT = 10; 3763763244SGreg Roach 3863763244SGreg Roach // Tell the browser to cache the results 3963763244SGreg Roach protected const CACHE_LIFE = 1200; 4063763244SGreg Roach 41c4943cffSGreg Roach protected SearchService $search_service; 4263763244SGreg Roach 4363763244SGreg Roach /** 4463763244SGreg Roach * @param SearchService $search_service 4563763244SGreg Roach */ 4663763244SGreg Roach public function __construct(SearchService $search_service) 4763763244SGreg Roach { 4863763244SGreg Roach $this->search_service = $search_service; 4963763244SGreg Roach } 5063763244SGreg Roach 5163763244SGreg Roach /** 5263763244SGreg Roach * @param ServerRequestInterface $request 5363763244SGreg Roach * 5463763244SGreg Roach * @return ResponseInterface 5563763244SGreg Roach */ 5663763244SGreg Roach public function handle(ServerRequestInterface $request): ResponseInterface 5763763244SGreg Roach { 5863763244SGreg Roach $data = $this->search($request) 59*f25fc0f9SGreg Roach ->map(static fn(string $datum): array => ['value' => $datum]); 6063763244SGreg Roach 6163763244SGreg Roach return response($data) 626172e7f6SGreg Roach ->withHeader('cache-control', 'public,max-age=' . static::CACHE_LIFE); 6363763244SGreg Roach } 6463763244SGreg Roach 6563763244SGreg Roach /** 6663763244SGreg Roach * @param ServerRequestInterface $request 6763763244SGreg Roach * 6836779af1SGreg Roach * @return Collection<int,string> 6963763244SGreg Roach */ 7063763244SGreg Roach abstract protected function search(ServerRequestInterface $request): Collection; 7163763244SGreg Roach} 72