1
|
|
|
<?php |
2
|
|
|
namespace Kingsquare\Parser\Banking\Mt940\Engine; |
3
|
|
|
|
4
|
|
|
use Kingsquare\Parser\Banking\Mt940\Engine; |
5
|
|
|
use Kingsquare\Objects\TransactionType; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Knab parser for Kingsquare mt940 package. |
9
|
|
|
* |
10
|
|
|
* @package Kingsquare\Parser\Banking\Mt940\Engine |
11
|
|
|
* @author Kingsquare ([email protected]) |
12
|
|
|
* @author Sam Mousa ([email protected]) |
13
|
|
|
* @license http://opensource.org/licenses/MIT MIT |
14
|
|
|
*/ |
15
|
|
|
class Knab extends Engine |
16
|
|
|
{ |
17
|
|
|
const IBAN = '[a-zA-Z]{2}[0-9]{2}[a-zA-Z0-9]{4}[0-9]{7}(?:[a-zA-Z0-9]?){0,16}'; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @inheritdoc |
21
|
|
|
*/ |
22
|
|
|
protected function parseStatementBank() |
23
|
|
|
{ |
24
|
|
|
return 'KNAB'; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @inheritdoc |
29
|
|
|
*/ |
30
|
|
|
protected function parseStatementStartPrice() |
31
|
|
|
{ |
32
|
|
|
return $this->parseStatementPrice('60M'); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @inheritdoc |
37
|
|
|
*/ |
38
|
|
|
protected function parseStatementEndPrice() |
39
|
|
|
{ |
40
|
|
|
return $this->parseStatementPrice('62M'); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @inheritdoc |
45
|
|
|
*/ |
46
|
|
|
protected function parseStatementStartTimestamp() |
47
|
|
|
{ |
48
|
|
|
return $this->parseTimestampFromStatement('60M'); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @inheritdoc |
53
|
|
|
*/ |
54
|
|
|
protected function parseStatementEndTimestamp() |
55
|
|
|
{ |
56
|
|
|
return $this->parseTimestampFromStatement('62M'); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @inheritdoc |
61
|
|
|
*/ |
62
|
|
|
protected function parseTransactionAccount() |
63
|
|
|
{ |
64
|
|
|
$results = []; |
65
|
|
|
|
66
|
|
|
// SEPA MT940 Structured |
67
|
|
|
if (preg_match('/^:86:.*?\/IBAN\/(' . self::IBAN . ')/ims', $this->getCurrentTransactionData(), $results) |
68
|
|
|
&& !empty($results[1]) |
69
|
|
|
) { |
70
|
|
|
return $this->sanitizeAccount($results[1]); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
|
74
|
|
|
$pattern = '/^:86:.*?REK:\s*(?<account>' . self::IBAN . '|\d+)/ims'; |
75
|
|
|
if (preg_match($pattern, $this->getCurrentTransactionData(), $results) |
76
|
|
|
&& !empty($results['account']) |
77
|
|
|
) { |
78
|
|
|
return $results['account']; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return ''; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @inheritdoc |
86
|
|
|
*/ |
87
|
|
|
protected function parseTransactionAccountName() |
88
|
|
|
{ |
89
|
|
|
$results = []; |
90
|
|
|
|
91
|
|
|
// SEPA MT940 Structured |
92
|
|
View Code Duplication |
if (preg_match('#/NAME/(.*?)/(EREF|REMI|ADDR)/#ms', $this->getCurrentTransactionData(), $results) |
|
|
|
|
93
|
|
|
&& !empty($results[1]) |
94
|
|
|
) { |
95
|
|
|
$accountName = trim($results[1]); |
96
|
|
|
if (!empty($accountName)) { |
97
|
|
|
return $this->sanitizeAccountName($accountName); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
if (preg_match('/NAAM: (.+)/', $this->getCurrentTransactionData(), $results) |
102
|
|
|
&& !empty($results[1]) |
103
|
|
|
) { |
104
|
|
|
return trim($results[1]); |
105
|
|
|
} |
106
|
|
|
if (preg_match('#/NAME/(.*?)\n?/(REMI|CSID)/#ms', $this->getCurrentTransactionData(), $results) |
107
|
|
|
&& !empty($results[1]) |
108
|
|
|
) { |
109
|
|
|
return trim($results[1]); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
return ''; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @inheritdoc |
117
|
|
|
*/ |
118
|
|
|
protected function parseTransactionDescription() |
119
|
|
|
{ |
120
|
|
|
$description = parent::parseTransactionDescription(); |
121
|
|
|
|
122
|
|
|
// SEPA MT940 Structured |
123
|
|
|
if (strpos($description, '/REMI/') !== false |
124
|
|
|
&& preg_match('#/REMI/(.*)[/:]?#', $description, $results) && !empty($results[1]) |
125
|
|
|
) { |
126
|
|
|
return $results[1]; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
$accountIsInDescription = strpos($description, 'REK:'); |
130
|
|
|
if ($accountIsInDescription !== false) { |
131
|
|
|
return trim(substr($description, 0, $accountIsInDescription)); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
$name = $this->parseTransactionAccountName(); |
135
|
|
|
if ($name === '') { |
136
|
|
|
return $description; |
137
|
|
|
} |
138
|
|
|
$accountNameIsInDescription = strpos($description, $name); |
139
|
|
|
if ($accountNameIsInDescription !== false) { |
140
|
|
|
return trim(substr($description, 0, $accountNameIsInDescription-6)); |
141
|
|
|
} |
142
|
|
|
return $description; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @inheritdoc |
147
|
|
|
*/ |
148
|
|
|
public static function isApplicable($string) |
149
|
|
|
{ |
150
|
|
|
$firstline = strtok($string, "\r\n\t"); |
151
|
|
|
return strpos($firstline, 'F01KNABNL2HAXXX0000000000') !== false; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
protected function parseTransactionType() |
155
|
|
|
{ |
156
|
|
|
$code = $this->parseTransactionCode(); |
157
|
|
|
switch ($code) { |
158
|
|
|
case 541: |
159
|
|
|
case 544: |
160
|
|
|
case 547: |
161
|
|
|
$result = TransactionType::get(TransactionType::SEPA_TRANSFER); |
162
|
|
|
break; |
163
|
|
|
case 64: |
164
|
|
|
$result = TransactionType::get(TransactionType::SEPA_DIRECTDEBIT); |
165
|
|
|
break; |
166
|
|
|
case 93: |
167
|
|
|
$result = TransactionType::get(TransactionType::BANK_COSTS); |
168
|
|
|
break; |
169
|
|
|
case 13: |
170
|
|
|
case 30: |
171
|
|
|
$result = TransactionType::get(TransactionType::PAYMENT_TERMINAL); |
172
|
|
|
break; |
173
|
|
|
case "MSC": |
174
|
|
|
$result = TransactionType::get(TransactionType::BANK_INTEREST); |
175
|
|
|
break; |
176
|
|
|
case "TRF": |
177
|
|
|
$result = TransactionType::get(TransactionType::UNKNOWN); |
178
|
|
|
break; |
179
|
|
|
default: |
180
|
|
|
var_dump($code); |
|
|
|
|
181
|
|
|
var_dump($this->getCurrentTransactionData()); die(); |
182
|
|
|
throw new \RuntimeException("Don't know code $code for RABOBANK"); |
|
|
|
|
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
return $result; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
} |
189
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.