xref: /webtrees/app/Http/RequestHandlers/TomSelectPlace.php (revision d11be7027e34e3121be11cc025421873364403f9)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2023 webtrees development team
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees\Http\RequestHandlers;
21
22use Fisharebest\Webtrees\Place;
23use Fisharebest\Webtrees\Services\SearchService;
24use Fisharebest\Webtrees\Tree;
25use Illuminate\Support\Collection;
26
27/**
28 * Autocomplete for places.
29 */
30class TomSelectPlace extends AbstractTomSelectHandler
31{
32    protected SearchService $search_service;
33
34    /**
35     * @param SearchService $search_service
36     */
37    public function __construct(SearchService $search_service)
38    {
39        $this->search_service = $search_service;
40    }
41
42    /**
43     * Perform the search
44     *
45     * @param Tree   $tree
46     * @param string $query
47     * @param int    $offset
48     * @param int    $limit
49     * @param string $at
50     *
51     * @return Collection<int,array{text:string,value:string}>
52     */
53    protected function search(Tree $tree, string $query, int $offset, int $limit, string $at): Collection
54    {
55        return $this->search_service
56            ->searchPlaces($tree, $query, $offset, $limit)
57            ->map(static function (Place $place): array {
58                return [
59                    'text'  => $place->gedcomName(),
60                    'value' => (string) $place->id(),
61                ];
62            });
63    }
64}
65