Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
95.00% |
38 / 40 |
|
50.00% |
1 / 2 |
CRAP | |
0.00% |
0 / 1 |
SkCsobMailParser | |
95.00% |
38 / 40 |
|
50.00% |
1 / 2 |
11 | |
0.00% |
0 / 1 |
parseMulti | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
3 | |||
parse | |
93.94% |
31 / 33 |
|
0.00% |
0 / 1 |
8.01 |
1 | <?php |
2 | declare(strict_types=1); |
3 | |
4 | namespace Tomaj\BankMailsParser\Parser\Csob; |
5 | |
6 | use Tomaj\BankMailsParser\MailContent; |
7 | use Tomaj\BankMailsParser\Parser\ParserInterface; |
8 | |
9 | class SkCsobMailParser implements ParserInterface |
10 | { |
11 | /** |
12 | * @return MailContent[] |
13 | */ |
14 | public function parseMulti(string $content): array |
15 | { |
16 | $transactions = array_slice(explode("dňa ", $content), 1); |
17 | |
18 | $mailContents = []; |
19 | foreach ($transactions as $transaction) { |
20 | $mailContent = $this->parse($transaction); |
21 | if ($mailContent !== null) { |
22 | $mailContents[] = $mailContent; |
23 | } |
24 | } |
25 | |
26 | return $mailContents; |
27 | } |
28 | |
29 | public function parse(string $content): ?MailContent |
30 | { |
31 | $mailContent = new MailContent(); |
32 | |
33 | $pattern1 = '/(.*) bola na účte (.*) zaúčtovaná suma SEPA platobného príkazu/m'; |
34 | $res = preg_match($pattern1, $content, $result); |
35 | if (!$res) { |
36 | return null; |
37 | } |
38 | |
39 | $mailContent->setTransactionDate(strtotime($result[1])); |
40 | |
41 | $pattern2 = '/suma:.*?([+-])(.*?) ([A-Z]+)/m'; |
42 | $res = preg_match($pattern2, $content, $result); |
43 | if ($res) { |
44 | // there's unicode non-breaking space (u00A0) in mime encoded version of email, unicode regex switched is necessary |
45 | $amount = floatval(str_replace(',', '.', preg_replace('/\s+/u', '', $result[2]))); |
46 | $currency = $result[3]; |
47 | if ($result[1] === '-') { |
48 | $amount = -$amount; |
49 | } |
50 | $mailContent->setAmount($amount); |
51 | $mailContent->setCurrency($currency); |
52 | } |
53 | |
54 | $pattern3 = '/informácia pre príjemcu: (.*)/m'; |
55 | $res = preg_match($pattern3, $content, $result); |
56 | if ($res) { |
57 | $mailContent->setReceiverMessage(trim($result[1])); |
58 | } |
59 | |
60 | $pattern4 = '/VS([0-9]+)/m'; |
61 | $res = preg_match($pattern4, $content, $result); |
62 | if ($res) { |
63 | $mailContent->setVs($result[1]); |
64 | } |
65 | |
66 | $pattern5 = '/KS([0-9]+)/m'; |
67 | $res = preg_match($pattern5, $content, $result); |
68 | if ($res) { |
69 | $mailContent->setKs($result[1]); |
70 | } |
71 | |
72 | $pattern6 = '/z účtu:.*?([A-Z0-9 ]+)/m'; |
73 | $res = preg_match($pattern6, $content, $result); |
74 | if ($res) { |
75 | $iban = trim($result[1]); |
76 | $mailContent->setAccountNumber($iban); |
77 | } |
78 | |
79 | return $mailContent; |
80 | } |
81 | } |