1 | <?php |
||||
2 | declare(strict_types=1); |
||||
3 | /** |
||||
4 | * AccountController.php |
||||
5 | * Copyright (c) 2019 [email protected] |
||||
6 | * |
||||
7 | * This file is part of Firefly III (https://github.com/firefly-iii). |
||||
8 | * |
||||
9 | * This program is free software: you can redistribute it and/or modify |
||||
10 | * it under the terms of the GNU Affero General Public License as |
||||
11 | * published by the Free Software Foundation, either version 3 of the |
||||
12 | * License, or (at your option) any later version. |
||||
13 | * |
||||
14 | * This program is distributed in the hope that it will be useful, |
||||
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||||
17 | * GNU Affero General Public License for more details. |
||||
18 | * |
||||
19 | * You should have received a copy of the GNU Affero General Public License |
||||
20 | * along with this program. If not, see <https://www.gnu.org/licenses/>. |
||||
21 | */ |
||||
22 | |||||
23 | namespace FireflyIII\Api\V1\Controllers\Search; |
||||
24 | |||||
25 | use FireflyIII\Api\V1\Controllers\Controller; |
||||
26 | use FireflyIII\Support\Http\Api\AccountFilter; |
||||
27 | use FireflyIII\Support\Search\AccountSearch; |
||||
28 | use FireflyIII\Transformers\AccountTransformer; |
||||
29 | use Illuminate\Http\JsonResponse; |
||||
30 | use Illuminate\Http\Request; |
||||
31 | use Illuminate\Http\Response; |
||||
32 | use Illuminate\Pagination\LengthAwarePaginator; |
||||
33 | use League\Fractal\Pagination\IlluminatePaginatorAdapter; |
||||
34 | use League\Fractal\Resource\Collection as FractalCollection; |
||||
35 | use Log; |
||||
36 | |||||
37 | /** |
||||
38 | * Class AccountController |
||||
39 | */ |
||||
40 | class AccountController extends Controller |
||||
41 | { |
||||
42 | use AccountFilter; |
||||
43 | |||||
44 | /** @var array */ |
||||
45 | private $validFields; |
||||
46 | |||||
47 | public function __construct() |
||||
48 | { |
||||
49 | parent::__construct(); |
||||
50 | $this->validFields = [ |
||||
51 | AccountSearch::SEARCH_ALL, |
||||
52 | AccountSearch::SEARCH_ID, |
||||
53 | AccountSearch::SEARCH_NAME, |
||||
54 | AccountSearch::SEARCH_IBAN, |
||||
55 | AccountSearch::SEARCH_NUMBER, |
||||
56 | ]; |
||||
57 | } |
||||
58 | |||||
59 | /** |
||||
60 | * @param Request $request |
||||
61 | * |
||||
62 | * @return JsonResponse|Response |
||||
63 | */ |
||||
64 | public function search(Request $request) |
||||
65 | { |
||||
66 | Log::debug('Now in account search()'); |
||||
67 | $manager = $this->getManager(); |
||||
68 | $query = $request->get('query'); |
||||
69 | $field = $request->get('field'); |
||||
70 | $type = $request->get('type') ?? 'all'; |
||||
71 | if ('' === $query || !in_array($field, $this->validFields, true)) { |
||||
72 | return response(null, 422); |
||||
73 | } |
||||
74 | $types = $this->mapAccountTypes($type); |
||||
75 | Log::debug(sprintf('Going to search for "%s" in types', $query), $types); |
||||
76 | |||||
77 | /** @var AccountSearch $search */ |
||||
78 | $search = app(AccountSearch::class); |
||||
79 | $search->setUser(auth()->user()); |
||||
80 | $search->setTypes($types); |
||||
81 | $search->setField($field); |
||||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||
82 | $search->setQuery($query); |
||||
0 ignored issues
–
show
It seems like
$query can also be of type null ; however, parameter $query of FireflyIII\Support\Searc...countSearch::setQuery() does only seem to accept string , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
83 | |||||
84 | $accounts = $search->search(); |
||||
85 | |||||
86 | Log::debug(sprintf('Found %d accounts', $accounts->count())); |
||||
87 | |||||
88 | /** @var AccountTransformer $transformer */ |
||||
89 | $transformer = app(AccountTransformer::class); |
||||
90 | $transformer->setParameters($this->parameters); |
||||
91 | $count = $accounts->count(); |
||||
92 | $perPage = 0 === $count ? 1 : $count; |
||||
93 | $paginator = new LengthAwarePaginator($accounts, $count, $perPage, 1); |
||||
94 | |||||
95 | $resource = new FractalCollection($accounts, $transformer, 'accounts'); |
||||
96 | $resource->setPaginator(new IlluminatePaginatorAdapter($paginator)); |
||||
97 | |||||
98 | return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json'); |
||||
99 | } |
||||
100 | } |
||||
101 |