1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Brazanation\Documents\StateRegistration; |
4
|
|
|
|
5
|
|
|
use Brazanation\Documents\DigitCalculator; |
6
|
|
|
|
7
|
|
|
final class Goias extends State |
8
|
|
|
{ |
9
|
|
|
const LONG_NAME = 'Goias'; |
10
|
|
|
|
11
|
|
|
const REGEX = '/^(1[015])(\d{3})(\d{3})(\d{1})$/'; |
12
|
|
|
|
13
|
|
|
const FORMAT = '$1.$2.$3-$4'; |
14
|
|
|
|
15
|
|
|
const LENGTH = 9; |
16
|
|
|
|
17
|
|
|
const NUMBER_OF_DIGITS = 1; |
18
|
|
|
|
19
|
|
|
const SHORT_NAME = 'GO'; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
private $digit; |
25
|
|
|
|
26
|
12 |
|
public function __construct() |
27
|
|
|
{ |
28
|
12 |
|
parent::__construct(self::LONG_NAME, self::LENGTH, self::NUMBER_OF_DIGITS, self::REGEX, self::FORMAT); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* |
33
|
|
|
* @param string $digit |
34
|
|
|
* |
35
|
|
|
* @return Goias |
36
|
|
|
*/ |
37
|
12 |
|
private function setCurrentDigit(string $digit) |
38
|
|
|
{ |
39
|
12 |
|
$this->digit = $digit; |
40
|
|
|
|
41
|
12 |
|
return $this; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* {@inheritdoc} |
46
|
|
|
* |
47
|
|
|
* @see http://www.sintegra.gov.br/Cad_Estados/cad_GO.html |
48
|
|
|
*/ |
49
|
10 |
|
public function calculateDigit(string $baseNumber) : string |
50
|
|
|
{ |
51
|
10 |
|
$digitToReplace = $this->discoverDigitToReplace((int) $baseNumber); |
52
|
|
|
|
53
|
10 |
|
$calculator = new DigitCalculator($baseNumber); |
54
|
10 |
|
$calculator->useComplementaryInsteadOfModule(); |
55
|
10 |
|
$calculator->replaceWhen($digitToReplace, 10); |
56
|
10 |
|
$calculator->replaceWhen('0', 11); |
57
|
10 |
|
$calculator->withModule(DigitCalculator::MODULE_11); |
58
|
|
|
|
59
|
10 |
|
$digit = $calculator->calculate(); |
60
|
|
|
|
61
|
10 |
|
if ($this->isMagicNumber($baseNumber)) { |
62
|
4 |
|
return $this->fixedDigitForMagicNumber($digit); |
63
|
|
|
} |
64
|
|
|
|
65
|
6 |
|
return "{$digit}"; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* A bizarre rule for this number(11094402). |
70
|
|
|
* |
71
|
|
|
* Default digit is 0, but could be 1 too :( |
72
|
|
|
* |
73
|
|
|
* @param string $number A magic base number. |
74
|
|
|
* |
75
|
|
|
* @return bool Returns true if number is magic, otherwise false. |
76
|
|
|
*/ |
77
|
10 |
|
private function isMagicNumber(string $number) : bool |
78
|
|
|
{ |
79
|
10 |
|
return $number == '11094402'; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* A big workaround for StateRegistration(11094402) |
84
|
|
|
* |
85
|
|
|
* @param string $digit |
86
|
|
|
* |
87
|
|
|
* @return string |
88
|
|
|
*/ |
89
|
4 |
|
private function fixedDigitForMagicNumber(string $digit) : string |
90
|
|
|
{ |
91
|
4 |
|
if ($digit == $this->digit && $digit == '0') { |
92
|
2 |
|
return '0'; |
93
|
|
|
} |
94
|
|
|
|
95
|
2 |
|
return '1'; |
96
|
|
|
} |
97
|
|
|
|
98
|
10 |
|
private function discoverDigitToReplace(string $number) : string |
99
|
|
|
{ |
100
|
10 |
|
$digitToReplace = '0'; |
101
|
10 |
|
if ((10103105 <= $number) && ($number <= 10119997)) { |
102
|
|
|
$digitToReplace = '1'; |
103
|
|
|
} |
104
|
|
|
|
105
|
10 |
|
return $digitToReplace; |
106
|
|
|
} |
107
|
|
|
|
108
|
12 |
|
public function normalizeNumber(string $number) : string |
109
|
|
|
{ |
110
|
12 |
|
$number = parent::normalizeNumber($number); |
111
|
12 |
|
$this->setCurrentDigit(substr($number, -($this->getNumberOfDigits()))); |
112
|
|
|
|
113
|
12 |
|
return $number; |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|