More working triggers.

This commit is contained in:
James Cole 2016-01-13 14:12:46 +01:00
parent 8720511046
commit afec8480fb
7 changed files with 325 additions and 5 deletions

View File

@ -0,0 +1,64 @@
<?php
/**
* ToAccountContains.php
* Copyright (C) 2016 Sander Dorigo
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
namespace FireflyIII\Rules\Triggers;
use FireflyIII\Models\RuleTrigger;
use FireflyIII\Models\TransactionJournal;
use Log;
/**
* Class ToAccountContains
*
* @package FireflyIII\Rules\Triggers
*/
class ToAccountContains implements TriggerInterface
{
/** @var RuleTrigger */
protected $trigger;
/** @var TransactionJournal */
protected $journal;
/**
* TriggerInterface constructor.
*
* @param RuleTrigger $trigger
* @param TransactionJournal $journal
*/
public function __construct(RuleTrigger $trigger, TransactionJournal $journal)
{
$this->trigger = $trigger;
$this->journal = $journal;
}
/**
* @return bool
*/
public function triggered()
{
$toAccountName = strtolower($this->journal->destination_account->name);
$search = strtolower($this->trigger->trigger_value);
$strpos = strpos($toAccountName, $search);
if (!($strpos === false)) {
// found something
Log::debug('"' . $toAccountName . '" contains the text "' . $search . '". Return true.');
return true;
}
// found nothing.
Log::debug('"' . $toAccountName . '" does not contain the text "' . $search . '". Return false.');
return false;
}
}

View File

@ -0,0 +1,74 @@
<?php
/**
* ToAccountEnds.php
* Copyright (C) 2016 Sander Dorigo
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
namespace FireflyIII\Rules\Triggers;
use FireflyIII\Models\RuleTrigger;
use FireflyIII\Models\TransactionJournal;
use Log;
/**
* Class ToAccountEnds
*
* @package FireflyIII\Rules\Triggers
*/
class Ends implements TriggerInterface
{
/** @var RuleTrigger */
protected $trigger;
/** @var TransactionJournal */
protected $journal;
/**
* TriggerInterface constructor.
*
* @param RuleTrigger $trigger
* @param TransactionJournal $journal
*/
public function __construct(RuleTrigger $trigger, TransactionJournal $journal)
{
$this->trigger = $trigger;
$this->journal = $journal;
}
/**
* @return bool
*/
public function triggered()
{
$toAccountName = strtolower($this->journal->destination_account->name);
$toAccountNameLength = strlen($toAccountName);
$search = strtolower($this->trigger->trigger_value);
$searchLength = strlen($search);
// if the string to search for is longer than the account name,
// shorten the search string.
if ($searchLength > $toAccountNameLength) {
Log::debug('Search string "' . $search . '" (' . $searchLength . ') is longer than "' . $toAccountName . '" (' . $toAccountNameLength . '). ');
$search = substr($search, ($toAccountNameLength * -1));
$searchLength = strlen($search);
Log::debug('Search string is now "' . $search . '" (' . $searchLength . ') instead.');
}
$part = substr($toAccountName, $searchLength * -1);
if ($part == $search) {
Log::debug('"' . $toAccountName . '" ends with "' . $search . '". Return true.');
return true;
}
Log::debug('"' . $toAccountName . '" does not end with "' . $search . '". Return false.');
return false;
}
}

View File

@ -0,0 +1,60 @@
<?php
/**
* ToAccountIs.php
* Copyright (C) 2016 Sander Dorigo
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
namespace FireflyIII\Rules\Triggers;
use FireflyIII\Models\RuleTrigger;
use FireflyIII\Models\TransactionJournal;
use Log;
/**
* Class ToAccountIs
*
* @package FireflyIII\Rules\Triggers
*/
class ToAccountIs implements TriggerInterface
{
/** @var RuleTrigger */
protected $trigger;
/** @var TransactionJournal */
protected $journal;
/**
* TriggerInterface constructor.
*
* @param RuleTrigger $trigger
* @param TransactionJournal $journal
*/
public function __construct(RuleTrigger $trigger, TransactionJournal $journal)
{
$this->trigger = $trigger;
$this->journal = $journal;
}
/**
* @return bool
*/
public function triggered()
{
$toAccountName = strtolower($this->journal->destination_account->name);
$search = strtolower($this->trigger->trigger_value);
if ($toAccountName == $search) {
Log::debug('"' . $toAccountName . '" equals "' . $search . '" exactly. Return true.');
return true;
}
Log::debug('"' . $toAccountName . '" does not equal "' . $search . '". Return false.');
return false;
}
}

