Completed
Push — master ( 2c0bf4...172fae )
by Hannes
02:27
created

PrintingVisitor::beforeAccountNode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 1
1
<?php
2
/**
3
 * This file is part of byrokrat\autogiro.
4
 *
5
 * byrokrat\autogiro is free software: you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License as published
7
 * by the Free Software Foundation, either version 3 of the License, or
8
 * (at your option) any later version.
9
 *
10
 * byrokrat\autogiro is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with byrokrat\autogiro. If not, see <http://www.gnu.org/licenses/>.
17
 *
18
 * Copyright 2016-17 Hannes Forsgård
19
 */
20
21
declare(strict_types = 1);
22
23
namespace byrokrat\autogiro\Writer;
24
25
use byrokrat\autogiro\Visitor\Visitor;
26
use byrokrat\autogiro\Exception\LogicException;
27
use byrokrat\autogiro\Tree\Node;
28
use byrokrat\autogiro\Tree\DateNode;
29
use byrokrat\autogiro\Tree\TextNode;
30
use byrokrat\autogiro\Tree\PayeeBgcNumberNode;
31
use byrokrat\autogiro\Tree\PayeeBankgiroNode;
32
use byrokrat\autogiro\Tree\PayerNumberNode;
33
use byrokrat\autogiro\Tree\AccountNode;
34
use byrokrat\autogiro\Tree\AmountNode;
35
use byrokrat\autogiro\Tree\IntervalNode;
36
use byrokrat\autogiro\Tree\IdNode;
37
use byrokrat\autogiro\Tree\RepetitionsNode;
38
use byrokrat\amount\Currency\SEK;
39
use byrokrat\banking\AccountNumber;
40
use byrokrat\id\Id;
41
use byrokrat\id\PersonalId;
42
use byrokrat\id\OrganizationId;
43
44
/**
45
 * Visitor that generates files to bgc from parse trees
46
 */
47
class PrintingVisitor extends Visitor
48
{
49
    /**
50
     * End-of-line chars used when generating files
51
     */
52
    const EOL = "\r\n";
53
54
    /**
55
     * @var Output
56
     */
57
    private $output;
58
59
    public function setOutput(Output $output)
60
    {
61
        $this->output = $output;
62
    }
63
64
    public function beforeDateNode(DateNode $node)
65
    {
66
        $this->assertAttribute($node, 'date', \DateTimeInterface::CLASS);
67
        $this->output->write($node->getAttribute('date')->format('Ymd'));
68
    }
69
70
    public function beforeImmediateDateNode()
71
    {
72
        $this->output->write('GENAST  ');
73
    }
74
75
    public function beforeTextNode(TextNode $node)
76
    {
77
        $this->output->write($node->getValue());
78
    }
79
80
    public function beforePayeeBgcNumberNode(PayeeBgcNumberNode $node)
81
    {
82
        $this->output->write(str_pad($node->getValue(), 6, '0', STR_PAD_LEFT));
83
    }
84
85
    public function beforePayeeBankgiroNode(PayeeBankgiroNode $node)
86
    {
87
        $this->assertAttribute($node, 'account', AccountNumber::CLASS);
88
        $number = $node->getAttribute('account')->getSerialNumber() . $node->getAttribute('account')->getCheckDigit();
89
        $this->output->write(str_pad($number, 10, '0', STR_PAD_LEFT));
90
    }
91
92
    public function beforePayerNumberNode(PayerNumberNode $node)
93
    {
94
        $this->output->write(str_pad($node->getValue(), 16, '0', STR_PAD_LEFT));
95
    }
96
97
    public function beforeAccountNode(AccountNode $node)
98
    {
99
        $this->assertAttribute($node, 'account', AccountNumber::CLASS);
100
        $number = $node->getAttribute('account')->getSerialNumber() . $node->getAttribute('account')->getCheckDigit();
101
        $this->output->write(
102
            $node->getAttribute('account')->getClearingNumber()
103
            . str_pad($number, 12, '0', STR_PAD_LEFT)
104
        );
105
    }
106
107
    public function beforeIntervalNode(IntervalNode $node)
108
    {
109
        $this->output->write($node->getValue());
110
    }
111
112
    public function beforeRepetitionsNode(RepetitionsNode $node)
113
    {
114
        $this->output->write($node->getValue());
115
    }
116
117
    public function beforeAmountNode(AmountNode $node)
118
    {
119
        $this->assertAttribute($node, 'amount', SEK::CLASS);
120
        $this->output->write(
121
            str_pad($node->getAttribute('amount')->getSignalString(), 12, '0', STR_PAD_LEFT)
122
        );
123
    }
124
125
    public function beforeIdNode(IdNode $node)
126
    {
127
        $this->assertAttribute($node, 'id', Id::CLASS);
128
        if ($node->getAttribute('id') instanceof PersonalId) {
129
            $this->output->write($node->getAttribute('id')->format('Ymdsk'));
130
        }
131
        if ($node->getAttribute('id') instanceof OrganizationId) {
132
            $this->output->write($node->getAttribute('id')->format('00Ssk'));
133
        }
134
    }
135
136
    private function assertAttribute(Node $node, string $attr, string $classname)
137
    {
138
        if (!$node->hasAttribute($attr) || !$node->getAttribute($attr) instanceof $classname) {
139
            throw new LogicException("Failing attribute '$attr' in {$node->getType()}");
140
        }
141
    }
142
143
    public function beforeRequestOpeningRecordNode()
144
    {
145
        $this->output->write('01');
146
    }
147
148
    public function afterRequestOpeningRecordNode()
149
    {
150
        $this->output->write(self::EOL);
151
    }
152
153
    public function beforeCreateMandateRequestNode()
154
    {
155
        $this->output->write('04');
156
    }
157
158
    public function afterCreateMandateRequestNode()
159
    {
160
        $this->output->write(self::EOL);
161
    }
162
163
    public function beforeDeleteMandateRequestNode()
164
    {
165
        $this->output->write('03');
166
    }
167
168
    public function afterDeleteMandateRequestNode()
169
    {
170
        $this->output->write(self::EOL);
171
    }
172
}
173