Completed
Push — knab-fixes ( 36afaf )
by Robin
05:28
created

Knab::parseTransactionAccount()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 11
rs 9.4286
cc 3
eloc 6
nc 2
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
        $pattern = '/^:86:.*?REK:\s*(?<account>' . self::IBAN . '|\d+)/ims';
66
        if (preg_match($pattern, $this->getCurrentTransactionData(), $results)
67
                && !empty($results['account'])
68
        ) {
69
            return $results['account'];
70
        }
71
    }
72
73
    /**
74
     * @inheritdoc
75
     */
76 View Code Duplication
    protected function parseTransactionAccountName()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
77
    {
78
        $results = [];
79
        if (preg_match('/NAAM: (.+)/', $this->getCurrentTransactionData(), $results)
80
                && !empty($results[1])
81
        ) {
82
            return trim($results[1]);
83
        }
84
    }
85
86
    /**
87
     * @inheritdoc
88
     */
89
    protected function parseTransactionDescription()
90
    {
91
        $description = parent::parseTransactionDescription();
92
        $accountIsInDescription = strpos($description, 'REK:');
93
        if ($accountIsInDescription !== false) {
94
            return trim(substr($description, 0, $accountIsInDescription));
95
        }
96
97
        $name = $this->parseTransactionAccountName();
98
        $accountNameIsInDescription = strpos($description, $name);
99
        if ($accountNameIsInDescription !== false) {
100
            return trim(substr($description, 0, $accountNameIsInDescription-6));
101
        }
102
        return $description;
103
    }
104
105
    /**
106
     * @inheritdoc
107
     */
108
    public static function isApplicable($string)
109
    {
110
        $firstline = strtok($string, "\r\n\t");
111
        return strpos($firstline, 'F01KNABNL2HAXXX0000000000') !== false;
112
    }
113
114
}
115