Completed
Push — master ( bd2bbc...380a6c )
by Robin
01:57
created

Knab::parseTransactionDescription()   C

Complexity

Conditions 7
Paths 5

Size

Total Lines 26
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 26
rs 6.7272
cc 7
eloc 15
nc 5
nop 0
1
<?php
2
namespace Kingsquare\Parser\Banking\Mt940\Engine;
3
4
use Kingsquare\Parser\Banking\Mt940\Engine;
5
6
/**
7
 * Knab parser for Kingsquare mt940 package.
8
 *
9
 * @package Kingsquare\Parser\Banking\Mt940\Engine
10
 * @author Kingsquare ([email protected])
11
 * @author Sam Mousa ([email protected])
12
 * @license http://opensource.org/licenses/MIT MIT
13
 */
14
class Knab extends Engine
15
{
16
    const IBAN = '[a-zA-Z]{2}[0-9]{2}[a-zA-Z0-9]{4}[0-9]{7}(?:[a-zA-Z0-9]?){0,16}';
17
18
    /**
19
     * @inheritdoc
20
     */
21
    protected function parseStatementBank()
22
    {
23
        return 'KNAB';
24
    }
25
26
    /**
27
     * @inheritdoc
28
     */
29
    protected function parseStatementStartPrice()
30
    {
31
        return $this->parseStatementPrice('60M');
32
    }
33
34
    /**
35
     * @inheritdoc
36
     */
37
    protected function parseStatementEndPrice()
38
    {
39
        return $this->parseStatementPrice('62M');
40
    }
41
42
    /**
43
     * @inheritdoc
44
     */
45
    protected function parseStatementStartTimestamp()
46
    {
47
        return $this->parseTimestampFromStatement('60M');
48
    }
49
50
    /**
51
     * @inheritdoc
52
     */
53
    protected function parseStatementEndTimestamp()
54
    {
55
        return $this->parseTimestampFromStatement('62M');
56
    }
57
58
    /**
59
     * @inheritdoc
60
     */
61
    protected function parseTransactionAccount()
62
    {
63
        $results = [];
64
65
        // SEPA MT940 Structured
66
        if (preg_match('/^:86:.*?\/IBAN\/(' . self::IBAN . ')/ims', $this->getCurrentTransactionData(), $results)
67
                && !empty($results[1])
68
        ) {
69
            return $this->sanitizeAccount($results[1]);
70
        }
71
72
73
        $pattern = '/^:86:.*?REK:\s*(?<account>' . self::IBAN . '|\d+)/ims';
74
        if (preg_match($pattern, $this->getCurrentTransactionData(), $results)
75
                && !empty($results['account'])
76
        ) {
77
            return $results['account'];
78
        }
79
80
        return '';
81
    }
82
83
    /**
84
     * @inheritdoc
85
     */
86
    protected function parseTransactionAccountName()
87
    {
88
        $results = [];
89
90
        // SEPA MT940 Structured
91 View Code Duplication
        if (preg_match('#/NAME/(.*?)/(EREF|REMI|ADDR)/#ms', $this->getCurrentTransactionData(), $results)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
92
                && !empty($results[1])
93
        ) {
94
            $accountName = trim($results[1]);
95
            if (!empty($accountName)) {
96
                return $this->sanitizeAccountName($accountName);
97
            }
98
        }
99
100
        if (preg_match('/NAAM: (.+)/', $this->getCurrentTransactionData(), $results)
101
                && !empty($results[1])
102
        ) {
103
            return trim($results[1]);
104
        }
105
        if (preg_match('#/NAME/(.*?)\n?/(REMI|CSID)/#ms', $this->getCurrentTransactionData(), $results)
106
            && !empty($results[1])
107
        ) {
108
            return trim($results[1]);
109
        }
110
111
        return '';
112
    }
113
114
    /**
115
     * @inheritdoc
116
     */
117
    protected function parseTransactionDescription()
118
    {
119
        $description = parent::parseTransactionDescription();
120
121
        // SEPA MT940 Structured
122
        if (strpos($description, '/REMI/') !== false
123
                && preg_match('#/REMI/(.*)[/:]?#', $description, $results) && !empty($results[1])
124
        ) {
125
            return $results[1];
126
        }
127
128
        $accountIsInDescription = strpos($description, 'REK:');
129
        if ($accountIsInDescription !== false) {
130
            return trim(substr($description, 0, $accountIsInDescription));
131
        }
132
133
        $name = $this->parseTransactionAccountName();
134
        if ($name === '') {
135
            return $description;
136
        }
137
        $accountNameIsInDescription = strpos($description, $name);
138
        if ($accountNameIsInDescription !== false) {
139
            return trim(substr($description, 0, $accountNameIsInDescription-6));
140
        }
141
        return $description;
142
    }
143
144
    /**
145
     * @inheritdoc
146
     */
147
    public static function isApplicable($string)
148
    {
149
        $firstline = strtok($string, "\r\n\t");
150
        return strpos($firstline, 'F01KNABNL2HAXXX0000000000') !== false;
151
    }
152
153
}
154