Issues (2564)

Http/RequestHandlers/AbstractTomSelectHandler.php (1 issue)

Labels
Severity
1
<?php
2
3
/**
4
 * webtrees: online genealogy
5
 * Copyright (C) 2025 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
18
declare(strict_types=1);
19
20
namespace Fisharebest\Webtrees\Http\RequestHandlers;
21
22
use Fisharebest\Webtrees\Tree;
23
use Fisharebest\Webtrees\Validator;
24
use Illuminate\Support\Collection;
25
use Psr\Http\Message\ResponseInterface;
26
use Psr\Http\Message\ServerRequestInterface;
27
use Psr\Http\Server\RequestHandlerInterface;
28
29
use function response;
30
31
/**
32
 * Autocomplete for TomSelect based controls.
33
 */
34
abstract class AbstractTomSelectHandler implements RequestHandlerInterface
35
{
36
    // For clients that request one page of data at a time.
37
    private const int RESULTS_PER_PAGE = 50;
0 ignored issues
show
A parse error occurred: Syntax error, unexpected T_STRING, expecting '=' on line 37 at column 22
Loading history...
38
39
    /**
40
     * @param ServerRequestInterface $request
41
     *
42
     * @return ResponseInterface
43
     */
44
    public function handle(ServerRequestInterface $request): ResponseInterface
45
    {
46
        $tree  = Validator::attributes($request)->tree();
47
        $at    = Validator::queryParams($request)->isInArray(['', '@'])->string('at');
48
        $page  = Validator::queryParams($request)->integer('page', 1);
49
        $query = Validator::queryParams($request)->string('query');
50
51
        // Fetch one more row than we need, so we can know if more rows exist.
52
        $offset = ($page - 1) * self::RESULTS_PER_PAGE;
53
        $limit  = self::RESULTS_PER_PAGE + 1;
54
55
        // Perform the search.
56
        if ($query !== '') {
57
            $results = $this->search($tree, $query, $offset, $limit, $at ? '@' : '');
58
        } else {
59
            $results = new Collection();
60
        }
61
62
        if ($results->count() > self::RESULTS_PER_PAGE) {
63
            $next_url = route(static::class, ['tree' => $tree->name(), 'at' => $at ? '@' : '', 'page' => $page + 1]);
64
        } else {
65
            $next_url = null;
66
        }
67
68
        return response([
69
            'data'    => $results->slice(0, self::RESULTS_PER_PAGE)->all(),
70
            'nextUrl' => $next_url,
71
        ]);
72
    }
73
74
    /**
75
     * Perform the search
76
     *
77
     * @param Tree   $tree
78
     * @param string $query
79
     * @param int    $offset
80
     * @param int    $limit
81
     * @param string $at    "@" or ""
82
     *
83
     * @return Collection<int,array{text:string,value:string}>
84
     */
85
    abstract protected function search(Tree $tree, string $query, int $offset, int $limit, string $at): Collection;
86
}
87