Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
24 / 24 |
|
100.00% |
1 / 1 |
CRAP | |
100.00% |
1 / 1 |
| VubMailParser | |
100.00% |
24 / 24 |
|
100.00% |
1 / 1 |
6 | |
100.00% |
1 / 1 |
| parse | |
100.00% |
24 / 24 |
|
100.00% |
1 / 1 |
6 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Tomaj\BankMailsParser\Parser\Vub; |
| 6 | |
| 7 | use Tomaj\BankMailsParser\MailContent; |
| 8 | use Tomaj\BankMailsParser\Parser\ParserInterface; |
| 9 | |
| 10 | class VubMailParser implements ParserInterface |
| 11 | { |
| 12 | public function parse(string $content): ?MailContent |
| 13 | { |
| 14 | $mailContent = new MailContent(); |
| 15 | |
| 16 | $pattern1 = '/Dtum:.*?(.*)/m'; |
| 17 | $res = preg_match($pattern1, $content, $result); |
| 18 | if ($res) { |
| 19 | $mailContent->setTransactionDate(strtotime(trim($result[1]))); |
| 20 | } |
| 21 | |
| 22 | $pattern2 = '/Z tu:.*?([A-Z0-9]+)/m'; |
| 23 | $res = preg_match($pattern2, $content, $result); |
| 24 | if ($res) { |
| 25 | $iban = trim($result[1]); |
| 26 | $mailContent->setAccountNumber($iban); |
| 27 | } |
| 28 | |
| 29 | $pattern3 = '/Suma:.*?([0-9,]+)/m'; |
| 30 | $res = preg_match($pattern3, $content, $result); |
| 31 | if ($res) { |
| 32 | // there's unicode non-breaking space (u00A0) in mime encoded version of email, unicode regex switched is necessary |
| 33 | $amount = floatval(str_replace(',', '.', preg_replace('/\s+/u', '', $result[1]))); |
| 34 | $mailContent->setAmount($amount); |
| 35 | } |
| 36 | |
| 37 | $pattern4 = '/VS:.*?([0-9,]+)/m'; |
| 38 | $res = preg_match($pattern4, $content, $result); |
| 39 | if ($res) { |
| 40 | $mailContent->setVs($result[1]); |
| 41 | } |
| 42 | |
| 43 | $pattern5 = '/KS:.*?([0-9,]+)/m'; |
| 44 | $res = preg_match($pattern5, $content, $result); |
| 45 | if ($res) { |
| 46 | $mailContent->setKs($result[1]); |
| 47 | } |
| 48 | |
| 49 | return $mailContent; |
| 50 | } |
| 51 | } |