|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Brazanation\Documents; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Class StateRegistration |
|
7
|
|
|
* |
|
8
|
|
|
* @package Brazanation\Documents |
|
9
|
|
|
* |
|
10
|
|
|
* @method static StateRegistration AC($number) |
|
11
|
|
|
* @method static StateRegistration AL($number) |
|
12
|
|
|
* @method static StateRegistration AM($number) |
|
13
|
|
|
* @method static StateRegistration AP($number) |
|
14
|
|
|
* @method static StateRegistration BA($number) |
|
15
|
|
|
* @method static StateRegistration CE($number) |
|
16
|
|
|
* @method static StateRegistration DF($number) |
|
17
|
|
|
* @method static StateRegistration ES($number) |
|
18
|
|
|
* @method static StateRegistration GO($number) |
|
19
|
|
|
* @method static StateRegistration MA($number) |
|
20
|
|
|
* @method static StateRegistration MG($number) |
|
21
|
|
|
* @method static StateRegistration MS($number) |
|
22
|
|
|
* @method static StateRegistration MT($number) |
|
23
|
|
|
* @method static StateRegistration PA($number) |
|
24
|
|
|
* @method static StateRegistration PB($number) |
|
25
|
|
|
* @method static StateRegistration PE($number) |
|
26
|
|
|
* @method static StateRegistration PI($number) |
|
27
|
|
|
* @method static StateRegistration PR($number) |
|
28
|
|
|
* @method static StateRegistration RJ($number) |
|
29
|
|
|
* @method static StateRegistration RN($number) |
|
30
|
|
|
* @method static StateRegistration RO($number) |
|
31
|
|
|
* @method static StateRegistration RR($number) |
|
32
|
|
|
* @method static StateRegistration RS($number) |
|
33
|
|
|
* @method static StateRegistration SC($number) |
|
34
|
|
|
* @method static StateRegistration SE($number) |
|
35
|
|
|
* @method static StateRegistration SP($number) |
|
36
|
|
|
* @method static StateRegistration TO($number) |
|
37
|
|
|
*/ |
|
38
|
|
|
final class StateRegistration extends AbstractDocument |
|
39
|
|
|
{ |
|
40
|
|
|
/** |
|
41
|
|
|
* @var StateRegistration\StateInterface |
|
42
|
|
|
*/ |
|
43
|
|
|
private $state; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @var array |
|
47
|
|
|
*/ |
|
48
|
|
|
private static $availableStates = [ |
|
49
|
|
|
StateRegistration\Acre::SHORT_NAME => StateRegistration\Acre::class, |
|
50
|
|
|
StateRegistration\Alagoas::SHORT_NAME => StateRegistration\Alagoas::class, |
|
51
|
|
|
StateRegistration\Amapa::SHORT_NAME => StateRegistration\Amapa::class, |
|
52
|
|
|
StateRegistration\Amazonas::SHORT_NAME => StateRegistration\Amazonas::class, |
|
53
|
|
|
StateRegistration\Bahia::SHORT_NAME => StateRegistration\Bahia::class, |
|
54
|
|
|
StateRegistration\Ceara::SHORT_NAME => StateRegistration\Ceara::class, |
|
55
|
|
|
StateRegistration\DistritoFederal::SHORT_NAME => StateRegistration\DistritoFederal::class, |
|
56
|
|
|
StateRegistration\EspiritoSanto::SHORT_NAME => StateRegistration\EspiritoSanto::class, |
|
57
|
|
|
StateRegistration\Goias::SHORT_NAME => StateRegistration\Goias::class, |
|
58
|
|
|
StateRegistration\Maranhao::SHORT_NAME => StateRegistration\Maranhao::class, |
|
59
|
|
|
StateRegistration\MatoGrosso::SHORT_NAME => StateRegistration\MatoGrosso::class, |
|
60
|
|
|
StateRegistration\MatoGrossoSul::SHORT_NAME => StateRegistration\MatoGrossoSul::class, |
|
61
|
|
|
StateRegistration\MinasGerais::SHORT_NAME => StateRegistration\MinasGerais::class, |
|
62
|
|
|
StateRegistration\Para::SHORT_NAME => StateRegistration\Para::class, |
|
63
|
|
|
StateRegistration\Paraiba::SHORT_NAME => StateRegistration\Paraiba::class, |
|
64
|
|
|
StateRegistration\Parana::SHORT_NAME => StateRegistration\Parana::class, |
|
65
|
|
|
StateRegistration\Pernambuco::SHORT_NAME => StateRegistration\Pernambuco::class, |
|
66
|
|
|
StateRegistration\Piaui::SHORT_NAME => StateRegistration\Piaui::class, |
|
67
|
|
|
StateRegistration\RioDeJaneiro::SHORT_NAME => StateRegistration\RioDeJaneiro::class, |
|
68
|
|
|
StateRegistration\RioGrandeDoNorte::SHORT_NAME => StateRegistration\RioGrandeDoNorte::class, |
|
69
|
|
|
StateRegistration\RioGrandeDoSul::SHORT_NAME => StateRegistration\RioGrandeDoSul::class, |
|
70
|
|
|
StateRegistration\Rondonia::SHORT_NAME => StateRegistration\Rondonia::class, |
|
71
|
|
|
StateRegistration\Roraima::SHORT_NAME => StateRegistration\Roraima::class, |
|
72
|
|
|
StateRegistration\SantaCatarina::SHORT_NAME => StateRegistration\SantaCatarina::class, |
|
73
|
|
|
StateRegistration\SaoPaulo::SHORT_NAME => StateRegistration\SaoPaulo::class, |
|
74
|
|
|
StateRegistration\Sergipe::SHORT_NAME => StateRegistration\Sergipe::class, |
|
75
|
|
|
StateRegistration\Tocantins::SHORT_NAME => StateRegistration\Tocantins::class, |
|
76
|
|
|
]; |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* StateRegistration constructor. |
|
80
|
|
|
* |
|
81
|
|
|
* @param string $number |
|
82
|
|
|
* @param StateRegistration\StateInterface $state |
|
83
|
|
|
*/ |
|
84
|
966 |
|
public function __construct($number, StateRegistration\StateInterface $state) |
|
85
|
|
|
{ |
|
86
|
966 |
|
$number = $state->normalizeNumber($number); |
|
87
|
966 |
|
$this->state = $state; |
|
88
|
966 |
|
parent::__construct($number, $state->getLength(), $state->getNumberOfDigits(), $state->getState()); |
|
89
|
780 |
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Try to create a StateRegistration object from given number and state short name. |
|
93
|
|
|
* |
|
94
|
|
|
* @param string $number Document number to be parsed. |
|
95
|
|
|
* @param string $state State short name. |
|
96
|
|
|
* |
|
97
|
|
|
* @return AbstractDocument|boolean Returns a new Document instance or FALSE on failure. |
|
98
|
|
|
*/ |
|
99
|
483 |
|
public static function createFromString($number, $state = '') |
|
100
|
|
|
{ |
|
101
|
|
|
try { |
|
102
|
483 |
|
return static::$state($number); |
|
103
|
93 |
|
} catch (Exception\InvalidDocument $exception) { |
|
104
|
93 |
|
return false; |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* @param string $name |
|
110
|
|
|
* @param array $arguments |
|
111
|
|
|
* |
|
112
|
|
|
* @return StateRegistration |
|
113
|
|
|
* |
|
114
|
|
|
* @throws \ReflectionException |
|
115
|
|
|
*/ |
|
116
|
503 |
|
public static function __callStatic($name, $arguments) |
|
117
|
|
|
{ |
|
118
|
503 |
|
$class = self::$availableStates[$name]; |
|
119
|
503 |
|
$reflection = new \ReflectionClass($class); |
|
120
|
|
|
|
|
121
|
|
|
/** @var StateRegistration\StateInterface $state */ |
|
122
|
503 |
|
$state = $reflection->newInstanceArgs(); |
|
123
|
|
|
|
|
124
|
503 |
|
return new self($arguments[0], $state); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* {@inheritdoc} |
|
129
|
|
|
*/ |
|
130
|
888 |
|
protected function extractBaseNumber($number) |
|
131
|
|
|
{ |
|
132
|
888 |
|
return $this->state->extractBaseNumber($number); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* {@inheritdoc} |
|
137
|
|
|
*/ |
|
138
|
966 |
|
protected function extractCheckerDigit($number) |
|
139
|
|
|
{ |
|
140
|
966 |
|
return $this->state->extractCheckerDigit($number); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* Voter does not has a specific format. |
|
145
|
|
|
* |
|
146
|
|
|
* @return string Returns only numbers. |
|
147
|
|
|
*/ |
|
148
|
82 |
|
public function format() |
|
149
|
|
|
{ |
|
150
|
82 |
|
return preg_replace($this->state->getRegex(), $this->state->getFormat(), "{$this}"); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* {@inheritdoc} |
|
155
|
|
|
*/ |
|
156
|
834 |
|
public function calculateDigit($baseNumber) |
|
157
|
|
|
{ |
|
158
|
834 |
|
$digit = $this->state->calculateDigit($baseNumber); |
|
159
|
|
|
|
|
160
|
834 |
|
return "{$digit}"; |
|
161
|
|
|
} |
|
162
|
|
|
} |
|
163
|
|
|
|