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

299 lines
10 KiB
PHP
Raw Normal View History

2015-02-27 11:09:23 +01:00
<?php
/**
* Search.php
2017-10-21 08:40:00 +02:00
* Copyright (c) 2017 thegrumpydictator@gmail.com
*
2017-10-21 08:40:00 +02:00
* This file is part of Firefly III.
*
2017-10-21 08:40:00 +02:00
* Firefly III is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Firefly III 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
2017-12-17 14:44:05 +01:00
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
2015-02-27 11:09:23 +01:00
namespace FireflyIII\Support\Search;
2017-10-30 17:28:43 +01:00
use Carbon\Carbon;
2019-05-31 13:35:33 +02:00
use FireflyIII\Helpers\Collector\GroupCollectorInterface;
2019-03-02 14:12:09 +01:00
use FireflyIII\Models\AccountType;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\Repositories\Bill\BillRepositoryInterface;
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
use FireflyIII\Repositories\Category\CategoryRepositoryInterface;
2016-11-06 14:52:31 +01:00
use FireflyIII\User;
2019-02-27 19:08:09 +01:00
use Illuminate\Pagination\LengthAwarePaginator;
2015-03-20 18:21:14 +01:00
use Illuminate\Support\Collection;
2016-11-06 14:52:31 +01:00
use Log;
2015-02-27 11:09:23 +01:00
/**
2017-11-15 12:25:49 +01:00
* Class Search.
2015-02-27 11:09:23 +01:00
*/
class Search implements SearchInterface
{
2019-03-02 14:12:09 +01:00
/** @var AccountRepositoryInterface */
private $accountRepository;
/** @var BillRepositoryInterface */
private $billRepository;
/** @var BudgetRepositoryInterface */
private $budgetRepository;
/** @var CategoryRepositoryInterface */
private $categoryRepository;
2016-11-06 14:52:31 +01:00
/** @var int */
private $limit = 100;
/** @var Collection */
private $modifiers;
2017-11-15 12:25:49 +01:00
/** @var string */
private $originalQuery = '';
2019-03-02 14:12:09 +01:00
/** @var float */
private $startTime;
2016-11-06 14:52:31 +01:00
/** @var User */
private $user;
/** @var array */
2018-04-02 14:50:17 +02:00
private $validModifiers;
2017-11-15 12:25:49 +01:00
/** @var array */
private $words = [];
/**
* Search constructor.
*/
public function __construct()
{
2019-03-02 14:12:09 +01:00
$this->modifiers = new Collection;
$this->validModifiers = (array)config('firefly.search_modifiers');
$this->startTime = microtime(true);
$this->accountRepository = app(AccountRepositoryInterface::class);
$this->categoryRepository = app(CategoryRepositoryInterface::class);
$this->budgetRepository = app(BudgetRepositoryInterface::class);
$this->billRepository = app(BillRepositoryInterface::class);
if ('testing' === config('app.env')) {
2019-06-07 18:20:15 +02:00
Log::warning(sprintf('%s should not be instantiated in the TEST environment!', get_class($this)));
}
2019-02-27 19:08:09 +01:00
}
2019-02-27 19:08:09 +01:00
/**
* @return Collection
*/
public function getModifiers(): Collection
{
return $this->modifiers;
}
/**
* @return string
*/
public function getWordsAsString(): string
{
2018-04-02 14:50:17 +02:00
$string = implode(' ', $this->words);
2018-07-27 05:03:37 +02:00
if ('' === $string) {
2018-04-28 06:23:13 +02:00
return \is_string($this->originalQuery) ? $this->originalQuery : '';
}
2017-03-30 18:43:12 +02:00
return $string;
}
/**
* @return bool
*/
public function hasModifiers(): bool
{
return $this->modifiers->count() > 0;
}
/**
* @param string $query
*/
2018-07-27 05:03:37 +02:00
public function parseQuery(string $query): void
{
$filteredQuery = $query;
$this->originalQuery = $query;
$pattern = '/[a-z_]*:[0-9a-z-.]*/i';
$matches = [];
preg_match_all($pattern, $query, $matches);
foreach ($matches[0] as $match) {
$this->extractModifier($match);
$filteredQuery = str_replace($match, '', $filteredQuery);
}
$filteredQuery = trim(str_replace(['"', "'"], '', $filteredQuery));
2019-02-27 19:08:09 +01:00
if ('' !== $filteredQuery) {
2017-02-19 09:07:14 +01:00
$this->words = array_map('trim', explode(' ', $filteredQuery));
}
}
2016-11-06 14:52:31 +01:00
2019-05-31 13:35:33 +02:00
/**
* @param string $string
*/
private function extractModifier(string $string): void
{
$parts = explode(':', $string);
if (2 === count($parts) && '' !== trim((string)$parts[1]) && '' !== trim((string)$parts[0])) {
$type = trim((string)$parts[0]);
$value = trim((string)$parts[1]);
2019-06-21 19:10:02 +02:00
if (in_array($type, $this->validModifiers, true)) {
2019-05-31 13:35:33 +02:00
// filter for valid type
$this->modifiers->push(['type' => $type, 'value' => $value]);
}
}
}
2019-03-02 14:12:09 +01:00
/**
* @return float
*/
public function searchTime(): float
{
return microtime(true) - $this->startTime;
}
2015-02-27 11:09:23 +01:00
/**
2019-02-27 19:08:09 +01:00
* @return LengthAwarePaginator
2015-02-27 11:09:23 +01:00
*/
2019-02-27 19:08:09 +01:00
public function searchTransactions(): LengthAwarePaginator
2015-02-27 11:09:23 +01:00
{
2017-07-15 17:19:12 +02:00
Log::debug('Start of searchTransactions()');
2019-02-27 19:08:09 +01:00
$pageSize = 50;
$page = 1;
2019-05-31 13:35:33 +02:00
/** @var GroupCollectorInterface $collector */
$collector = app(GroupCollectorInterface::class);
$collector->setLimit($pageSize)->setPage($page)->withAccountInformation();
$collector->withCategoryInformation()->withBudgetInformation();
2019-02-27 19:08:09 +01:00
$collector->setSearchWords($this->words);
2016-11-06 14:52:31 +01:00
2019-02-27 19:08:09 +01:00
// Most modifiers can be applied to the collector directly.
$collector = $this->applyModifiers($collector);
2016-11-06 14:52:31 +01:00
2019-08-02 05:32:30 +02:00
return $collector->getPaginatedGroups();
2015-03-20 18:30:17 +01:00
2016-11-06 14:52:31 +01:00
}
/**
2019-05-31 13:35:33 +02:00
* @param GroupCollectorInterface $collector
2017-12-17 14:30:53 +01:00
*
2019-05-31 13:35:33 +02:00
* @return GroupCollectorInterface
2018-07-28 10:45:16 +02:00
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
2017-12-17 14:30:53 +01:00
*/
2019-05-31 13:35:33 +02:00
private function applyModifiers(GroupCollectorInterface $collector): GroupCollectorInterface
2017-10-30 17:28:43 +01:00
{
2019-02-27 19:08:09 +01:00
/*
* TODO:
* 'bill',
*/
2019-05-31 13:35:33 +02:00
$totalAccounts = new Collection;
2019-02-27 19:08:09 +01:00
2017-10-30 17:28:43 +01:00
foreach ($this->modifiers as $modifier) {
switch ($modifier['type']) {
2019-02-27 19:08:09 +01:00
default:
die(sprintf('unsupported modifier: "%s"', $modifier['type']));
2019-03-02 14:12:09 +01:00
case 'source':
// source can only be asset, liability or revenue account:
$searchTypes = [AccountType::ASSET, AccountType::MORTGAGE, AccountType::LOAN, AccountType::DEBT, AccountType::REVENUE];
$accounts = $this->accountRepository->searchAccount($modifier['value'], $searchTypes);
if ($accounts->count() > 0) {
2019-05-31 13:35:33 +02:00
$totalAccounts = $accounts->merge($totalAccounts);
2019-03-02 14:12:09 +01:00
}
break;
case 'destination':
// source can only be asset, liability or expense account:
$searchTypes = [AccountType::ASSET, AccountType::MORTGAGE, AccountType::LOAN, AccountType::DEBT, AccountType::EXPENSE];
$accounts = $this->accountRepository->searchAccount($modifier['value'], $searchTypes);
if ($accounts->count() > 0) {
2019-05-31 13:35:33 +02:00
$totalAccounts = $accounts->merge($totalAccounts);
2019-03-02 14:12:09 +01:00
}
break;
case 'category':
$result = $this->categoryRepository->searchCategory($modifier['value']);
if ($result->count() > 0) {
$collector->setCategories($result);
}
break;
case 'bill':
$result = $this->billRepository->searchBill($modifier['value']);
if ($result->count() > 0) {
$collector->setBills($result);
}
break;
case 'budget':
$result = $this->budgetRepository->searchBudget($modifier['value']);
if ($result->count() > 0) {
$collector->setBudgets($result);
}
break;
2017-10-30 17:28:43 +01:00
case 'amount_is':
case 'amount':
2018-04-02 14:50:17 +02:00
$amount = app('steam')->positive((string)$modifier['value']);
2017-10-30 17:33:39 +01:00
Log::debug(sprintf('Set "%s" using collector with value "%s"', $modifier['type'], $amount));
2017-10-30 17:28:43 +01:00
$collector->amountIs($amount);
break;
2017-10-30 17:34:56 +01:00
case 'amount_max':
2017-10-30 17:28:43 +01:00
case 'amount_less':
2018-04-02 14:50:17 +02:00
$amount = app('steam')->positive((string)$modifier['value']);
2017-10-30 17:33:39 +01:00
Log::debug(sprintf('Set "%s" using collector with value "%s"', $modifier['type'], $amount));
2017-10-30 17:28:43 +01:00
$collector->amountLess($amount);
break;
2017-10-30 17:34:56 +01:00
case 'amount_min':
2017-10-30 17:28:43 +01:00
case 'amount_more':
2018-04-02 14:50:17 +02:00
$amount = app('steam')->positive((string)$modifier['value']);
2017-10-30 17:33:39 +01:00
Log::debug(sprintf('Set "%s" using collector with value "%s"', $modifier['type'], $amount));
2017-10-30 17:28:43 +01:00
$collector->amountMore($amount);
break;
case 'type':
$collector->setTypes([ucfirst($modifier['value'])]);
2017-10-30 17:33:39 +01:00
Log::debug(sprintf('Set "%s" using collector with value "%s"', $modifier['type'], $modifier['value']));
2017-10-30 17:28:43 +01:00
break;
case 'date':
case 'on':
2017-10-30 17:33:39 +01:00
Log::debug(sprintf('Set "%s" using collector with value "%s"', $modifier['type'], $modifier['value']));
2017-10-30 17:28:43 +01:00
$start = new Carbon($modifier['value']);
$collector->setRange($start, $start);
break;
case 'date_before':
case 'before':
2017-10-30 17:33:39 +01:00
Log::debug(sprintf('Set "%s" using collector with value "%s"', $modifier['type'], $modifier['value']));
2017-10-30 17:28:43 +01:00
$before = new Carbon($modifier['value']);
$collector->setBefore($before);
break;
case 'date_after':
case 'after':
2017-10-30 17:33:39 +01:00
Log::debug(sprintf('Set "%s" using collector with value "%s"', $modifier['type'], $modifier['value']));
2017-10-30 17:28:43 +01:00
$after = new Carbon($modifier['value']);
2018-11-21 06:13:30 +01:00
$collector->setAfter($after);
2017-10-30 17:28:43 +01:00
break;
}
}
2019-05-31 13:35:33 +02:00
$collector->setAccounts($totalAccounts);
2017-10-30 17:28:43 +01:00
return $collector;
}
/**
2019-05-31 13:35:33 +02:00
* @param int $limit
*/
2019-05-31 13:35:33 +02:00
public function setLimit(int $limit): void
{
2019-05-31 13:35:33 +02:00
$this->limit = $limit;
}
/**
* @param User $user
*/
public function setUser(User $user): void
{
$this->user = $user;
$this->accountRepository->setUser($user);
$this->billRepository->setUser($user);
$this->categoryRepository->setUser($user);
$this->budgetRepository->setUser($user);
}
2017-02-05 16:16:15 +01:00
}