Passed
Pull Request — master (#65)
by Robin
02:59 queued 01:31
created

Ing::parseNameFromTransactionData()   A

Complexity

Conditions 5
Paths 7

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 9
nc 7
nop 1
dl 0
loc 17
rs 9.6111
c 0
b 0
f 0
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
     * @param $transactionData
84
     *
85
     * @return string
86
     */
87
    private function parseNameFromTransactionData($transactionData)
88
    {
89
        if (preg_match('#(.*) (Not-Provided|NOTPROVIDED)#', $transactionData, $results) === 1) {
90
            $name = trim($results[1]);
91
            if (!empty($name)) {
92
                return $this->sanitizeAccountName($name);
93
            }
94
        }
95
96
        if (preg_match('#\D+#', $transactionData, $results)) {
97
            $name = trim($results[0]);
98
            if (!empty($name)) {
99
                return $this->sanitizeAccountName($name);
100
            }
101
        }
102
103
        return '';
104
    }
105
106
    /**
107
     * Overloaded: ING encapsulates the description with /REMI/ for SEPA.
108
     *
109
     * @inheritdoc
110
     */
111
    protected function sanitizeDescription($string)
112
    {
113
        $descriptionStart = $descriptionEnd = '';
114
        $description = parent::sanitizeDescription($string);
115
        if (strpos($description, '/PREF/') !== false
116
            && preg_match('#/PREF/(.*?)/#s', $description, $results) && !empty($results[1])
117
        ) {
118
            $descriptionStart = $results[1];
119
        }
120
        if (strpos($description, '/EREF/') !== false
121
            && preg_match('#/EREF/(.*?)/#s', $description, $results) && !empty($results[1])
122
        ) {
123
            $descriptionStart = $results[1];
124
        }
125
        if (strpos($description, '/REMI/USTD//') !== false
126
            && preg_match('#/REMI/USTD//(.*?)/#s', $description, $results) && !empty($results[1])
127
        ) {
128
            $descriptionEnd = $results[1];
129
        }
130
        if (strpos($description, '/REMI/STRD/CUR/') !== false
131
            && preg_match('#/REMI/STRD/CUR/(.*?)/#s', $description, $results) && !empty($results[1])
132
        ) {
133
            $descriptionEnd = $results[1];
134
        }
135
136
        return trim($descriptionStart.' '.$descriptionEnd);
137
    }
138
139
    /**
140
     * Overloaded: Is applicable if first line has INGB.
141
     *
142
     * @inheritdoc
143
     */
144
    public static function isApplicable($string)
145
    {
146
        $firstline = strtok($string, "\r\n\t");
147
148
        return strpos($firstline, 'INGB') !== false;
149
    }
150
}
151