Completed
Push — master ( 3652f2...13727b )
by
unknown
26s queued 10s
created

Zetb::isApplicable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 2
c 1
b 0
f 1
nc 1
nop 1
dl 0
loc 5
rs 10
1
<?php
2
3
namespace Kingsquare\Parser\Banking\Mt940\Engine;
4
5
use Kingsquare\Banking\Transaction\Type;
6
use Kingsquare\Parser\Banking\Mt940\Engine;
7
8
/**
9
 * Zetb parser for Kingsquare mt940 package.
10
 *
11
 * @package Kingsquare\Parser\Banking\Mt940\Engine
12
 * @author Sevan Nerse ([email protected])
13
 * @license http://opensource.org/licenses/MIT MIT
14
 */
15
class Zetb extends Engine
16
{
17
    const IBAN = '[a-zA-Z]{2}[0-9]{2}[a-zA-Z0-9]{4}[0-9]{7}(?:[a-zA-Z0-9]?){0,16}';
18
19
    /**
20
     * @inheritdoc
21
     */
22
    protected function parseStatementBank()
23
    {
24
        return 'ZETB';
25
    }
26
27
    /**
28
     * @inheritdoc
29
     */
30
    protected function parseTransactionAccount()
31
    {
32
        $results = [];
33
34
        // SEPA MT940 Structured
35
        if (preg_match('/^:86:.*?\/IBAN\/(' . self::IBAN . ')/ims', $this->getCurrentTransactionData(), $results)
36
            && !empty($results[1])
37
        ) {
38
            return $this->sanitizeAccount($results[1]);
39
        }
40
41
        $pattern = '/^:86:.*?REK:\s*(?<account>' . self::IBAN . '|\d+)/ims';
42
        if (preg_match($pattern, $this->getCurrentTransactionData(), $results)
43
            && !empty($results['account'])
44
        ) {
45
            return $results['account'];
46
        }
47
48
        return '';
49
    }
50
51
    /**
52
     * @inheritdoc
53
     */
54
    protected function parseTransactionAccountName()
55
    {
56
        $results = [];
57
58
        // SEPA MT940 Structured
59
        if (preg_match('#/NAME/(.*?)/(EREF|REMI|ADDR)/#ms', $this->getCurrentTransactionData(), $results)
60
            && !empty($results[1])
61
        ) {
62
            $accountName = trim($results[1]);
63
            if (!empty($accountName)) {
64
                return $this->sanitizeAccountName($accountName);
65
            }
66
        }
67
68
        if (preg_match('/NAAM: (.+)/', $this->getCurrentTransactionData(), $results)
69
            && !empty($results[1])
70
        ) {
71
            return trim($results[1]);
72
        }
73
        
74
        if (preg_match('#/NAME/(.*?)\n?/(REMI|CSID)/#ms', $this->getCurrentTransactionData(), $results)
75
            && !empty($results[1])
76
        ) {
77
            return trim($results[1]);
78
        }
79
80
        return '';
81
    }
82
83
    /**
84
     * @inheritdoc
85
     */
86
    protected function parseTransactionDescription()
87
    {
88
        $description = parent::parseTransactionDescription();
89
90
        // SEPA MT940 Structured
91
        if (strpos($description, '/REMI/') !== false
92
            && preg_match('#/REMI/(.*)[/:]?#', $description, $results) && !empty($results[1])
93
        ) {
94
            return $results[1];
95
        }
96
97
        $accountIsInDescription = strpos($description, 'REK:');
98
        
99
        if ($accountIsInDescription !== false) {
100
            return trim(substr($description, 0, $accountIsInDescription));
101
        }
102
103
        $name = $this->parseTransactionAccountName();
104
        
105
        if ($name === '') {
106
            return $description;
107
        }
108
        
109
        $accountNameIsInDescription = strpos($description, $name);
110
        
111
        if ($accountNameIsInDescription !== false) {
112
            return trim(substr($description, 0, $accountNameIsInDescription - 6));
113
        }
114
        
115
        return $description;
116
    }
117
118
    /**
119
     * @inheritdoc
120
     */
121
    public static function isApplicable($string)
122
    {
123
        $firstline = strtok($string, "\r\n\t");
124
125
        return strpos($firstline, 'F01ZEIBNGLAXXXX') !== false;
126
    }
127
128
    /**
129
     * @TODO WIP get this into the transaction somehow.. (possibly as a decorator over the transactions?)
130
     * @return int
131
     */
132
    protected function parseTransactionType()
133
    {
134
        static $map = [
135
            541 => Type::SEPA_TRANSFER,
136
            544 => Type::SEPA_TRANSFER,
137
            547 => Type::SEPA_TRANSFER,
138
            64 => Type::SEPA_DIRECTDEBIT,
139
            93 => Type::BANK_COSTS,
140
            13 => Type::PAYMENT_TERMINAL,
141
            30 => Type::PAYMENT_TERMINAL,
142
            'MSC' => Type::BANK_INTEREST,
143
            'TRF' => Type::UNKNOWN,
144
        ];
145
146
        $code = $this->parseTransactionCode();
147
148
        if (array_key_exists($code, $map)) {
149
            return $map[$code];
150
        }
151
152
        throw new \RuntimeException("Don't know code $code for this bank");
153
    }
154
}
155