-
Notifications
You must be signed in to change notification settings - Fork 1
/
Asn.php
146 lines (126 loc) · 4.02 KB
/
Asn.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<?php
namespace Kingsquare\Parser\Banking\Mt940\Engine;
use Kingsquare\Banking\Transaction\Type;
use Kingsquare\Parser\Banking\Mt940\Engine;
/**
* Asn parser for Kingsquare mt940 package.
*
* @package Kingsquare\Parser\Banking\Mt940\Engine
* @author Sevan Nerse ([email protected])
* @license http://opensource.org/licenses/MIT MIT
*/
class Asn 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}';
/**
* @inheritdoc
*/
protected function parseStatementBank()
{
return 'ASN';
}
/**
* @inheritdoc
*/
protected function parseTransactionAccount()
{
$results = [];
// SEPA MT940 Structured
if (preg_match('/^:86:.*?\/IBAN\/(' . self::IBAN . ')/ims', $this->getCurrentTransactionData(), $results)
&& !empty($results[1])
) {
return $this->sanitizeAccount($results[1]);
}
$pattern = '/^:86:.*?REK:\s*(?<account>' . self::IBAN . '|\d+)/ims';
if (preg_match($pattern, $this->getCurrentTransactionData(), $results)
&& !empty($results['account'])
) {
return $results['account'];
}
return '';
}
/**
* @inheritdoc
*/
protected function parseTransactionAccountName()
{
$results = [];
// SEPA MT940 Structured
if (preg_match('#/NAME/(.*?)/(EREF|REMI|ADDR)/#ms', $this->getCurrentTransactionData(), $results)
&& !empty($results[1])
) {
$accountName = trim($results[1]);
if (!empty($accountName)) {
return $this->sanitizeAccountName($accountName);
}
}
if (preg_match('/NAAM: (.+)/', $this->getCurrentTransactionData(), $results)
&& !empty($results[1])
) {
return trim($results[1]);
}
if (preg_match('#/NAME/(.*?)\n?/(REMI|CSID)/#ms', $this->getCurrentTransactionData(), $results)
&& !empty($results[1])
) {
return trim($results[1]);
}
return '';
}
/**
* @inheritdoc
*/
protected function parseTransactionDescription()
{
$description = parent::parseTransactionDescription();
// SEPA MT940 Structured
if (strpos($description, '/REMI/') !== false
&& preg_match('#/REMI/(.*)[/:]?#', $description, $results) && !empty($results[1])
) {
return $results[1];
}
$accountIsInDescription = strpos($description, 'REK:');
if ($accountIsInDescription !== false) {
return trim(substr($description, 0, $accountIsInDescription));
}
$name = $this->parseTransactionAccountName();
if ($name === '') {
return $description;
}
$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, 'F01ASNBNL21XXXX0000000000') !== false;
}
/**
* @TODO WIP get this into the transaction somehow.. (possibly as a decorator over the transactions?)
* @return int
*/
protected function parseTransactionType()
{
static $map = [
541 => Type::SEPA_TRANSFER,
544 => Type::SEPA_TRANSFER,
547 => Type::SEPA_TRANSFER,
64 => Type::SEPA_DIRECTDEBIT,
93 => Type::BANK_COSTS,
13 => Type::PAYMENT_TERMINAL,
30 => Type::PAYMENT_TERMINAL,
'MSC' => Type::BANK_INTEREST,
'TRF' => Type::UNKNOWN,
];
$code = $this->parseTransactionCode();
if (array_key_exists($code, $map)) {
return $map[$code];
}
throw new \RuntimeException("Don't know code $code for this bank");
}
}