Lines 100.00% 67 / 67
Functions and Methods 100.00% 3 / 3
Classes and Traits 100.00% 1 / 1
Name Lines Functions and Methods CRAP Classes and Traits
TatraBankaMailParser 100.00% 67 / 67 100.00% 3 / 3 21 100.00% 1 / 1
 bankDomains 100.00% 1 / 1 100.00% 1 / 1 1
 supports 100.00% 8 / 8 100.00% 1 / 1 6
 parse 100.00% 58 / 58 100.00% 1 / 1 14
1<?php
2
3declare(strict_types=1);
4
5namespace Tomaj\BankMailsParser\Parser\TatraBanka;
6
7use Tomaj\BankMailsParser\Exception\InvalidEmailFormatException;
8use Tomaj\BankMailsParser\MailContent;
9use Tomaj\BankMailsParser\Parser\BankDate;
10use Tomaj\BankMailsParser\Parser\MailWhitespace;
11use Tomaj\BankMailsParser\Parser\ParserInterface;
12use Tomaj\BankMailsParser\Parser\ParsesBankMail;
13
14final readonly class TatraBankaMailParser implements ParserInterface
15{
16    use ParsesBankMail;
17
18    private const string TRANSACTION = '/^' . MailWhitespace::WS . '(\d{1,2}\.[ \t\x{00A0}\x{2009}\x{202F}]*\d{1,2}\.[ \t\x{00A0}\x{2009}\x{202F}]*\d{2,4}(?:[ \t\x{00A0}\x{2009}\x{202F}]+\d{1,2}:\d{2}(?::\d{2})?)?|\d{4}-\d{2}-\d{2}(?:[ \t\x{00A0}\x{2009}\x{202F}]+\d{1,2}:\d{2}(?::\d{2})?)?|\d{1,2}[ \t\x{00A0}\x{2009}\x{202F}]+[A-Za-z]+[ \t\x{00A0}\x{2009}\x{202F}]+\d{4}(?:[ \t\x{00A0}\x{2009}\x{202F}]+\d{1,2}:\d{2}(?::\d{2})?)?)[ \t\x{00A0}\x{2009}\x{202F}]+bol zostatok Vasho uctu ([A-Za-z0-9]+) (zvyseny|znizeny) o ([0-9 \x{00A0}\x{2009}\x{202F}\t]+,[0-9]+) ([A-Za-z]+)/mu';
19
20    /**
21     * @return list<string>
22     */
23    #[\Override]
24    public function bankDomains(): array
25    {
26        return ['tatrabanka.sk'];
27    }
28
29    #[\Override]
30    public function supports(string $content, ?string $sender = null, ?string $subject = null): bool
31    {
32        return $this->supportsQuietly(function () use ($content, $sender, $subject): bool {
33            $content = $this->withoutFreeText($this->scrubUtf8($this->withoutByteOrderMark($content)));
34            if ($this->matched(self::TRANSACTION, $content)) {
35                return true;
36            }
37
38            $senderMatches = $sender !== null && $this->senderDomain($sender, 'tatrabanka.sk');
39            $subjectMatches = $subject !== null && str_contains(strtolower($subject), 'tatra');
40
41            return ($senderMatches || $subjectMatches) && str_contains($content, 'zostatok Vasho uctu');
42        });
43    }
44
45    #[\Override]
46    public function parse(string $content): MailContent
47    {
48        $original = $this->scrubUtf8($this->requireUniformLineBreaks($content));
49        $description = $this->lineValue($original, 'Popis transakcie');
50        $receiverMessage = $this->lineValue($original, 'Informacia pre prijemcu');
51        $sourceAccountNumber = null;
52        if ($description !== null) {
53            $descriptionParts = explode(' ', $description, 2);
54            $sourceAccountNumber = count($descriptionParts) === 2 ? $descriptionParts[1] : $descriptionParts[0];
55        }
56
57        $content = $this->withoutFreeText($original);
58        if (!$this->matched(self::TRANSACTION, $content, $matches)) {
59            throw InvalidEmailFormatException::malformedContent(
60                self::class,
61                'Could not find required transaction pattern',
62            );
63        }
64
65        $rawDate = trim($this->group($matches, 1));
66        $transactionDate = BankDate::parseFirst($rawDate, [
67            '!j. n. Y G:i:s',
68            '!j.n.Y G:i:s',
69            '!j. n. Y G:i',
70            '!j.n.Y G:i',
71            '!Y-m-d G:i:s',
72            '!Y-m-d H:i:s',
73            '!Y-m-d G:i',
74            '!Y-m-d H:i',
75            '!j. n. Y',
76            '!j.n.Y',
77            '!Y-m-d',
78        ], BankDate::bratislava());
79        if (!$transactionDate instanceof \DateTimeImmutable && preg_match('/[A-Za-z]/', $rawDate) === 1) {
80            $transactionDate = BankDate::parseEnglish($rawDate, BankDate::bratislava());
81        }
82
83        $accountNumber = $this->group($matches, 2);
84        $amount = $this->group($matches, 4);
85        if ($this->group($matches, 3) === 'znizeny') {
86            $amount = '-' . $amount;
87        }
88
89        $money = $this->money($amount, $this->group($matches, 5), ',');
90
91        $vs = null;
92        $ss = null;
93        $ks = null;
94        if ($this->matched($this->field('Referencia platitela:', '\/VS(\d*)\/SS(\d*)\/KS(\d*)'), $original, $refMatches)) {
95            $vs = $this->paymentSymbol($this->group($refMatches, 1), null);
96            $ss = $this->paymentSymbol($this->group($refMatches, 2), null);
97            $ks = $this->paymentSymbol($this->group($refMatches, 3), null);
98        }
99
100        // 4.1.0 fallbacks. They read payer text and never the amount, account, or date.
101        if ($vs === null && $this->matched('/vs([0-9]{1,10})(?!\d)/i', $original, $prefixed)) {
102            $vs = $this->group($prefixed, 1);
103        }
104        if ($vs === null && $this->matched('/Informacia pre prijemcu:.*\b([0-9]{1,10})\b.*/i', $original, $fromMessage)) {
105            $vs = $this->group($fromMessage, 1);
106        }
107        if ($vs === null && $this->matched('/Referencia platitela:.*\b([0-9]{1,10})\b.*/i', $original, $fromReference)) {
108            $vs = $this->group($fromReference, 1);
109        }
110
111        return new MailContent(
112            money: $money,
113            transactionDate: $transactionDate,
114            accountNumber: $accountNumber,
115            sourceAccountNumber: $sourceAccountNumber,
116            vs: $vs,
117            ss: $ss,
118            ks: $ks,
119            receiverMessage: $receiverMessage,
120            description: $description,
121        );
122    }
123}