xref: /webtrees/app/Http/RequestHandlers/AbstractAutocompleteHandler.php (revision 89f7189b61a494347591c99bdb92afb7d8b66e1b)
163763244SGreg Roach<?php
263763244SGreg Roach
363763244SGreg Roach/**
463763244SGreg Roach * webtrees: online genealogy
5*89f7189bSGreg Roach * Copyright (C) 2021 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
15*89f7189bSGreg 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
4163763244SGreg Roach    /** @var SearchService */
4263763244SGreg Roach    protected $search_service;
4363763244SGreg Roach
4463763244SGreg Roach    /**
4563763244SGreg Roach     * @param SearchService $search_service
4663763244SGreg Roach     */
4763763244SGreg Roach    public function __construct(SearchService $search_service)
4863763244SGreg Roach    {
4963763244SGreg Roach        $this->search_service = $search_service;
5063763244SGreg Roach    }
5163763244SGreg Roach
5263763244SGreg Roach    /**
5363763244SGreg Roach     * @param ServerRequestInterface $request
5463763244SGreg Roach     *
5563763244SGreg Roach     * @return ResponseInterface
5663763244SGreg Roach     */
5763763244SGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
5863763244SGreg Roach    {
5963763244SGreg Roach        $data = $this->search($request)
6063763244SGreg Roach            ->map(static function (string $datum): array {
6163763244SGreg Roach                return ['value' => $datum];
6263763244SGreg Roach            });
6363763244SGreg Roach
6463763244SGreg Roach        return response($data)
6563763244SGreg Roach            ->withHeader('Cache-Control', 'public,max-age=' . static::CACHE_LIFE);
6663763244SGreg Roach    }
6763763244SGreg Roach
6863763244SGreg Roach    /**
6963763244SGreg Roach     * @param ServerRequestInterface $request
7063763244SGreg Roach     *
7163763244SGreg Roach     * @return Collection<string>
7263763244SGreg Roach     */
7363763244SGreg Roach    abstract protected function search(ServerRequestInterface $request): Collection;
7463763244SGreg Roach}
75