|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Claudsonm\BoletoWinner\Validators; |
|
4
|
|
|
|
|
5
|
|
|
class BoletoValidator implements Validator |
|
6
|
|
|
{ |
|
7
|
|
|
/** |
|
8
|
|
|
* {@inheritdoc} |
|
9
|
|
|
*/ |
|
10
|
45 |
|
public function verifyWritableLine(string $writableLine): bool |
|
11
|
|
|
{ |
|
12
|
|
|
$blocks = [ |
|
13
|
45 |
|
substr($writableLine, 0, 10), |
|
14
|
45 |
|
substr($writableLine, 10, 11), |
|
15
|
45 |
|
substr($writableLine, 21, 11), |
|
16
|
|
|
]; |
|
17
|
|
|
|
|
18
|
45 |
|
$blocksValidated = 0; |
|
19
|
45 |
|
foreach ($blocks as $block) { |
|
20
|
45 |
|
$blockLength = strlen($block) - 1; |
|
21
|
45 |
|
$blockInputNumbers = substr($block, 0, $blockLength); |
|
22
|
45 |
|
$blockVerificationDigit = $this->getLastCharacter($block); |
|
23
|
45 |
|
if (module10($blockInputNumbers) === $blockVerificationDigit) { |
|
24
|
34 |
|
$blocksValidated++; |
|
25
|
|
|
} |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
45 |
|
return 3 === $blocksValidated; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* {@inheritdoc} |
|
33
|
|
|
*/ |
|
34
|
48 |
|
public function verifyBarcode(string $barcode): bool |
|
35
|
|
|
{ |
|
36
|
48 |
|
$barcodeVerificationDigit = substr($barcode, 4, 1); |
|
37
|
48 |
|
$blockInputNumbers = substr($barcode, 0, 4); |
|
38
|
48 |
|
$blockInputNumbers .= substr($barcode, 5); |
|
39
|
|
|
|
|
40
|
48 |
|
return $this->calculateBarcodeDigit($blockInputNumbers) === $barcodeVerificationDigit; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Returns the last character of the input string. |
|
45
|
|
|
*/ |
|
46
|
45 |
|
private function getLastCharacter(string $input): string |
|
47
|
|
|
{ |
|
48
|
45 |
|
return substr($input, -1); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Computes the barcode verification digit. |
|
53
|
|
|
*/ |
|
54
|
48 |
|
private function calculateBarcodeDigit(string $inputNumbers): string |
|
55
|
|
|
{ |
|
56
|
48 |
|
$digit = module11($inputNumbers); |
|
57
|
48 |
|
if ('0' === $digit) { |
|
58
|
7 |
|
return '1'; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
41 |
|
return $digit; |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
|