Triodos::parseStatementBank()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
namespace Kingsquare\Parser\Banking\Mt940\Engine;
4
5
use Kingsquare\Parser\Banking\Mt940\Engine;
6
7
/**
8
 * @author Kingsquare ([email protected])
9
 * @license http://opensource.org/licenses/MIT MIT
10
 */
11
class Triodos extends Engine
12
{
13
    /**
14
     * returns the name of the bank.
15
     *
16
     * @return string
17
     */
18
    protected function parseStatementBank()
19
    {
20
        return 'Triodos';
21
    }
22
23
    /**
24
     * Overloaded: the bankaccount is always prefixed.
25
     *
26
     * @inheritdoc
27
     */
28
    protected function parseStatementAccount()
29
    {
30
        $results = [];
31
        if (preg_match('#:25:TRIODOSBANK/([\d\.]+)#', $this->getCurrentStatementData(), $results)
32
            && !empty($results[1])
33
        ) {
34
            return $this->sanitizeAccount($results[1]);
35
        }
36
37
        return parent::parseStatementAccount();
38
    }
39
40
    /**
41
     * Overloaded: According to spec, field :28: is always 1.
42
     *
43
     * @inheritdoc
44
     */
45
    protected function parseStatementNumber()
46
    {
47
        return 1;
48
    }
49
50
    /**
51
     * Overloaded: According to spec, field :28: is always 000.
52
     *
53
     * @inheritdoc
54
     */
55
    protected function parseTransactionCode()
56
    {
57
        return '000';
58
    }
59
60
    /**
61
     * Overloaded: It might be IBAN or not and depending on that return a different part of the description.
62
     *
63
     * @inheritdoc
64
     */
65
    protected function parseTransactionAccount()
66
    {
67
        $parts = $this->getDescriptionParts();
68
        $account = $parts[0];
69
        if (preg_match('#[A-Z]{2}[\d]{2}[A-Z]{4}(.*)#', $parts[2], $results)) {
70
            $account = $parts[2];
71
        } elseif (preg_match('#10(\d+)#', $parts[0], $results)) {
72
            $account = $results[1];
73
        }
74
75
        return $this->sanitizeAccount($account);
76
    }
77
78
    /**
79
     * Overloaded: It might be IBAN or not and depending on that return a different part of the description.
80
     *
81
     * @inheritdoc
82
     */
83
    protected function parseTransactionAccountName()
84
    {
85
        $parts = $this->getTransactionAccountParts();
86
87
        return $this->sanitizeAccountName(substr(array_shift($parts), 2));
88
    }
89
90
    private function getTransactionAccountParts()
91
    {
92
        $parts = $this->getDescriptionParts();
93
        array_shift($parts); // remove BBAN / BIC code
94
        if (preg_match('#[A-Z]{2}[\d]{2}[A-Z]{4}(.*)#', $parts[1], $results)) {
95
            array_shift($parts); // remove IBAN too
96
            array_shift($parts); // remove IBAN some more
97
        }
98
99
        array_pop($parts);// remove own account / BBAN
100
        return $parts;
101
    }
102
103
    /**
104
     * Crude parsing of the combined iban / non iban description field.
105
     *
106
     * @inheritdoc
107
     */
108
    protected function parseTransactionDescription()
109
    {
110
        $parts = $this->getTransactionAccountParts();
111
        foreach ($parts as &$part) {
112
            $part = substr($part, 2);
113
        }
114
115
        return $this->sanitizeDescription(implode('', $parts));
116
    }
117
118
    /**
119
     * In Triodos everything is put into :86: field with '>\d{2}' seperators
120
     * This method parses that out and returns the array.
121
     *
122
     * @return array
123
     */
124
    private function getDescriptionParts()
125
    {
126
        $parts = explode('>', parent::parseTransactionDescription());
127
        array_shift($parts); // remove 000 prefix
128
        return $parts;
129
    }
130
131
    /**
132
     * Overloaded: Do not skip a header.
133
     *
134
     * @inheritdoc
135
     */
136
    protected function parseStatementData()
137
    {
138
        return preg_split(
139
            '/(^:20:|^-X{,3}$|\Z)/m',
140
            $this->getRawData(),
141
            -1,
142
            PREG_SPLIT_NO_EMPTY
143
        );
144
    }
145
146
    /**
147
     * Overloaded: Is applicable if second line has :25:TRIODOSBANK.
148
     *
149
     * @inheritdoc
150
     */
151
    public static function isApplicable($string)
152
    {
153
        static $token = "\r\n\t";
154
        /** @noinspection UnusedFunctionResultInspection */
155
        strtok($string, $token);
156
        $secondline = strtok($token);
157
        return strpos($secondline, ':25:TRIODOSBANK') !== false;
158
    }
159
}
160