Skip to content

Add description line glue property (#481) #483

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions lib/Fhp/Action/GetStatementOfAccount.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,9 @@ private function parseMt940()
} else {
$parser = new MT940();
}
if (null !== $this->account->getTransactionDescriptionLineGlue()) {
$parser->setDescriptionLineGlue($this->account->getTransactionDescriptionLineGlue());
}

try {
// Note: Some banks encode their MT 940 data as SWIFT/ISO-8859 like it should be according to the
Expand Down
27 changes: 22 additions & 5 deletions lib/Fhp/MT940/MT940.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ class MT940
public const CD_CREDIT = 'credit';
public const CD_DEBIT = 'debit';

private string $descriptionLineGlue = '';

/**
* @throws MT940Exception
*/
Expand Down Expand Up @@ -143,7 +145,7 @@ public function parse(string $rawData): array

if (isset($result[$soaDate])) {
#$result[$soaDate] = ['end_balance' => []];

$amount = str_replace(',', '.', substr($day[$i], 10, -1));
$cdMark = substr($day[$i], 0, 1);
if ($cdMark == 'C') {
Expand Down Expand Up @@ -180,15 +182,17 @@ protected function parseDescription($descr, $transaction): array
preg_match_all('/\?(\d{2})([^\?]+)/', $descr, $matches, PREG_SET_ORDER);

$descriptionLines = [];
$description1 = ''; // Legacy, could be removed.
$description2 = ''; // Legacy, could be removed.
$description1 = '';
$description2 = '';
foreach ($matches as $m) {
$index = (int) $m[1];

if ((20 <= $index && $index <= 29) || (60 <= $index && $index <= 63)) {
if (20 <= $index && $index <= 29) {
if ($description1 !== '') $description1 .= $this->descriptionLineGlue;
$description1 .= $m[2];
} else {
if ($description2 !== '') $description2 .= $this->descriptionLineGlue;
$description2 .= $m[2];
}
$descriptionLines[] = $m[2];
Expand Down Expand Up @@ -222,7 +226,7 @@ protected function extractStructuredDataFromRemittanceLines($descriptionLines, s
{
$description = [];
if (empty($descriptionLines) || strlen($descriptionLines[0]) < 5 || $descriptionLines[0][4] !== '+') {
$description['SVWZ'] = implode('', $descriptionLines);
$description['SVWZ'] = implode($this->descriptionLineGlue, $descriptionLines);
} else {
$lastType = null;
foreach ($descriptionLines as $line) {
Expand All @@ -233,7 +237,11 @@ protected function extractStructuredDataFromRemittanceLines($descriptionLines, s
$lastType = substr($line, 0, 4);
$description[$lastType] = substr($line, 5);
} else {
$description[$lastType] .= $line;
if ($lastType === 'SVWZ') {
$description[$lastType] .= $this->descriptionLineGlue . $line;
} else {
$description[$lastType] .= $line;
}
}
if (strlen($line) < 27) {
// Usually, lines are 27 characters long. In case characters are missing, then it's either the end
Expand All @@ -255,4 +263,13 @@ protected function getDate(string $val): string
preg_match('/(\d{4})(\d{2})(\d{2})/', $val, $m);
return $m[1] . '-' . $m[2] . '-' . $m[3];
}

public function getDescriptionLineGlue(): string {
return $this->descriptionLineGlue;
}

public function setDescriptionLineGlue(string $descriptionLineGlue): self {
$this->descriptionLineGlue = $descriptionLineGlue;
return $this;
}
}
22 changes: 22 additions & 0 deletions lib/Fhp/Model/SEPAAccount.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ class SEPAAccount
/** @var string|null */
protected $blz;

/**
* Determines how single lines in transaction descriptions are joined, see #481.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should somehow refer to MT940 / the fact that this is about transaction statements for this account.

* Defaults to an empty string for maximum compatibility but some banks implicitly assume line breaks for this.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd say "backwards compatibility" (with previous versions of this library) rather than "maximum compatibility" (with the largest number of banks).

* This is not provided by the bank and needs to be set manually.
* @var string|null
*/
protected $transactionDescriptionLineGlue;

public function getIban(): ?string
{
return $this->iban;
Expand Down Expand Up @@ -94,4 +102,18 @@ public function setBlz(?string $blz)

return $this;
}

public function getTransactionDescriptionLineGlue(): ?string
{
return $this->transactionDescriptionLineGlue;
}

/**
* @return $this
*/
public function setTransactionDescriptionLineGlue($transactionDescriptionLineGlue)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add type for the argument?

{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add self return type?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I left it out to match the other getters in this file.

$this->transactionDescriptionLineGlue = $transactionDescriptionLineGlue;
return $this;
}
}
1 change: 1 addition & 0 deletions lib/Tests/Fhp/Integration/DKB/DKBIntegrationTestBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ protected function getTestAccount(): SEPAAccount
$sepaAccount->setBic('BYLADEM1001');
$sepaAccount->setAccountNumber('1234567890');
$sepaAccount->setBlz('12030000');
$sepaAccount->setTransactionDescriptionLineGlue(PHP_EOL);
return $sepaAccount;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ private function checkResult(StatementOfAccount $statement)
$this->assertEquals(new \DateTime('2019-09-04'), $transaction1->getBookingDate());
$this->assertEquals(Statement::CD_DEBIT, $transaction1->getCreditDebit());
$this->assertEqualsWithDelta(12.00, $transaction1->getAmount(), 0.01);
$this->assertEquals('32301000-P111111-33333333 DATUM 02.09.2019, 22.19 UHR1.TAN 012345', $transaction1->getMainDescription());
$this->assertEquals("32301000-P111111-33333\n333 \nDATUM 02.09.2019, 22.19 UHR\n1.TAN 012345", $transaction1->getMainDescription());
$this->assertEquals('HKCCS12345', $transaction1->getStructuredDescription()['KREF']);
$this->assertEquals('EMPFAENGER ABCDE', $transaction1->getName());

Expand Down