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

198 lines
6.4 KiB
PHP
Raw Normal View History

<?php
/**
* Modifier.php
* Copyright (c) 2017 thegrumpydictator@gmail.com
*
2017-10-21 08:40:00 +02:00
* This file is part of Firefly III.
*
* 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
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace FireflyIII\Support\Search;
2017-02-19 09:07:14 +01:00
use Carbon\Carbon;
use Exception;
2017-02-19 07:38:51 +01:00
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Models\Transaction;
use Log;
use Steam;
class Modifier
{
/**
* @param Transaction $transaction
* @param string $amount
* @param int $expected
*
* @return bool
*/
public static function amountCompare(Transaction $transaction, string $amount, int $expected): bool
{
$amount = Steam::positive($amount);
$transactionAmount = Steam::positive($transaction->transaction_amount);
$compare = bccomp($amount, $transactionAmount);
Log::debug(sprintf('%s vs %s is %d', $amount, $transactionAmount, $compare));
return $compare === $expected;
}
2017-10-22 20:13:02 +02:00
/**
* @param array $modifier
* @param Transaction $transaction
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*
* @return bool
2017-11-15 12:25:49 +01:00
*
2017-10-22 20:13:02 +02:00
* @throws FireflyException
*/
2017-02-19 07:38:51 +01:00
public static function apply(array $modifier, Transaction $transaction): bool
{
2017-10-30 17:39:25 +01:00
$res = true;
2017-02-19 07:38:51 +01:00
switch ($modifier['type']) {
case 'source':
2017-10-30 17:39:25 +01:00
$name = Steam::tryDecrypt($transaction->account_name);
$res = self::stringCompare($name, $modifier['value']);
2017-02-19 07:38:51 +01:00
Log::debug(sprintf('Source is %s? %s', $modifier['value'], var_export($res, true)));
break;
case 'destination':
2017-10-30 17:39:25 +01:00
$name = Steam::tryDecrypt($transaction->opposing_account_name);
$res = self::stringCompare($name, $modifier['value']);
2017-02-19 07:38:51 +01:00
Log::debug(sprintf('Destination is %s? %s', $modifier['value'], var_export($res, true)));
break;
case 'category':
2017-11-05 19:49:20 +01:00
$res = self::category($transaction, $modifier['value']);
2017-02-19 07:38:51 +01:00
Log::debug(sprintf('Category is %s? %s', $modifier['value'], var_export($res, true)));
break;
2017-02-19 07:41:12 +01:00
case 'budget':
2017-11-05 19:49:20 +01:00
$res = self::budget($transaction, $modifier['value']);
2017-02-19 07:41:12 +01:00
Log::debug(sprintf('Budget is %s? %s', $modifier['value'], var_export($res, true)));
break;
2017-02-19 09:07:14 +01:00
case 'bill':
2017-10-30 17:39:25 +01:00
$name = Steam::tryDecrypt($transaction->bill_name);
2017-11-05 19:49:20 +01:00
$res = self::stringCompare($name, $modifier['value']);
2017-02-19 09:07:14 +01:00
Log::debug(sprintf('Bill is %s? %s', $modifier['value'], var_export($res, true)));
break;
2017-02-19 07:38:51 +01:00
}
2017-02-19 07:38:51 +01:00
return $res;
}
2017-02-19 09:07:14 +01:00
/**
* @param Carbon $date
* @param string $compare
*
* @return bool
*/
public static function dateAfter(Carbon $date, string $compare): bool
{
try {
$compareDate = new Carbon($compare);
} catch (Exception $e) {
return false;
}
return $date->greaterThanOrEqualTo($compareDate);
}
/**
* @param Carbon $date
* @param string $compare
*
* @return bool
*/
public static function dateBefore(Carbon $date, string $compare): bool
{
try {
$compareDate = new Carbon($compare);
} catch (Exception $e) {
return false;
}
return $date->lessThanOrEqualTo($compareDate);
}
/**
* @param Carbon $date
* @param string $compare
*
* @return bool
*/
public static function sameDate(Carbon $date, string $compare): bool
{
try {
$compareDate = new Carbon($compare);
} catch (Exception $e) {
return false;
}
return $compareDate->isSameDay($date);
}
/**
2017-02-19 07:38:51 +01:00
* @param string $haystack
* @param string $needle
*
* @return bool
*/
2017-02-19 07:38:51 +01:00
public static function stringCompare(string $haystack, string $needle): bool
{
2017-11-15 12:25:49 +01:00
$res = !(false === strpos(strtolower($haystack), strtolower($needle)));
2017-02-19 07:38:51 +01:00
Log::debug(sprintf('"%s" is in "%s"? %s', $needle, $haystack, var_export($res, true)));
return $res;
}
2017-02-19 07:41:12 +01:00
/**
* @param Transaction $transaction
* @param string $search
*
* @return bool
*/
private static function budget(Transaction $transaction, string $search): bool
{
$journalBudget = '';
2017-11-15 12:25:49 +01:00
if (null !== $transaction->transaction_journal_budget_name) {
2017-02-19 07:41:12 +01:00
$journalBudget = Steam::decrypt(intval($transaction->transaction_journal_budget_encrypted), $transaction->transaction_journal_budget_name);
}
$transactionBudget = '';
2017-11-15 12:25:49 +01:00
if (null !== $transaction->transaction_budget_name) {
2017-02-19 07:41:12 +01:00
$journalBudget = Steam::decrypt(intval($transaction->transaction_budget_encrypted), $transaction->transaction_budget_name);
}
return self::stringCompare($journalBudget, $search) || self::stringCompare($transactionBudget, $search);
}
/**
2017-02-19 07:38:51 +01:00
* @param Transaction $transaction
* @param string $search
*
* @return bool
*/
2017-02-19 07:38:51 +01:00
private static function category(Transaction $transaction, string $search): bool
{
2017-02-19 07:38:51 +01:00
$journalCategory = '';
2017-11-15 12:25:49 +01:00
if (null !== $transaction->transaction_journal_category_name) {
2017-02-19 07:38:51 +01:00
$journalCategory = Steam::decrypt(intval($transaction->transaction_journal_category_encrypted), $transaction->transaction_journal_category_name);
}
$transactionCategory = '';
2017-11-15 12:25:49 +01:00
if (null !== $transaction->transaction_category_name) {
2017-02-19 07:38:51 +01:00
$journalCategory = Steam::decrypt(intval($transaction->transaction_category_encrypted), $transaction->transaction_category_name);
}
2017-02-19 07:38:51 +01:00
return self::stringCompare($journalCategory, $search) || self::stringCompare($transactionCategory, $search);
}
2017-07-07 08:09:42 +02:00
}