Lines 100.00% 66 / 66
Functions and Methods 100.00% 3 / 3
Classes and Traits 100.00% 1 / 1
Name Lines Functions and Methods CRAP Classes and Traits
VubMailParser 100.00% 66 / 66 100.00% 3 / 3 22 100.00% 1 / 1
 bankDomains 100.00% 1 / 1 100.00% 1 / 1 1
 supports 100.00% 10 / 10 100.00% 1 / 1 6
 parse 100.00% 55 / 55 100.00% 1 / 1 15
1<?php
2
3declare(strict_types=1);
4
5namespace Tomaj\BankMailsParser\Parser\Vub;
6
7use Tomaj\BankMailsParser\Exception\InvalidEmailFormatException;
8use Tomaj\BankMailsParser\MailContent;
9use Tomaj\BankMailsParser\Parser\BankDate;
10use Tomaj\BankMailsParser\Parser\ParserInterface;
11use Tomaj\BankMailsParser\Parser\ParsesBankMail;
12
13final readonly class VubMailParser implements ParserInterface
14{
15    use ParsesBankMail;
16
17    /**
18     * @return list<string>
19     */
20    #[\Override]
21    public function bankDomains(): array
22    {
23        return ['vub.sk'];
24    }
25
26    #[\Override]
27    public function supports(string $content, ?string $sender = null, ?string $subject = null): bool
28    {
29        return $this->supportsQuietly(function () use ($content): bool {
30            $content = $this->withoutByteOrderMark($content);
31            $hasDate = $this->matched($this->field('D(?:á|a)?tum', ':'), $content);
32            $hasSource = $this->matched($this->field('Z[ \t\x{00A0}\x{2009}\x{202F}]*(?:(?:ú|u)?čtu|tu)', ':'), $content);
33            $hasDestination = $this->matched($this->field('Na[ \t\x{00A0}\x{2009}\x{202F}]+(?:účet|et)', ':'), $content);
34            $hasStatus = $this->matched($this->field('(?:Stav|SIGN)', ':'), $content);
35            $hasAmount = $this->matched($this->field('Suma', ':'), $content);
36            $hasVs = $this->matched($this->field('VS', ':'), $content);
37
38            return $hasDate && $hasSource && $hasDestination && $hasStatus && $hasAmount && $hasVs;
39        });
40    }
41
42    #[\Override]
43    public function parse(string $content): MailContent
44    {
45        $content = $this->requireUniformLineBreaks($content);
46        $signals = 0;
47
48        $transactionDate = null;
49        if ($this->matched($this->field('D(?:á|a)?tum:', '(.*)$'), $content, $result)) {
50            ++$signals;
51            $transactionDate = BankDate::parseFirst(mb_trim($this->group($result, 1), " \t\n\r\0\x0B\u{00A0}\u{2009}\u{202F}"), ['!j.n.Y', '!Y-m-d', '!j.n.Y G:i'], BankDate::bratislava());
52        }
53
54        $ownAccountNumber = null;
55        if ($this->matched($this->field('Na[ \t\x{00A0}\x{2009}\x{202F}]+(?:účet|et):', '([A-Z0-9 \x{00A0}\x{2009}\x{202F}\t]*)'), $content, $result)) {
56            $account = $this->normalizeAccount($this->group($result, 1));
57            if ($account !== '') {
58                ++$signals;
59                $ownAccountNumber = $account;
60            }
61        }
62
63        $accountNumber = null;
64        if ($this->matched($this->field('Z[ \t\x{00A0}\x{2009}\x{202F}]*(?:(?:ú|u)?čtu|tu):', '([A-Z0-9 \x{00A0}\x{2009}\x{202F}\t]*)'), $content, $result)) {
65            $account = $this->normalizeAccount($this->group($result, 1));
66            if ($account !== '') {
67                ++$signals;
68                $accountNumber = $account;
69            }
70        }
71
72        $money = null;
73        if ($this->matched($this->field('Suma:', '(.*)$'), $content, $result)) {
74            ++$signals;
75            $raw = trim(str_replace(["\u{00A0}", "\u{2009}", "\u{202F}", "\t"], ' ', $this->group($result, 1)));
76            if ($raw !== '' && preg_match('/^(-?\d(?:[\d \x{00A0}\x{2009}\x{202F}\t.,]*\d)?)(?:[ \t]*([A-Z]{3}))?$/u', $raw, $amount) === 1) {
77                $currency = $amount[2] ?? 'EUR';
78                $money = $this->moneyOrNull($amount[1], $currency, ',');
79            }
80        }
81
82        $vs = null;
83        if ($this->matched($this->field('VS:', '([0-9]+)'), $content, $result)) {
84            ++$signals;
85            $vs = $this->paymentSymbol($this->group($result, 1));
86        }
87
88        $ks = null;
89        if ($this->matched($this->field('KS:', '([0-9]+)'), $content, $result)) {
90            ++$signals;
91            $ks = $this->paymentSymbol($this->group($result, 1));
92        }
93
94        $ss = null;
95        if ($this->matched($this->field('(?:ŠS|SS|S):', '([0-9]+)'), $content, $result)) {
96            ++$signals;
97            $ss = $this->paymentSymbol($this->group($result, 1));
98        }
99
100        $sign = null;
101        if ($this->matched($this->field('SIGN:', '(.*)$'), $content, $result)) {
102            ++$signals;
103            $value = mb_trim($this->group($result, 1), " \t\n\r\0\x0B\u{00A0}\u{2009}\u{202F}");
104            if ($value !== '') {
105                $sign = $value;
106            }
107        }
108
109        if ($signals < 2) {
110            throw InvalidEmailFormatException::malformedContent(self::class, 'No VÚB fields found');
111        }
112
113        return new MailContent(
114            money: $money,
115            transactionDate: $transactionDate,
116            accountNumber: $accountNumber,
117            ownAccountNumber: $ownAccountNumber,
118            vs: $vs,
119            ss: $ss,
120            ks: $ks,
121            sign: $sign,
122        );
123    }
124}