1 | <?php |
||
2 | /** |
||
3 | * FromAccountContains.php |
||
4 | * Copyright (c) 2017 [email protected] |
||
5 | * |
||
6 | * This file is part of Firefly III. |
||
7 | * |
||
8 | * Firefly III is free software: you can redistribute it and/or modify |
||
9 | * it under the terms of the GNU General Public License as published by |
||
10 | * the Free Software Foundation, either version 3 of the License, or |
||
11 | * (at your option) any later version. |
||
12 | * |
||
13 | * Firefly III is distributed in the hope that it will be useful, |
||
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
16 | * GNU General Public License for more details. |
||
17 | * |
||
18 | * You should have received a copy of the GNU General Public License |
||
19 | * along with Firefly III. If not, see <http://www.gnu.org/licenses/>. |
||
20 | */ |
||
21 | declare(strict_types=1); |
||
22 | |||
23 | namespace FireflyIII\TransactionRules\Triggers; |
||
24 | |||
25 | use FireflyIII\Models\Account; |
||
26 | use FireflyIII\Models\TransactionJournal; |
||
27 | use FireflyIII\Repositories\Journal\JournalRepositoryInterface; |
||
28 | use Log; |
||
29 | |||
30 | /** |
||
31 | * Class FromAccountContains. |
||
32 | */ |
||
33 | final class FromAccountContains extends AbstractTrigger implements TriggerInterface |
||
34 | { |
||
35 | /** |
||
36 | * A trigger is said to "match anything", or match any given transaction, |
||
37 | * when the trigger value is very vague or has no restrictions. Easy examples |
||
38 | * are the "AmountMore"-trigger combined with an amount of 0: any given transaction |
||
39 | * has an amount of more than zero! Other examples are all the "Description"-triggers |
||
40 | * which have hard time handling empty trigger values such as "" or "*" (wild cards). |
||
41 | * |
||
42 | * If the user tries to create such a trigger, this method MUST return true so Firefly III |
||
43 | * can stop the storing / updating the trigger. If the trigger is in any way restrictive |
||
44 | * (even if it will still include 99.9% of the users transactions), this method MUST return |
||
45 | * false. |
||
46 | * |
||
47 | * @param mixed $value |
||
48 | * |
||
49 | * @return bool |
||
50 | */ |
||
51 | public static function willMatchEverything($value = null): bool |
||
52 | { |
||
53 | if (null !== $value) { |
||
54 | $res = '' === (string)$value; |
||
55 | if (true === $res) { |
||
56 | Log::error(sprintf('Cannot use %s with "" as a value.', self::class)); |
||
57 | } |
||
58 | |||
59 | return $res; |
||
60 | } |
||
61 | Log::error(sprintf('Cannot use %s with a null value.', self::class)); |
||
62 | |||
63 | return true; |
||
64 | } |
||
65 | |||
66 | /** |
||
67 | * Returns true when from-account contains X |
||
68 | * |
||
69 | * @param TransactionJournal $journal |
||
70 | * |
||
71 | * @return bool |
||
72 | */ |
||
73 | public function triggered(TransactionJournal $journal): bool |
||
74 | { |
||
75 | $fromAccountName = ''; |
||
76 | |||
77 | /** @var JournalRepositoryInterface $repository */ |
||
78 | $repository = app(JournalRepositoryInterface::class); |
||
79 | |||
80 | /** @var Account $account */ |
||
81 | foreach ($repository->getJournalSourceAccounts($journal, false) as $account) { |
||
0 ignored issues
–
show
Deprecated Code
introduced
by
Loading history...
|
|||
82 | $fromAccountName .= strtolower($account->name); |
||
83 | } |
||
84 | |||
85 | $search = strtolower($this->triggerValue); |
||
86 | $strpos = strpos($fromAccountName, $search); |
||
87 | |||
88 | if (!(false === $strpos)) { |
||
89 | Log::debug(sprintf('RuleTrigger FromAccountContains for journal #%d: "%s" contains "%s", return true.', $journal->id, $fromAccountName, $search)); |
||
90 | |||
91 | return true; |
||
92 | } |
||
93 | |||
94 | Log::debug( |
||
95 | sprintf( |
||
96 | 'RuleTrigger FromAccountContains for journal #%d: "%s" does not contain "%s", return false.', |
||
97 | $journal->id, |
||
98 | $fromAccountName, |
||
99 | $search |
||
100 | ) |
||
101 | ); |
||
102 | |||
103 | return false; |
||
104 | } |
||
105 | } |
||
106 |