Completed
Branch master (1d0a0c)
by Robin
03:05
created

Ing::sanitizeDescription()   B

Complexity

Conditions 7
Paths 3

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 8
nc 3
nop 1
dl 0
loc 15
rs 8.8333
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
        $description = parent::sanitizeDescription($string);
114
        if (strpos($description, '/REMI/USTD//') !== false
115
                && preg_match('#/REMI/USTD//(.*?)/#s', $description, $results) && !empty($results[1])
116
        ) {
117
            return $results[1];
118
        }
119
        if (strpos($description, '/REMI/STRD/CUR/') !== false
120
                && preg_match('#/REMI/STRD/CUR/(.*?)/#s', $description, $results) && !empty($results[1])
121
        ) {
122
            return $results[1];
123
        }
124
125
        return $description;
126
    }
127
128
    /**
129
     * Overloaded: Is applicable if first line has INGB.
130
     *
131
     * {@inheritdoc}
132
     */
133
    public static function isApplicable($string)
134
    {
135
        $firstline = strtok($string, "\r\n\t");
136
137
        return strpos($firstline, 'INGB') !== false;
138
    }
139
}
140