1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Brazanation\Documents\StateRegistration; |
4
|
|
|
|
5
|
|
|
use Brazanation\Documents\DigitCalculator; |
6
|
|
|
|
7
|
|
|
final class Bahia extends State |
8
|
|
|
{ |
9
|
|
|
const LABEL = 'Bahia'; |
10
|
|
|
|
11
|
|
|
const REGEX = '/^([\d]{6,7})(\d{2})$/'; |
12
|
|
|
|
13
|
|
|
const FORMAT = '$1-$2'; |
14
|
|
|
|
15
|
|
|
const LENGTH = 9; |
16
|
|
|
|
17
|
|
|
const DIGITS_COUNT = 2; |
18
|
|
|
|
19
|
11 |
|
public function __construct() |
20
|
|
|
{ |
21
|
11 |
|
parent::__construct(self::LABEL, self::LENGTH, self::DIGITS_COUNT, self::REGEX, self::FORMAT); |
22
|
11 |
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* {@inheritdoc} |
26
|
|
|
*/ |
27
|
11 |
|
public function normalizeNumber($number) |
28
|
|
|
{ |
29
|
11 |
|
if (!empty($number)) { |
30
|
10 |
|
return str_pad(parent::normalizeNumber($number), $this->getLength(), '0', STR_PAD_LEFT); |
31
|
|
|
} |
32
|
|
|
|
33
|
1 |
|
return $number; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* {@inheritdoc} |
38
|
|
|
* |
39
|
|
|
* @see http://www.sintegra.gov.br/Cad_Estados/cad_BA.html |
40
|
|
|
*/ |
41
|
10 |
|
public function calculateDigit($baseNumber) |
42
|
|
|
{ |
43
|
10 |
|
$calculator = new DigitCalculator($baseNumber); |
44
|
10 |
|
$calculator->useComplementaryInsteadOfModule(); |
45
|
10 |
|
$calculator->replaceWhen('0', 10, 11); |
46
|
10 |
|
$calculator->withModule($this->discoverModule($baseNumber)); |
47
|
|
|
|
48
|
10 |
|
$firstDigit = $calculator->calculate(); |
49
|
10 |
|
$calculator->addDigit($firstDigit); |
50
|
10 |
|
$secondDigit = $calculator->calculate(); |
51
|
|
|
|
52
|
10 |
|
return "{$secondDigit}{$firstDigit}"; |
53
|
|
|
} |
54
|
|
|
|
55
|
10 |
|
private function discoverModule($baseNumber) |
56
|
|
|
{ |
57
|
10 |
|
$charToCheck = substr($baseNumber, 1, 1); |
58
|
10 |
|
if (strlen($baseNumber) == 6) { |
59
|
|
|
$charToCheck = substr($baseNumber, 0, 1); |
60
|
|
|
} |
61
|
10 |
|
if (6 <= $charToCheck && $charToCheck <= 9) { |
62
|
3 |
|
return DigitCalculator::MODULE_11; |
63
|
|
|
} |
64
|
|
|
|
65
|
7 |
|
return DigitCalculator::MODULE_10; |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|