From 36afaf0eb1280ee7d3bcc3ebe4a4161d3d77b3fd Mon Sep 17 00:00:00 2001 From: Robin Speekenbrink Date: Mon, 4 Jan 2016 10:26:40 +0100 Subject: [PATCH] updated the KNAB parser based on private test file provided by KNAB + implemented start/end timestamps and prices --- src/Parser/Banking/Mt940/Engine/Knab.php | 70 ++++++++++++++++--- .../Banking/Mt940/Engine/Knab/ParseTest.php | 48 +++++++++++++ test/Parser/Banking/Mt940/Engine/Knab/sample | 18 +++++ 3 files changed, 127 insertions(+), 9 deletions(-) create mode 100644 test/Parser/Banking/Mt940/Engine/Knab/ParseTest.php create mode 100644 test/Parser/Banking/Mt940/Engine/Knab/sample diff --git a/src/Parser/Banking/Mt940/Engine/Knab.php b/src/Parser/Banking/Mt940/Engine/Knab.php index 964d588..64c27a1 100644 --- a/src/Parser/Banking/Mt940/Engine/Knab.php +++ b/src/Parser/Banking/Mt940/Engine/Knab.php @@ -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*(?' . 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; } - } diff --git a/test/Parser/Banking/Mt940/Engine/Knab/ParseTest.php b/test/Parser/Banking/Mt940/Engine/Knab/ParseTest.php new file mode 100644 index 0000000..d117094 --- /dev/null +++ b/test/Parser/Banking/Mt940/Engine/Knab/ParseTest.php @@ -0,0 +1,48 @@ +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()); + } +} diff --git a/test/Parser/Banking/Mt940/Engine/Knab/sample b/test/Parser/Banking/Mt940/Engine/Knab/sample new file mode 100644 index 0000000..0886808 --- /dev/null +++ b/test/Parser/Banking/Mt940/Engine/Knab/sample @@ -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 +-} \ No newline at end of file