|
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-18 Hannes Forsgård |
|
19
|
|
|
*/ |
|
20
|
|
|
|
|
21
|
|
|
declare(strict_types = 1); |
|
22
|
|
|
|
|
23
|
|
|
namespace byrokrat\autogiro\Visitor; |
|
24
|
|
|
|
|
25
|
|
|
use byrokrat\autogiro\Tree\Node; |
|
26
|
|
|
use byrokrat\autogiro\Tree\Obj; |
|
27
|
|
|
use byrokrat\amount\Currency\SEK; |
|
28
|
|
|
use byrokrat\amount\Exception as AmountException; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Create amount object under child 'Object' |
|
32
|
|
|
*/ |
|
33
|
|
|
final class AmountVisitor extends Visitor |
|
34
|
|
|
{ |
|
35
|
|
|
use ErrorAwareTrait; |
|
36
|
|
|
|
|
37
|
|
|
public function beforeAmount(Node $node): void |
|
38
|
|
|
{ |
|
39
|
|
|
if ($node->hasChild('Object')) { |
|
40
|
|
|
return; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
$signalStr = (string)$node->getValueFrom('Text'); |
|
44
|
|
|
|
|
45
|
|
|
if (trim($signalStr) == '') { |
|
46
|
|
|
return; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
try { |
|
50
|
|
|
$invertSign = false; |
|
51
|
|
|
|
|
52
|
|
|
// due to charset issues unknown trailing signal chars are treated as 'å' |
|
53
|
|
|
if (!preg_match('/^[0-9åJKLMNOPQR]$/', mb_substr($signalStr, -1))) { |
|
54
|
|
|
$signalStr = mb_substr($signalStr, 0, -1) . '0'; |
|
55
|
|
|
$invertSign = true; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
$object = SEK::createFromSignalString($signalStr); |
|
59
|
|
|
|
|
60
|
|
|
if ($invertSign) { |
|
61
|
|
|
$object = $object->getInverted(); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
$node->addChild(new Obj($node->getLineNr(), $object)); |
|
65
|
|
|
} catch (AmountException $e) { |
|
66
|
|
|
$this->getErrorObject()->addError( |
|
67
|
|
|
"Invalid signaled amount %s on line %s", |
|
68
|
|
|
(string)$node->getValueFrom('Text'), |
|
69
|
|
|
(string)$node->getLineNr() |
|
70
|
|
|
); |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|