-
Notifications
You must be signed in to change notification settings - Fork 0
/
Parser.php
150 lines (120 loc) · 3.66 KB
/
Parser.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<?php
namespace SieParser;
use Exception;
use SieParser\data\Transaction;
use SieParser\data\Voucher;
use SieParser\types\Fnamn;
use SieParser\types\Ib;
use SieParser\types\Konto;
use SieParser\types\Orgnr;
use SieParser\types\ParseType;
use SieParser\types\Rar;
use SieParser\types\Res;
use SieParser\types\Ub;
class Parser
{
private \SplFileObject $file;
/**
* @throws Exception
*/
public function __construct($file)
{
$this->file = new \SplFileObject($file);
if (!$this->validateFile()) {
throw new Exception('Not a valid SIE file.');
}
}
public function getOrgnr(): array
{
return $this->parse(new Orgnr());
}
public function getFnamn(): array
{
return $this->parse(new Fnamn());
}
public function getRar(): array
{
return $this->parse(new Rar());
}
public function getIb(): array
{
return $this->parse(new Ib());
}
public function getUb(): array
{
return $this->parse(new Ub());
}
public function getRes(): array
{
return $this->parse(new Res());
}
public function getVer(): array
{
return $this->parseVouchers();
}
public function getKonto(): array
{
return $this->parse(new Konto());
}
function parse(ParseType $parseType): array
{
$parseResult = [];
$this->file->setFlags(\SplFileObject::DROP_NEW_LINE);
while(!$this->file->eof()) {
if (str_contains($this->file->fgets(), $parseType->getFlag())) {
$line = explode(" ", $this->file->current());
$parseResult[] = $parseType->parse($line);
}
}
$this->file->rewind();
return $parseResult;
}
function parseVouchers(): array
{
$vouchers = [];
$this->file->setFlags(\SplFileObject::DROP_NEW_LINE);
while(!$this->file->eof()) {
if (str_contains($this->file->fgets(), "#VER")) {
$line = $this->file->current();
$splitLine = explode(' ', $line);
// build voucherText
$voucherText = "";
for ($i = 0; $i < count($splitLine)-1; $i++) {
if ($i > 3) {
$voucherText .= $splitLine[$i] . " ";
}
}
$voucher = new Voucher($splitLine[1], $splitLine[2], $splitLine[3], $voucherText);
// get voucher rows ("#TRANS")
$voucherRows = [];
while($this->file->current() != '}') {
if (str_starts_with(trim($this->file->current()), "#TRANS")) {
$split = explode(" ", $this->file->current());
$split = array_values(array_filter($split));
$amountPos = 0;
foreach ($split as $key => $splitLine) {
if (str_contains($splitLine, "}")) {
$amountPos = $key + 1;
}
}
$voucherRow = new Transaction($split[1], $split[$amountPos]);
$voucherRows[] = $voucherRow;
}
$this->file->next();
}
$voucher->trans = $voucherRows;
$vouchers[] = $voucher;
}
}
$this->file->rewind();
return $vouchers;
}
public function validateFile(): bool
{
if (str_contains($this->file->getCurrentLine(), "#FLAGGA")) {
return true;
} else {
return false;
}
}
}