Skip to content

Commit

Permalink
Merge pull request #32 from fruitl00p/knab-fixes
Browse files Browse the repository at this point in the history
KNAB fixes
  • Loading branch information
fruitl00p committed May 20, 2016
2 parents 26e69a3 + 36afaf0 commit ab0b77a
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 9 deletions.
70 changes: 61 additions & 9 deletions src/Parser/Banking/Mt940/Engine/Knab.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,49 +14,101 @@
class Knab extends Engine
{
const IBAN = '[a-zA-Z]{2}[0-9]{2}[a-zA-Z0-9]{4}[0-9]{7}(?:[a-zA-Z0-9]?){0,16}';

/**
* returns the name of the bank
* @return string
* @inheritdoc
*/
protected function parseStatementBank()
{
return 'KNAB';
}

/**
* @inheritdoc
*/
protected function parseStatementStartPrice()
{
return $this->parseStatementPrice('60M');
}

/**
* @inheritdoc
*/
protected function parseStatementEndPrice()
{
return $this->parseStatementPrice('62M');
}

/**
* @inheritdoc
*/
protected function parseStatementStartTimestamp()
{
return $this->parseTimestampFromStatement('60M');
}

/**
* @inheritdoc
*/
protected function parseStatementEndTimestamp()
{
return $this->parseTimestampFromStatement('62M');
}

/**
* @inheritdoc
*/
protected function parseTransactionAccount()
{
$results = [];

$pattern = '/^:86:.*?REK:\s*(?<account>' . self::IBAN . '|\d+)/ims';
if (preg_match($pattern, $this->getCurrentTransactionData(), $results)
&& !empty($results['account'])
&& !empty($results['account'])
) {
return $results['account'];
}
}

/**
* @inheritdoc
*/
protected function parseTransactionAccountName()
{
$results = [];
if (preg_match('/^:86:.*?\/NAAM: (.*?)\s*:\d{2}.?:/ims', $this->getCurrentTransactionData(), $results)
&& !empty($results[1])
if (preg_match('/NAAM: (.+)/', $this->getCurrentTransactionData(), $results)
&& !empty($results[1])
) {
return $results[1];
return trim($results[1]);
}
}

/**
* @inheritdoc
*/
protected function parseTransactionDescription()
{
if (preg_match('/^:86:(.*?)REK:/ims', $this->getCurrentTransactionData(), $results)) {
return $results[1];
$description = parent::parseTransactionDescription();
$accountIsInDescription = strpos($description, 'REK:');
if ($accountIsInDescription !== false) {
return trim(substr($description, 0, $accountIsInDescription));
}

$name = $this->parseTransactionAccountName();
$accountNameIsInDescription = strpos($description, $name);
if ($accountNameIsInDescription !== false) {
return trim(substr($description, 0, $accountNameIsInDescription-6));
}
return $description;
}

/**
* @inheritdoc
*/
public static function isApplicable($string)
{
$firstline = strtok($string, "\r\n\t");
return strpos($firstline, 'F01KNABNL2HAXXX0000000000') !== false;
}


}
48 changes: 48 additions & 0 deletions test/Parser/Banking/Mt940/Engine/Knab/ParseTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace Kingsquare\Parser\Banking\Mt940\Engine\Knab;

use Kingsquare\Parser\Banking\Mt940\Engine\Knab;

/**
*
*/
class ParseTest extends \PHPUnit_Framework_TestCase
{
/**
* @var Knab
*/
private $engine = null;

protected function setUp()
{
$this->engine = new Knab();
$this->engine->loadString(file_get_contents(__DIR__.'/sample'));
}



public function testParseStatementBank()
{
$method = new \ReflectionMethod($this->engine, 'parseStatementBank');
$method->setAccessible(true);
$this->assertEquals('KNAB', $method->invoke($this->engine));
}

public function testParsesAllFoundStatements()
{
$statements = $this->engine->parse();

$this->assertEquals(1, count($statements));
$this->assertEquals('03-12-2015', $statements[0]->getStartTimestamp('d-m-Y'));
$this->assertEquals('03-12-2015', $statements[0]->getStartTimestamp('d-m-Y'));
}

public function testCorrectHandlingOfVariousStatementPricing()
{
$statements = $this->engine->parse();
$this->assertEquals(1000.21, $statements[0]->getStartPrice());
$this->assertEquals(945.21, $statements[0]->getEndPrice());
$this->assertEquals(55, $statements[0]->getDeltaPrice());
}
}
18 changes: 18 additions & 0 deletions test/Parser/Banking/Mt940/Engine/Knab/sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{1:F01KNABNL2HAXXX0000000000}{2:I940KNABNL2HXXXXN3020}{4:
:20:B5L04MSAU20N3I99
:25:0762918446
:28C:20/3
:60M:C151203EUR1000,21
:61:1512031203D15,NTRFNONREF
:61:1512031203D15,NTRF03-12-2015 15:58
:86:61385002542767281000000000000000000 6676341986995664 TEST PURCHAS
567890
REK: NL52RABO0326203011/NAAM: SOME NAME
:61:1512031203D10,NTRFNONREF
:86:03-12-2015 16:04 PAS: 1122
NAAM: ATM (DESCRIPTION)
:61:1512031203D15,NTRFNONREF
:86:03-12-2015 16:04 PAS: 1122
NAAM: POS
:62M:C151203EUR945,21
-}

0 comments on commit ab0b77a

Please sign in to comment.