1 | <?php |
||
2 | |||
3 | namespace Fns; |
||
4 | |||
5 | use Carbon\Carbon; |
||
6 | |||
7 | class TransformQrCodeToTicket |
||
8 | { |
||
9 | private $ticket; |
||
10 | private $qrcodeString; |
||
11 | |||
12 | private $regexToTicketMethod = [ |
||
13 | '/(i\=)([0-9]+)/' => [ |
||
14 | 'method' => 'setFiscalDocumentId', |
||
15 | 'type' => 'getInteger' |
||
16 | ], |
||
17 | '/(fp\=)([0-9]+)/' => [ |
||
18 | 'method' => 'setFiscalSign', |
||
19 | 'type' => 'getInteger' |
||
20 | ], |
||
21 | '/(fn\=)([0-9]+)/' => [ |
||
22 | 'method' => 'setFn', |
||
23 | 'type' => 'getInteger' |
||
24 | ], |
||
25 | '/(s\=)(([0-9]+\.?[0-9]+)|[0-9]+)/' => [ |
||
26 | 'method' => 'setSum', |
||
27 | 'filter' => 'aroundSum', |
||
28 | 'type' => 'getInteger' |
||
29 | ], |
||
30 | '/(t\=)([0-9]+\T[0-9]+)/' => [ |
||
31 | 'method' => 'setDate', |
||
32 | 'filter' => 'toDateTimeLocalString', |
||
33 | 'type' => 'getString' |
||
34 | ], |
||
35 | '/(n\=)([0-9]+)$/' => [ |
||
36 | 'method' => 'setTypeOperation', |
||
37 | 'type' => 'getInteger' |
||
38 | ], |
||
39 | |||
40 | ]; |
||
41 | |||
42 | public function __construct(Ticket $ticket) |
||
43 | { |
||
44 | $this->ticket = $ticket; |
||
45 | } |
||
46 | |||
47 | public function setQrCode(string $qrcode) |
||
48 | { |
||
49 | $this->qrcodeString = $qrcode; |
||
50 | } |
||
51 | |||
52 | public function getTicket(): Ticket |
||
53 | { |
||
54 | foreach ($this->regexToTicketMethod as $regex => $config) { |
||
55 | $temp = []; |
||
56 | |||
57 | preg_match($regex, $this->qrcodeString, $temp); |
||
58 | if (isset($config['filter'])) { |
||
59 | $value = $this->{$config['filter']}(array_pop($temp)); |
||
60 | } else { |
||
61 | $value = array_pop($temp); |
||
62 | } |
||
63 | $normalizeType = $this->{$config['type']}($value); |
||
64 | $this->ticket->{$config['method']}($normalizeType); |
||
65 | } |
||
66 | return $this->ticket; |
||
67 | } |
||
68 | |||
69 | private function aroundSum(string $sum): int |
||
70 | { |
||
71 | return round(($sum * 100), 0); |
||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
![]() |
|||
72 | } |
||
73 | |||
74 | private function toDateTimeLocalString(string $date): string |
||
75 | { |
||
76 | return Carbon::parse($date)->toDateTimeLocalString(); |
||
77 | } |
||
78 | |||
79 | private function getInteger($value): int |
||
80 | { |
||
81 | return (int) $value; |
||
82 | } |
||
83 | |||
84 | private function getString($value): string |
||
85 | { |
||
86 | return (string) $value; |
||
87 | } |
||
88 | } |
||
89 |