Files
firefly-iii/app/Support/Search/AccountSearch.php

145 lines
4.3 KiB
PHP
Raw Normal View History

2019-10-02 18:00:01 +02:00
<?php
2020-06-30 19:05:35 +02:00
2019-10-02 18:00:01 +02:00
/**
* AccountSearch.php
2020-06-30 19:05:35 +02:00
* Copyright (c) 2020 james@firefly-iii.org
2019-10-02 18:00:01 +02:00
*
* This file is part of Firefly III (https://github.com/firefly-iii).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
2020-06-30 19:05:35 +02:00
declare(strict_types=1);
2019-10-02 18:00:01 +02:00
namespace FireflyIII\Support\Search;
2019-10-02 18:00:01 +02:00
use FireflyIII\User;
2023-06-04 06:30:22 +02:00
use Illuminate\Contracts\Auth\Authenticatable;
2019-10-02 18:00:01 +02:00
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Collection;
/**
* Class AccountSearch
*/
class AccountSearch implements GenericSearchInterface
{
/** @var string */
public const string SEARCH_ALL = 'all';
2023-12-20 19:35:52 +01:00
2019-10-02 18:00:01 +02:00
/** @var string */
public const string SEARCH_IBAN = 'iban';
2023-12-20 19:35:52 +01:00
2019-10-02 18:00:01 +02:00
/** @var string */
public const string SEARCH_ID = 'id';
2023-12-20 19:35:52 +01:00
2022-03-29 14:59:58 +02:00
/** @var string */
public const string SEARCH_NAME = 'name';
2023-12-20 19:35:52 +01:00
2022-03-29 14:59:58 +02:00
/** @var string */
2023-12-02 12:56:48 +01:00
public const string SEARCH_NUMBER = 'number';
2020-10-24 18:40:17 +02:00
private string $field;
private string $query;
private array $types;
private User $user;
2019-10-02 18:00:01 +02:00
public function __construct()
{
$this->types = [];
}
public function search(): Collection
{
2022-03-29 14:59:58 +02:00
$searchQuery = $this->user->accounts()
2023-12-20 19:35:52 +01:00
->leftJoin('account_types', 'accounts.account_type_id', '=', 'account_types.id')
->leftJoin('account_meta', 'accounts.id', '=', 'account_meta.account_id')
->whereIn('account_types.type', $this->types)
;
2022-03-29 14:59:58 +02:00
$like = sprintf('%%%s%%', $this->query);
2019-10-02 18:00:01 +02:00
$originalQuery = $this->query;
2023-12-20 19:35:52 +01:00
2019-10-02 18:00:01 +02:00
switch ($this->field) {
2020-10-03 17:53:23 +02:00
default:
2019-10-02 18:00:01 +02:00
case self::SEARCH_ALL:
2020-10-24 18:40:17 +02:00
$searchQuery->where(
2023-12-21 05:07:26 +01:00
static function (Builder $q) use ($like): void { // @phpstan-ignore-line
$q->whereLike('accounts.id', $like);
$q->orWhereLike('accounts.name', $like);
$q->orWhereLike('accounts.iban', $like);
2019-10-02 18:00:01 +02:00
}
);
// meta data:
2020-10-24 18:40:17 +02:00
$searchQuery->orWhere(
2023-12-21 05:07:26 +01:00
static function (Builder $q) use ($originalQuery): void { // @phpstan-ignore-line
2019-10-02 18:00:01 +02:00
$json = json_encode($originalQuery, JSON_THROW_ON_ERROR);
2020-02-22 11:05:16 +01:00
$q->where('account_meta.name', '=', 'account_number');
$q->whereLike('account_meta.data', $json);
2019-10-02 18:00:01 +02:00
}
);
2023-12-20 19:35:52 +01:00
2019-10-02 18:00:01 +02:00
break;
2023-12-20 19:35:52 +01:00
2019-10-02 18:00:01 +02:00
case self::SEARCH_ID:
2022-12-29 19:42:26 +01:00
$searchQuery->where('accounts.id', '=', (int)$originalQuery);
2023-12-20 19:35:52 +01:00
2019-10-02 18:00:01 +02:00
break;
2023-12-20 19:35:52 +01:00
2019-10-02 18:00:01 +02:00
case self::SEARCH_NAME:
$searchQuery->whereLike('accounts.name', $like);
2023-12-20 19:35:52 +01:00
2019-10-02 18:00:01 +02:00
break;
2023-12-20 19:35:52 +01:00
2019-10-02 18:00:01 +02:00
case self::SEARCH_IBAN:
$searchQuery->whereLike('accounts.iban', $like);
2023-12-20 19:35:52 +01:00
2019-10-02 18:00:01 +02:00
break;
2023-12-20 19:35:52 +01:00
2019-10-02 18:00:01 +02:00
case self::SEARCH_NUMBER:
// meta data:
2020-10-24 18:40:17 +02:00
$searchQuery->Where(
2023-12-21 05:07:26 +01:00
static function (Builder $q) use ($originalQuery): void { // @phpstan-ignore-line
2019-10-02 18:00:01 +02:00
$json = json_encode($originalQuery, JSON_THROW_ON_ERROR);
$q->where('account_meta.name', 'account_number');
$q->where('account_meta.data', $json);
}
);
2023-12-20 19:35:52 +01:00
2019-10-02 18:00:01 +02:00
break;
}
2020-10-24 18:40:17 +02:00
return $searchQuery->distinct()->get(['accounts.*']);
2019-10-02 18:00:01 +02:00
}
public function setField(string $field): void
{
$this->field = $field;
}
public function setQuery(string $query): void
{
$this->query = $query;
}
public function setTypes(array $types): void
{
$this->types = $types;
}
2023-12-20 19:35:52 +01:00
public function setUser(null|Authenticatable|User $user): void
2019-10-02 18:00:01 +02:00
{
2023-10-30 19:49:40 +01:00
if ($user instanceof User) {
2023-06-04 06:30:22 +02:00
$this->user = $user;
}
2019-10-02 18:00:01 +02:00
}
}