This repository has been archived by the owner on Dec 8, 2023. It is now read-only.
forked from fruitl00p/php-mt940
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Triodos.php
158 lines (139 loc) · 3.88 KB
/
Triodos.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
147
148
149
150
151
152
153
154
155
156
157
158
<?php
namespace Kingsquare\Parser\Banking\Mt940\Engine;
use Kingsquare\Parser\Banking\Mt940\Engine;
/**
* @author Kingsquare ([email protected])
* @license http://opensource.org/licenses/MIT MIT
*/
class Triodos extends Engine
{
/**
* returns the name of the bank.
*
* @return string
*/
protected function parseStatementBank()
{
return 'Triodos';
}
/**
* Overloaded: the bankaccount is always prefixed.
*
* {@inheritdoc}
*/
protected function parseStatementAccount()
{
$results = [];
if (preg_match('#:25:TRIODOSBANK/([\d\.]+)#', $this->getCurrentStatementData(), $results)
&& !empty($results[1])
) {
return $this->sanitizeAccount($results[1]);
}
return parent::parseStatementAccount();
}
/**
* Overloaded: According to spec, field :28: is always 1.
*
* {@inheritdoc}
*/
protected function parseStatementNumber()
{
return 1;
}
/**
* Overloaded: According to spec, field :28: is always 000.
*
* {@inheritdoc}
*/
protected function parseTransactionCode()
{
return '000';
}
/**
* Overloaded: It might be IBAN or not and depending on that return a different part of the description.
*
* {@inheritdoc}
*/
protected function parseTransactionAccount()
{
$parts = $this->getDescriptionParts();
$account = $parts[0];
if (preg_match('#[A-Z]{2}[\d]{2}[A-Z]{4}(.*)#', $parts[2], $results)) {
$account = $parts[2];
} elseif (preg_match('#10(\d+)#', $parts[0], $results)) {
$account = $results[1];
}
return $this->sanitizeAccount($account);
}
/**
* Overloaded: It might be IBAN or not and depending on that return a different part of the description.
*
* {@inheritdoc}
*/
protected function parseTransactionAccountName()
{
$parts = $this->getTransactionAccountParts();
return $this->sanitizeAccountName(substr(array_shift($parts), 2));
}
private function getTransactionAccountParts()
{
$parts = $this->getDescriptionParts();
array_shift($parts); // remove BBAN / BIC code
if (preg_match('#[A-Z]{2}[\d]{2}[A-Z]{4}(.*)#', $parts[1], $results)) {
array_shift($parts); // remove IBAN too
array_shift($parts); // remove IBAN some more
}
array_pop($parts);// remove own account / BBAN
return $parts;
}
/**
* Crude parsing of the combined iban / non iban description field.
*
* {@inheritdoc}
*/
protected function parseTransactionDescription()
{
$parts = $this->getTransactionAccountParts();
foreach ($parts as &$part) {
$part = substr($part, 2);
}
return $this->sanitizeDescription(implode('', $parts));
}
/**
* In Triodos everything is put into :86: field with '>\d{2}' seperators
* This method parses that out and returns the array.
*
* @return array
*/
private function getDescriptionParts()
{
$parts = explode('>', parent::parseTransactionDescription());
array_shift($parts); // remove 000 prefix
return $parts;
}
/**
* Overloaded: Do not skip a header.
*
* {@inheritdoc}
*/
protected function parseStatementData()
{
return preg_split(
'/(^:20:|^-X{,3}$|\Z)/m',
$this->getRawData(),
-1,
PREG_SPLIT_NO_EMPTY
);
}
/**
* Overloaded: Is applicable if second line has :25:TRIODOSBANK.
*
* {@inheritdoc}
*/
public static function isApplicable($string)
{
strtok($string, "\r\n\t");
$secondline = strtok("\r\n\t");
return strpos($secondline, ':25:TRIODOSBANK') !== false;
}
}