View File

@ -0,0 +1,62 @@
<?php
/**
* ToAccountStarts.php
* Copyright (C) 2016 Sander Dorigo
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
namespace FireflyIII\Rules\Triggers;
use FireflyIII\Models\RuleTrigger;
use FireflyIII\Models\TransactionJournal;
use Log;
/**
* Class ToAccountStarts
*
* @package FireflyIII\Rules\Triggers
*/
class ToAccountStarts implements TriggerInterface
{
/** @var RuleTrigger */
protected $trigger;
/** @var TransactionJournal */
protected $journal;
/**
* TriggerInterface constructor.
*
* @param RuleTrigger $trigger
* @param TransactionJournal $journal
*/
public function __construct(RuleTrigger $trigger, TransactionJournal $journal)
{
$this->trigger = $trigger;
$this->journal = $journal;
}
/**
* @return bool
*/
public function triggered()
{
$toAccountName = strtolower($this->journal->destination_account->name);
$search = strtolower($this->trigger->trigger_value);
$part = substr($toAccountName, 0, strlen($search));
if ($part == $search) {
Log::debug('"' . $toAccountName . '" starts with "' . $search . '". Return true.');
return true;
}
Log::debug('"' . $toAccountName . '" does not start with "' . $search . '". Return false.');
return false;
}
}

View File

@ -0,0 +1,59 @@
<?php
/**
* TransactionType.php
* Copyright (C) 2016 Sander Dorigo
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
namespace FireflyIII\Rules\Triggers;
use FireflyIII\Models\RuleTrigger;
use FireflyIII\Models\TransactionJournal;
use Log;
/**
* Class TransactionType
*
* @package FireflyIII\Rules\Triggers
*/
class TransactionType implements TriggerInterface
{
/** @var RuleTrigger */
protected $trigger;
/** @var TransactionJournal */
protected $journal;
/**
* TriggerInterface constructor.
*
* @param RuleTrigger $trigger
* @param TransactionJournal $journal
*/
public function __construct(RuleTrigger $trigger, TransactionJournal $journal)
{
$this->trigger = $trigger;
$this->journal = $journal;
}
/**
* @return bool
*/
public function triggered()
{
$type = strtolower($this->journal->transactionType->type);
$search = strtolower($this->trigger->trigger_value);
if ($type == $search) {
Log::debug('Journal is of type "' . $type . '" which matches with "' . $search . '". Return true');
return true;
}
Log::debug('Journal is of type "' . $type . '" which does not match with "' . $search . '". Return false');
return false;
}
}

View File

@ -48,6 +48,7 @@ class UserAction implements TriggerInterface
public function triggered() public function triggered()
{ {
Log::debug('user_action always returns true.'); Log::debug('user_action always returns true.');
return true; return true;
} }

View File

@ -174,11 +174,11 @@ return [
'from_account_ends' => 'FireflyIII\Rules\Triggers\FromAccountEnds', 'from_account_ends' => 'FireflyIII\Rules\Triggers\FromAccountEnds',
'from_account_is' => 'FireflyIII\Rules\Triggers\FromAccountIs', 'from_account_is' => 'FireflyIII\Rules\Triggers\FromAccountIs',
'from_account_contains' => 'FireflyIII\Rules\Triggers\FromAccountContains', 'from_account_contains' => 'FireflyIII\Rules\Triggers\FromAccountContains',
'to_account_starts' => 'FireflyIII\Rules\Triggers', 'to_account_starts' => 'FireflyIII\Rules\Triggers\ToAccountStarts',
'to_account_ends' => 'FireflyIII\Rules\Triggers', 'to_account_ends' => 'FireflyIII\Rules\Triggers\ToAccountEnds',
'to_account_is' => 'FireflyIII\Rules\Triggers', 'to_account_is' => 'FireflyIII\Rules\Triggers\ToAccountIs',
'to_account_contains' => 'FireflyIII\Rules\Triggers', 'to_account_contains' => 'FireflyIII\Rules\Triggers\ToAccountContains',
'transaction_type' => 'FireflyIII\Rules\Triggers', 'transaction_type' => 'FireflyIII\Rules\Triggers\TransactionType',
'amount_less' => 'FireflyIII\Rules\Triggers', 'amount_less' => 'FireflyIII\Rules\Triggers',
'amount_exactly' => 'FireflyIII\Rules\Triggers', 'amount_exactly' => 'FireflyIII\Rules\Triggers',
'amount_exactly_not' => 'FireflyIII\Rules\Triggers', 'amount_exactly_not' => 'FireflyIII\Rules\Triggers',