More valid triggers.

This commit is contained in:
James Cole 2016-01-13 14:37:19 +01:00
parent afec8480fb
commit 1270e5d15c
7 changed files with 333 additions and 6 deletions

View File

@ -0,0 +1,64 @@
<?php
/**
* AmountExactly.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 AmountExactly
*
* @package FireflyIII\Rules\Triggers
*/
class AmountExactly 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()
{
$amount = $this->journal->amount_positive;
$compare = $this->trigger->trigger_value;
$result = bccomp($amount, $compare, 4);
if ($result === 0) {
// found something
Log::debug($amount . ' is exactly ' . $compare . '. Return true.');
return true;
}
// found nothing.
Log::debug($amount . ' is not exactly ' . $compare . '. Return false.');
return false;
}
}

View File

@ -0,0 +1,64 @@
<?php
/**
* AmountLess.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 AmountLess
*
* @package FireflyIII\Rules\Triggers
*/
class AmountLess 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()
{
$amount = $this->journal->amount_positive;
$compare = $this->trigger->trigger_value;
$result = bccomp($amount, $compare, 4);
if ($result === -1) {
// found something
Log::debug($amount . ' is less than ' . $compare . '. Return true.');
return true;
}
// found nothing.
Log::debug($amount . ' is not less than ' . $compare . '. Return false.');
return false;
}
}

View File

@ -0,0 +1,64 @@
<?php
/**
* AmountMore.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 AmountMore
*
* @package FireflyIII\Rules\Triggers
*/
class AmountMore 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()
{
$amount = $this->journal->amount_positive;
$compare = $this->trigger->trigger_value;
$result = bccomp($amount, $compare, 4);
if ($result === 1) {
// found something
Log::debug($amount . ' is more than ' . $compare . '. Return true.');
return true;
}
// found nothing.
Log::debug($amount . ' is not more than ' . $compare . '. Return false.');
return false;
}
}

View File

@ -0,0 +1,74 @@
<?php
/**
* DescriptionEnds.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 DescriptionEnds
*
* @package FireflyIII\Rules\Triggers
*/
class DescriptionEnds 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()
{
$description = strtolower($this->journal->description);
$descriptionLength = strlen($description);
$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 > $descriptionLength) {
Log::debug('Search string "' . $search . '" (' . $searchLength . ') is longer than "' . $description . '" (' . $descriptionLength . '). ');
$search = substr($search, ($descriptionLength * -1));
$searchLength = strlen($search);
Log::debug('Search string is now "' . $search . '" (' . $searchLength . ') instead.');
}
$part = substr($description, $searchLength * -1);
if ($part == $search) {
Log::debug('"' . $description . '" ends with "' . $search . '". Return true.');
return true;
}
Log::debug('"' . $description . '" does not end with "' . $search . '". Return false.');
return false;
}
}

View File

@ -0,0 +1,62 @@
<?php
/**
* DescriptionStarts.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 DescriptionStarts
*
* @package FireflyIII\Rules\Triggers
*/
class DescriptionStarts 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()
{
$description = strtolower($this->journal->description);
$search = strtolower($this->trigger->trigger_value);
$part = substr($description, 0, strlen($search));
if ($part == $search) {
Log::debug('"' . $description . '" starts with "' . $search . '". Return true.');
return true;
}
Log::debug('"' . $description . '" does not start with "' . $search . '". Return false.');
return false;
}
}

View File

@ -18,7 +18,7 @@ use Log;
*
* @package FireflyIII\Rules\Triggers
*/
class Ends implements TriggerInterface
class ToAccountEnds implements TriggerInterface
{
/** @var RuleTrigger */
protected $trigger;

View File

@ -179,11 +179,10 @@ return [
'to_account_is' => 'FireflyIII\Rules\Triggers\ToAccountIs',
'to_account_contains' => 'FireflyIII\Rules\Triggers\ToAccountContains',
'transaction_type' => 'FireflyIII\Rules\Triggers\TransactionType',
'amount_less' => 'FireflyIII\Rules\Triggers',
'amount_exactly' => 'FireflyIII\Rules\Triggers',
'amount_exactly_not' => 'FireflyIII\Rules\Triggers',
'amount_more' => 'FireflyIII\Rules\Triggers',
'description_starts' => 'FireflyIII\Rules\Triggers',
'amount_less' => 'FireflyIII\Rules\Triggers\AmountLess',
'amount_exactly' => 'FireflyIII\Rules\Triggers\AmountExactly',
'amount_more' => 'FireflyIII\Rules\Triggers\AmountMore',
'description_starts' => 'FireflyIII\Rules\Triggers\DescriptionStarts',
'description_ends' => 'FireflyIII\Rules\Triggers',
'description_contains' => 'FireflyIII\Rules\Triggers\DescriptionContains',
'description_is' => 'FireflyIII\Rules\Triggers',