Ing::parseTransactionAccount()   A
last analyzed

Complexity

Conditions 6
Paths 8

Size

Total Lines 24
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 14
c 1
b 0
f 0
nc 8
nop 0
dl 0
loc 24
rs 9.2222
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 Ing extends Engine
12
{
13
    /**
14
     * returns the name of the bank.
15
     *
16
     * @return string
17
     */
18
    protected function parseStatementBank()
19
    {
20
        return 'ING';
21
    }
22
23
    /**
24
     * Overloaded: Added simple IBAN transaction handling.
25
     *
26
     * @inheritdoc
27
     */
28
    protected function parseTransactionAccount()
29
    {
30
        $account = parent::parseTransactionAccount();
31
        if ($account !== '') {
32
            return $account;
33
        }
34
35
        // IBAN
36
        $transactionData = str_replace('Europese Incasso, doorlopend ', '', $this->getCurrentTransactionData());
37
        $transactionData = preg_replace('![\r\n]+!', '', $transactionData);
38
        if (preg_match('#/CNTP/(.*?)/#', $transactionData, $results)) {
39
            $account = trim($results[1]);
40
            if (!empty($account)) {
41
                return $this->sanitizeAccount($account);
42
            }
43
        }
44
        if (preg_match('#:86:([A-Z]{2}[\d]{2}[A-Z]{4}[\d]+?) [A-Z]{6}[A-Z0-9]{0,4} #', $transactionData, $results)) {
45
            $account = trim($results[1]);
46
            if (!empty($account)) {
47
                return $this->sanitizeAccount($account);
48
            }
49
        }
50
51
        return '';
52
    }
53
54
    /**
55
     * Overloaded: Added simple IBAN transaction handling.
56
     *
57
     * @inheritdoc
58
     */
59
    protected function parseTransactionAccountName()
60
    {
61
        $name = parent::parseTransactionAccountName();
62
        if ($name !== '') {
63
            return $name;
64
        }
65
66
        // IBAN
67
        $transactionData = str_replace('Europese Incasso, doorlopend ', '', $this->getCurrentTransactionData());
68
        $transactionData = preg_replace('![\r\n]+!', '', $transactionData);
69
        if (preg_match('#/CNTP/[^/]*/[^/]*/(.*?)/#', $transactionData, $results)) {
70
            $name = trim($results[1]);
71
            if (!empty($name)) {
72
                return $this->sanitizeAccountName($name);
73
            }
74
        }
75
        if (preg_match('#:86:.*? [^ ]+ (.*)#', $transactionData, $results) !== 1) {
76
            return '';
77
        }
78
79
        return $this->parseNameFromTransactionData($results[1]);
80
    }
81
82
    /**
83
     * Overloaded: ING uses the :61: date-part of the field for two values:
84
     * Valuetimestamp (YYMMDD) and Entry date (book date) (MMDD).
85
     *
86
     * @return int
87
     */
88
    protected function parseTransactionEntryTimestamp()
89
    {
90
        $results = [];
91
        if (preg_match('/^:61:(\d{2})((\d{2})\d{2})((\d{2})\d{2})[C|D]/', $this->getCurrentTransactionData(), $results)
92
            && !empty($results[1])
93
        ) {
94
95
            list(, $valueDateY, $valueDateMD, $valueDateM, $entryDateMD, $entryDateM) = $results;
96
            $entryDate = $valueDateY . $entryDateMD;
97
            if ($valueDateMD !== $entryDateMD && $valueDateM > $entryDateM) {
98
                $entryDate = ($valueDateY + 1) . $entryDateMD;
99
            }
100
101
            return $this->sanitizeTimestamp($entryDate, 'ymd');
102
        }
103
        return 0;
104
    }
105
106
    /**
107
     * @param $transactionData
108
     *
109
     * @return string
110
     */
111
    private function parseNameFromTransactionData($transactionData)
112
    {
113
        if (preg_match('#(.*) (Not-Provided|NOTPROVIDED)#', $transactionData, $results) === 1) {
114
            $name = trim($results[1]);
115
            if (!empty($name)) {
116
                return $this->sanitizeAccountName($name);
117
            }
118
        }
119
120
        if (preg_match('#\D+#', $transactionData, $results)) {
121
            $name = trim($results[0]);
122
            if (!empty($name)) {
123
                return $this->sanitizeAccountName($name);
124
            }
125
        }
126
127
        return '';
128
    }
129
130
    /**
131
     * Overloaded: ING encapsulates the description with /REMI/ for SEPA.
132
     *
133
     * @inheritdoc
134
     */
135
    protected function sanitizeDescription($string)
136
    {
137
        $description = parent::sanitizeDescription($string);
138
        if (strpos($description, '/REMI/USTD//') !== false
139
            && preg_match('#/REMI/USTD//(.*?)/#s', $description, $results) && !empty($results[1])
140
        ) {
141
            return $results[1];
142
        }
143
        if (strpos($description, '/REMI/STRD/CUR/') !== false
144
            && preg_match('#/REMI/STRD/CUR/(.*?)/#s', $description, $results) && !empty($results[1])
145
        ) {
146
            return $results[1];
147
        }
148
149
        return $description;
150
    }
151
152
    /**
153
     * Overloaded: Is applicable if first line has INGB.
154
     *
155
     * @inheritdoc
156
     */
157
    public static function isApplicable($string)
158
    {
159
        $firstline = strtok($string, "\r\n\t");
160
161
        return strpos($firstline, 'INGB') !== false;
162
    }
163
}
164