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