|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Claudsonm\BoletoWinner\Factories; |
|
4
|
|
|
|
|
5
|
|
|
use Claudsonm\BoletoWinner\Bill; |
|
6
|
|
|
use Claudsonm\BoletoWinner\Boleto; |
|
7
|
|
|
use Claudsonm\BoletoWinner\Convenio; |
|
8
|
|
|
use Claudsonm\BoletoWinner\Exceptions\BoletoWinnerException; |
|
9
|
|
|
|
|
10
|
|
|
class BillFactory |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* Bills supported. |
|
14
|
|
|
* |
|
15
|
|
|
* @var array|string[] |
|
16
|
|
|
*/ |
|
17
|
|
|
protected array $bills = [ |
|
18
|
|
|
'boleto' => Boleto::class, |
|
19
|
|
|
'convenio' => Convenio::class, |
|
20
|
|
|
]; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* The singleton instance. |
|
24
|
|
|
*/ |
|
25
|
|
|
private static ?BillFactory $instance = null; |
|
26
|
|
|
|
|
27
|
1 |
|
protected function __construct() |
|
28
|
|
|
{ |
|
29
|
|
|
// Preventing direct construction calls with the `new` operator. |
|
30
|
1 |
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Get the singleton class instance or creates one if it's not set yet. |
|
34
|
|
|
*/ |
|
35
|
31 |
|
public static function getInstance(): self |
|
36
|
|
|
{ |
|
37
|
31 |
|
if (self::$instance) { |
|
38
|
31 |
|
return self::$instance; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
1 |
|
self::$instance = new static(); |
|
42
|
|
|
|
|
43
|
1 |
|
return self::$instance; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Returns an instance of the Bill class for the given type. |
|
48
|
|
|
* |
|
49
|
|
|
* @throws BoletoWinnerException |
|
50
|
|
|
*/ |
|
51
|
4 |
|
public function createBillInstance(string $type): Bill |
|
52
|
|
|
{ |
|
53
|
4 |
|
if (! isset($this->bills[$type])) { |
|
54
|
1 |
|
throw BoletoWinnerException::unsupportedType($type); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
3 |
|
return new $this->bills[$type](); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @throws BoletoWinnerException |
|
62
|
|
|
*/ |
|
63
|
22 |
|
public function createFromBarcodeOrWritableLine(string $barcodeOrWritableLine): Bill |
|
64
|
|
|
{ |
|
65
|
22 |
|
$input = $this->sanitizeInput($barcodeOrWritableLine); |
|
66
|
20 |
|
foreach ($this->bills as $billClass) { |
|
67
|
20 |
|
$bill = new $billClass(); |
|
68
|
20 |
|
$bill->setBarcode($input); |
|
69
|
20 |
|
if ($bill->isBarcodeValid()) { |
|
70
|
4 |
|
return $bill; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
18 |
|
$bill = new $billClass(); |
|
74
|
18 |
|
$bill->setWritableLine($input); |
|
75
|
18 |
|
if ($bill->isWritableLineValid()) { |
|
76
|
7 |
|
return $bill; |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
9 |
|
throw BoletoWinnerException::invalidInput($barcodeOrWritableLine); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @throws BoletoWinnerException |
|
85
|
|
|
*/ |
|
86
|
2 |
|
public function createFromWritableLine(string $writableLine): Bill |
|
87
|
|
|
{ |
|
88
|
2 |
|
$input = $this->sanitizeInput($writableLine); |
|
89
|
2 |
|
foreach ($this->bills as $billClass) { |
|
90
|
2 |
|
$bill = new $billClass(); |
|
91
|
2 |
|
$bill->setWritableLine($input); |
|
92
|
2 |
|
if ($bill->isWritableLineValid()) { |
|
93
|
2 |
|
return $bill; |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
1 |
|
throw BoletoWinnerException::invalidInput($writableLine); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @throws BoletoWinnerException |
|
102
|
|
|
*/ |
|
103
|
2 |
|
public function createFromBarcode(string $barcode): Bill |
|
104
|
|
|
{ |
|
105
|
2 |
|
$input = $this->sanitizeInput($barcode); |
|
106
|
2 |
|
foreach ($this->bills as $billClass) { |
|
107
|
2 |
|
$bill = new $billClass(); |
|
108
|
2 |
|
$bill->setBarcode($input); |
|
109
|
2 |
|
if ($bill->isBarcodeValid()) { |
|
110
|
2 |
|
return $bill; |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
1 |
|
throw BoletoWinnerException::invalidInput($barcode); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* @throws BoletoWinnerException |
|
119
|
|
|
*/ |
|
120
|
26 |
|
private function sanitizeInput(string $barcodeOrWritableLine): string |
|
121
|
|
|
{ |
|
122
|
26 |
|
$input = preg_replace('/[^0-9]/', '', $barcodeOrWritableLine); |
|
123
|
26 |
|
if (empty($input)) { |
|
124
|
2 |
|
throw BoletoWinnerException::inputRequired(); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
24 |
|
return $input; |
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
|
|
|