Completed
Pull Request — master (#38)
by Robin
01:56
created

Knab::isApplicable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
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
81
    /**
82
     * @inheritdoc
83
     */
84
    protected function parseTransactionAccountName()
85
    {
86
        $results = [];
87
88
        // SEPA MT940 Structured
89 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...
90
                && !empty($results[1])
91
        ) {
92
            $accountName = trim($results[1]);
93
            if (!empty($accountName)) {
94
                return $this->sanitizeAccountName($accountName);
95
            }
96
        }
97
98
        if (preg_match('/NAAM: (.+)/', $this->getCurrentTransactionData(), $results)
99
                && !empty($results[1])
100
        ) {
101
            return trim($results[1]);
102
        }
103
    }
104
105
    /**
106
     * @inheritdoc
107
     */
108
    protected function parseTransactionDescription()
109
    {
110
        $description = parent::parseTransactionDescription();
111
112
        // SEPA MT940 Structured
113
        if (strpos($description, '/REMI/') !== false
114
                && preg_match('#/REMI/(.*)[/:]?#', $description, $results) && !empty($results[1])
115
        ) {
116
            return $results[1];
117
        }
118
119
        $accountIsInDescription = strpos($description, 'REK:');
120
        if ($accountIsInDescription !== false) {
121
            return trim(substr($description, 0, $accountIsInDescription));
122
        }
123
124
        $name = $this->parseTransactionAccountName();
125
        $accountNameIsInDescription = strpos($description, $name);
126
        if ($accountNameIsInDescription !== false) {
127
            return trim(substr($description, 0, $accountNameIsInDescription-6));
128
        }
129
        return $description;
130
    }
131
132
    /**
133
     * @inheritdoc
134
     */
135
    public static function isApplicable($string)
136
    {
137
        $firstline = strtok($string, "\r\n\t");
138
        return strpos($firstline, 'F01KNABNL2HAXXX0000000000') !== false;
139
    }
140
141
}
142