Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
91.67% |
22 / 24 |
|
0.00% |
0 / 1 |
CRAP | |
0.00% |
0 / 1 |
TatraBankaSimpleMailParser | |
91.67% |
22 / 24 |
|
0.00% |
0 / 1 |
10.06 | |
0.00% |
0 / 1 |
parse | |
91.67% |
22 / 24 |
|
0.00% |
0 / 1 |
10.06 |
1 | <?php |
2 | declare(strict_types=1); |
3 | |
4 | namespace Tomaj\BankMailsParser\Parser\TatraBanka; |
5 | |
6 | use ReflectionParameter; |
7 | use Tomaj\BankMailsParser\MailContent; |
8 | use Tomaj\BankMailsParser\Parser\ParserInterface; |
9 | |
10 | class TatraBankaSimpleMailParser implements ParserInterface |
11 | { |
12 | private $map = [ |
13 | 'VS' => 'setVs', |
14 | 'RES' => 'setRes', |
15 | 'AC' => 'setAc', |
16 | 'SIGN' => 'setSign', |
17 | 'TRES' => 'setRes', |
18 | 'CID' => 'setCid', |
19 | 'AMT' => 'setAmount', |
20 | 'CURR' => 'setCurrency', |
21 | 'CC' => 'setCc', |
22 | 'TID' => 'setTid', |
23 | 'TIMESTAMP' => 'setTransactionDate', |
24 | 'TXN' => 'setTxn', |
25 | 'RC' => 'setRc', |
26 | 'HMAC' => 'setSign', |
27 | ]; |
28 | |
29 | /** |
30 | * @param $content |
31 | * @return ?MailContent |
32 | */ |
33 | public function parse(string $content): ?MailContent |
34 | { |
35 | $mailContent = new MailContent(); |
36 | |
37 | if (empty($content)) { |
38 | return null; |
39 | } |
40 | |
41 | foreach (explode(' ', $content) as $part) { |
42 | [$key, $value] = array_map('trim', explode('=', $part)); |
43 | |
44 | if (!isset($this->map[$key])) { |
45 | continue; |
46 | } |
47 | |
48 | $method = $this->map[$key]; |
49 | |
50 | $param = new ReflectionParameter([MailContent::class, $method], 0); |
51 | if ($param->getType()) { |
52 | $type = $param->getType()->getName(); |
53 | if ($type == 'string') { |
54 | $mailContent->$method($value); |
55 | } elseif ($type == 'int') { |
56 | $mailContent->$method(intval($value)); |
57 | } elseif ($type == 'float') { |
58 | $mailContent->$method(floatval($value)); |
59 | } else { |
60 | $mailContent->$method($value); |
61 | } |
62 | } else { |
63 | $mailContent->$method($value); |
64 | } |
65 | } |
66 | |
67 | if ($mailContent->getRes() === null) { |
68 | return null; |
69 | } |
70 | |
71 | if ($mailContent->getTransactionDate() === null) { |
72 | $mailContent->setTransactionDate(time()); |
73 | } |
74 | return $mailContent; |
75 | } |
76 | } |