Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
46 / 46 |
|
100.00% |
1 / 1 |
CRAP | |
100.00% |
1 / 1 |
| TatraBankaMailParser | |
100.00% |
46 / 46 |
|
100.00% |
1 / 1 |
13 | |
100.00% |
1 / 1 |
| parse | |
100.00% |
46 / 46 |
|
100.00% |
1 / 1 |
13 | |||
| 1 | <?php |
| 2 | declare(strict_types=1); |
| 3 | |
| 4 | namespace Tomaj\BankMailsParser\Parser\TatraBanka; |
| 5 | |
| 6 | use Tomaj\BankMailsParser\MailContent; |
| 7 | use Tomaj\BankMailsParser\Parser\ParserInterface; |
| 8 | |
| 9 | class TatraBankaMailParser implements ParserInterface |
| 10 | { |
| 11 | /** |
| 12 | * @param $content |
| 13 | * @return ?MailContent |
| 14 | */ |
| 15 | public function parse(string $content): ?MailContent |
| 16 | { |
| 17 | $mailContent = new MailContent(); |
| 18 | |
| 19 | $pattern1 = '/(.*) bol zostatok Vasho uctu ([a-zA-Z0-9]+) (zvyseny|znizeny) o ([0-9 ]+,[0-9]+) ([a-zA-Z]+)/m'; |
| 20 | $res = preg_match($pattern1, $content, $result); |
| 21 | if (!$res) { |
| 22 | return null; |
| 23 | } |
| 24 | |
| 25 | $mailContent->setTransactionDate(strtotime($result[1])); |
| 26 | $mailContent->setAccountNumber($result[2]); |
| 27 | |
| 28 | $amount = floatval(str_replace(',', '.', str_replace(' ', '', $result[4]))); |
| 29 | $currency = $result[5]; |
| 30 | if ($result[3] === 'znizeny') { |
| 31 | $amount = -$amount; |
| 32 | } |
| 33 | $mailContent->setAmount($amount); |
| 34 | $mailContent->setCurrency($currency); |
| 35 | |
| 36 | $pattern = '/Informacia pre prijemcu: (.*)/m'; |
| 37 | $res = preg_match($pattern, $content, $result); |
| 38 | if ($res) { |
| 39 | $mailContent->setReceiverMessage($result[1]); |
| 40 | } |
| 41 | |
| 42 | // loads VS provided in format: |
| 43 | // - Referencia platitela: /VS1234056789/SS/KS |
| 44 | $pattern = '/Referencia platitela: \/VS(.*)\/SS(.*)\/KS(.*)/m'; |
| 45 | $res = preg_match($pattern, $content, $result); |
| 46 | if ($res) { |
| 47 | $mailContent->setVs($result[1]); |
| 48 | $mailContent->setSs($result[2]); |
| 49 | $mailContent->setKs($result[3]); |
| 50 | } |
| 51 | |
| 52 | // search whole email for number with `vs` prefix |
| 53 | if ($mailContent->getVs() === null) { |
| 54 | $pattern = '/vs([0-9]{1,10})/i'; |
| 55 | $res = preg_match($pattern, $content, $result); |
| 56 | if ($res) { |
| 57 | $mailContent->setVs($result[1]); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | // if still no number found, check receiver message |
| 62 | // - some payers incorrectly set this field with VS number but without "VS" prefix |
| 63 | // - some banks send here variable symbol in Creditor Reference Information - SEPA XML format |
| 64 | // loads VS provided in formats: |
| 65 | // - Informacia pre prijemcu: 1234056789 |
| 66 | // - Informacia pre prijemcu: (CdtrRefInf)(Tp)(CdOrPrtry)(Cd)SCOR(/Cd)(/CdOrPrtry)(/Tp)(Ref)1234056789(/Ref)(/CdtrRefInf) |
| 67 | if ($mailContent->getVs() === null) { |
| 68 | $pattern = '/Informacia pre prijemcu:.*\b([0-9]{1,10})\b.*/i'; |
| 69 | $res = preg_match($pattern, $content, $result); |
| 70 | if ($res) { |
| 71 | $mailContent->setVs($result[1]); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | // if still no number found, check unique mandate reference |
| 76 | // some payers incorrectly set this field without correct prefixes /VS/SS/KS |
| 77 | // loads VS provided in format: |
| 78 | // - Referencia platitela: 1234056789 |
| 79 | if ($mailContent->getVs() === null) { |
| 80 | $pattern = '/Referencia platitela:.*\b([0-9]{1,10})\b.*/i'; |
| 81 | $res = preg_match($pattern, $content, $result); |
| 82 | if ($res) { |
| 83 | $mailContent->setVs($result[1]); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | $pattern4 = '/Popis transakcie: (.*)/m'; |
| 88 | $res = preg_match($pattern4, $content, $result); |
| 89 | if ($res) { |
| 90 | $mailContent->setDescription($result[1]); |
| 91 | |
| 92 | $descriptionParts = explode(' ', $result[1], 2); |
| 93 | $hasPrefix = count($descriptionParts) === 2; |
| 94 | $mailContent->setSourceAccountNumber($descriptionParts[$hasPrefix ? 1 : 0]); |
| 95 | } |
| 96 | |
| 97 | return $mailContent; |
| 98 | } |
| 99 | } |