1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Claudsonm\BoletoWinner; |
4
|
|
|
|
5
|
|
|
use BadMethodCallException; |
6
|
|
|
use Claudsonm\BoletoWinner\Exceptions\BoletoWinnerException; |
7
|
|
|
use Claudsonm\BoletoWinner\Factories\BillFactory; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @method static bool isValidBoleto(string $barcodeOrWritableLine) |
11
|
|
|
* @method static bool isValidConvenio(string $barcodeOrWritableLine) |
12
|
|
|
* |
13
|
|
|
* @see BillFactory |
14
|
|
|
*/ |
15
|
|
|
class BoletoWinner |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* Handle dynamic, static calls to the object. |
19
|
|
|
* |
20
|
|
|
* @param string $method |
21
|
|
|
* @param array $arguments |
22
|
|
|
* |
23
|
|
|
* @throws BadMethodCallException |
24
|
|
|
* @throws BoletoWinnerException |
25
|
|
|
* |
26
|
|
|
* @return bool |
27
|
|
|
*/ |
28
|
3 |
|
public static function __callStatic($method, $arguments) |
29
|
|
|
{ |
30
|
3 |
|
if (starts_with('isValid', $method)) { |
31
|
2 |
|
return self::handleIsValidTypeCall($method, $arguments); |
32
|
|
|
} |
33
|
|
|
|
34
|
1 |
|
throw new BadMethodCallException("Method `{$method}` does not exist."); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @throws BoletoWinnerException |
39
|
|
|
*/ |
40
|
1 |
|
public static function makeBill(string $barcodeOrWritableLine): Bill |
41
|
|
|
{ |
42
|
1 |
|
return BillFactory::getInstance() |
43
|
1 |
|
->createFromBarcodeOrWritableLine($barcodeOrWritableLine); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @throws BoletoWinnerException |
48
|
|
|
*/ |
49
|
1 |
|
public static function toWritableLine(string $barcode): string |
50
|
|
|
{ |
51
|
1 |
|
return BillFactory::getInstance() |
52
|
1 |
|
->createFromBarcode($barcode) |
53
|
1 |
|
->getWritableLine(); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @throws BoletoWinnerException |
58
|
|
|
*/ |
59
|
1 |
|
public static function toBarcode(string $writableLine): string |
60
|
|
|
{ |
61
|
1 |
|
return BillFactory::getInstance() |
62
|
1 |
|
->createFromWritableLine($writableLine) |
63
|
1 |
|
->getBarcode(); |
64
|
|
|
} |
65
|
|
|
|
66
|
8 |
|
public static function isValid(string $barcodeOrWritableLine): bool |
67
|
|
|
{ |
68
|
|
|
try { |
69
|
8 |
|
BillFactory::getInstance()->createFromBarcodeOrWritableLine($barcodeOrWritableLine); |
70
|
|
|
|
71
|
4 |
|
return true; |
72
|
4 |
|
} catch (BoletoWinnerException $exception) { |
73
|
4 |
|
return false; |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
1 |
|
public static function isValidWritableLine(string $writableLine): bool |
78
|
|
|
{ |
79
|
|
|
try { |
80
|
1 |
|
BillFactory::getInstance()->createFromWritableLine($writableLine); |
81
|
|
|
|
82
|
1 |
|
return true; |
83
|
1 |
|
} catch (BoletoWinnerException $exception) { |
84
|
1 |
|
return false; |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
1 |
|
public static function isValidBarcode(string $barcode): bool |
89
|
|
|
{ |
90
|
|
|
try { |
91
|
1 |
|
BillFactory::getInstance()->createFromBarcode($barcode); |
92
|
|
|
|
93
|
1 |
|
return true; |
94
|
1 |
|
} catch (BoletoWinnerException $exception) { |
95
|
1 |
|
return false; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @throws BoletoWinnerException |
101
|
|
|
*/ |
102
|
2 |
|
private static function handleIsValidTypeCall(string $method, array $arguments): bool |
103
|
|
|
{ |
104
|
2 |
|
$type = strtolower(substr($method, 7)); |
105
|
2 |
|
$input = self::sanitizeInput(array_shift($arguments)); |
106
|
2 |
|
$billClass = BillFactory::getInstance()->createBillInstance($type); |
107
|
2 |
|
$billClass->setBarcode($input)->setWritableLine($input); |
108
|
|
|
|
109
|
2 |
|
return $billClass->isBarcodeValid() || $billClass->isWritableLineValid(); |
110
|
|
|
} |
111
|
|
|
|
112
|
2 |
|
private static function sanitizeInput(string $input): string |
113
|
|
|
{ |
114
|
2 |
|
return preg_replace('/[^0-9]/', '', $input); |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|