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