1<?php
2
3// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the GNU Affero General Public License v3.0.
4// See the LICENCE file in the repository root for full licence text.
5
6namespace App\Libraries\Search;
7
8use App\Libraries\Elasticsearch\Sort;
9
10class UserSearchRequestParams extends UserSearchParams
11{
12 public function __construct(array $request)
13 {
14 parent::__construct();
15
16 $this->queryString = presence(trim(get_string($request['query'] ?? null) ?? ''));
17 $this->page = get_int($request['page'] ?? null);
18 $this->from = $this->pageAsFrom($this->page);
19 $this->recentOnly = get_bool($request['recent_only'] ?? null);
20 $this->parseSort(get_string($request['sort'] ?? null));
21 }
22
23 public function isLoginRequired(): bool
24 {
25 return true;
26 }
27
28 private function parseSort(?string $sortStr): void
29 {
30 $sortStr = $sortStr ?? '';
31
32 $options = explode('_', $sortStr);
33 $field = $options[0];
34 $order = $options[1] ?? null;
35
36 if (!in_array($field, static::VALID_SORT_FIELDS, true)) {
37 $field = static::DEFAULT_SORT_FIELD;
38 }
39
40 if (!in_array($order, ['asc', 'desc'], true)) {
41 $order = static::defaultSortOrder($field);
42 }
43
44 switch ($field) {
45 case 'username':
46 $this->sorts[] = new Sort('username.raw', $order);
47 break;
48 default:
49 $this->sorts[] = new Sort('_score', $order);
50 $this->sorts[] = new Sort('username.raw', $order === 'desc' ? 'asc' : 'desc');
51 }
52
53 $this->sortField = $field;
54 $this->sortOrder = $order;
55 }
56}