1
1
<?php
2
2
3
+ declare (strict_types=1 );
4
+
3
5
namespace geekcom \ValidatorDocs ;
4
6
5
7
use Illuminate \Validation \Validator as BaseValidator ;
6
8
7
9
use function preg_match ;
8
10
use function preg_replace ;
9
- use function strlen ;
11
+ use function mb_strlen ;
10
12
use function str_repeat ;
11
13
use function sprintf ;
12
14
use function substr ;
18
20
*/
19
21
class Validator extends BaseValidator
20
22
{
21
- protected function validateFormatoCpf ($ attribute , $ value )
23
+ protected function validateFormatoCpf ($ attribute , $ value ): bool
22
24
{
23
25
return preg_match ('/^\d{3}\.\d{3}\.\d{3}-\d{2}$/ ' , $ value ) > 0 ;
24
26
}
25
27
26
- protected function validateFormatoCnpj ($ attribute , $ value )
28
+ protected function validateFormatoCnpj ($ attribute , $ value ): bool
27
29
{
28
30
return preg_match ('/^\d{2}\.\d{3}\.\d{3}\/\d{4}-\d{2}$/ ' , $ value ) > 0 ;
29
31
}
30
32
31
- protected function validateFormatoCpfCnpj ($ attribute , $ value )
33
+ protected function validateFormatoCpfCnpj ($ attribute , $ value ): bool
32
34
{
33
35
return $ this ->validateFormatoCpf ($ attribute , $ value ) || $ this ->validateFormatoCnpj ($ attribute , $ value );
34
36
}
35
37
36
- protected function validateFormatoNis ($ attribute , $ value )
38
+ protected function validateFormatoNis ($ attribute , $ value ): bool
37
39
{
38
40
return preg_match ('/^\d{3}\.\d{5}\.\d{2}-\d{1}$/ ' , $ value ) > 0 ;
39
41
}
40
42
41
- /*
42
- * O Número de Matrícula tem a configuração aaaaaa.bb.cc.dddd.e.fffff.ggg.hhhhhhh-ii
43
- */
44
- protected function validateFormatoCertidao ($ attribute , $ value )
43
+ protected function validateFormatoCertidao ($ attribute , $ value ): bool
45
44
{
46
45
return preg_match ('/^\d{6}[. ]\d{2}[. ]\d{2}[. ]\d{4}[. ]\d{1}[. ]\d{5}[. ]\d{3}[. ]\d{7}[- ]\d{2}$/ ' , $ value ) > 0 ;
47
46
}
48
47
49
- protected function validateCpf ($ attribute , $ value )
48
+ protected function validateCpf ($ attribute , $ value ): bool
50
49
{
51
- $ c = preg_replace ( ' /\D/ ' , '' , $ value );
50
+ $ c = $ this -> removeCaracteresNaoNumericos ( $ value );
52
51
53
- if (strlen ($ c ) != 11 || preg_match ("/^ {$ c [0 ]}{11}$/ " , $ c )) {
52
+ if (mb_strlen ($ c ) != 11 || preg_match ("/^ {$ c [0 ]}{11}$/ " , $ c )) {
54
53
return false ;
55
54
}
56
55
@@ -69,11 +68,11 @@ protected function validateCpf($attribute, $value)
69
68
return true ;
70
69
}
71
70
72
- protected function validateCnpj ($ attribute , $ value )
71
+ protected function validateCnpj ($ attribute , $ value ): bool
73
72
{
74
- $ c = preg_replace ( ' /\D/ ' , '' , $ value );
73
+ $ c = $ this -> removeCaracteresNaoNumericos ( $ value );
75
74
76
- if (strlen ($ c ) != 14 || preg_match ("/^ {$ c [0 ]}{14}$/ " , $ c )) {
75
+ if (mb_strlen ($ c ) != 14 || preg_match ("/^ {$ c [0 ]}{14}$/ " , $ c )) {
77
76
return false ;
78
77
}
79
78
@@ -94,52 +93,51 @@ protected function validateCnpj($attribute, $value)
94
93
return true ;
95
94
}
96
95
97
- protected function validateCpfCnpj ($ attribute , $ value )
96
+ protected function validateCpfCnpj ($ attribute , $ value ): bool
98
97
{
99
98
return ($ this ->validateCpf ($ attribute , $ value ) || $ this ->validateCnpj ($ attribute , $ value ));
100
99
}
101
100
102
101
/**
103
102
* Trecho retirado do respect validation
104
103
*/
105
- protected function validateCnh ($ attribute , $ value )
104
+ protected function validateCnh ($ attribute , $ value ): bool
106
105
{
107
- $ ret = false ;
106
+ if (!is_scalar ($ value )) {
107
+ return false ;
108
+ }
108
109
109
- if ((strlen ($ input = preg_replace ('/[^\d]/ ' , '' , $ value )) == 11 )
110
- && (str_repeat ($ input [1 ], 11 ) != $ input )
111
- ) {
112
- $ dsc = 0 ;
110
+ $ value = $ this ->removeCaracteresNaoNumericos ($ value );
113
111
114
- for ( $ i = 0 , $ j = 9 , $ v = 0 ; $ i < 9 ; ++ $ i , -- $ j ) {
115
- $ v += ( int ) $ input [ $ i ] * $ j ;
116
- }
112
+ if ( mb_strlen ( $ value ) != 11 || (( int ) $ value === 0 ) ) {
113
+ return false ;
114
+ }
117
115
118
- if (( $ vl1 = $ v % 11 ) >= 10 ) {
119
- $ vl1 = 0 ;
120
- $ dsc = 2 ;
121
- }
116
+ for ( $ c = $ s1 = $ s2 = 0 , $ p = 9 ; $ c < 9 ; $ c ++, $ p -- ) {
117
+ $ s1 += ( int ) $ value [ $ c ] * $ p ;
118
+ $ s2 += ( int ) $ value [ $ c ] * ( 10 - $ p ) ;
119
+ }
122
120
123
- for ($ i = 0 , $ j = 1 , $ v = 0 ; $ i < 9 ; ++$ i , ++$ j ) {
124
- $ v += (int ) $ input [$ i ] * $ j ;
125
- }
121
+ $ dv1 = $ s1 % 11 ;
122
+ if ($ value [9 ] != ($ dv1 > 9 ) ? 0 : $ dv1 ) {
123
+ return false ;
124
+ }
126
125
127
- $ vl2 = ( $ x = ( $ v % 11 )) >= 10 ? 0 : $ x - $ dsc ;
126
+ $ dv2 = $ s2 % 11 - ( $ dv1 > 9 ? 2 : 0 ) ;
128
127
129
- $ ret = sprintf ('%d%d ' , $ vl1 , $ vl2 ) == substr ($ input , -2 );
130
- }
128
+ $ check = $ dv2 < 0 ? $ dv2 + 11 : $ dv2 > 9 ? 0 : $ dv2 ;
131
129
132
- return $ ret ;
130
+ return $ value [ 10 ] == $ check ;
133
131
}
134
132
135
- protected function validateTituloEleitor ($ attribute , $ value )
133
+ protected function validateTituloEleitor ($ attribute , $ value ): bool
136
134
{
137
- $ input = preg_replace ( ' /[^\d]/ ' , '' , $ value );
135
+ $ input = $ this -> removeCaracteresNaoNumericos ( $ value );
138
136
139
137
$ uf = substr ($ input , -4 , 2 );
140
138
141
- if (((strlen ($ input ) < 5 ) || (strlen ($ input ) > 13 )) ||
142
- (str_repeat ($ input [1 ], strlen ($ input )) == $ input ) ||
139
+ if (((mb_strlen ($ input ) < 5 ) || (mb_strlen ($ input ) > 13 )) ||
140
+ (str_repeat ($ input [1 ], mb_strlen ($ input )) == $ input ) ||
143
141
($ uf < 1 || $ uf > 28 )) {
144
142
return false ;
145
143
}
@@ -153,7 +151,7 @@ protected function validateTituloEleitor($attribute, $value)
153
151
$ fator = 9 ;
154
152
$ soma = 0 ;
155
153
156
- for ($ j = (strlen ($ sequencia ) - 1 ); $ j > -1 ; $ j --) {
154
+ for ($ j = (mb_strlen ($ sequencia ) - 1 ); $ j > -1 ; $ j --) {
157
155
$ soma += $ sequencia [$ j ] * $ fator ;
158
156
159
157
if ($ fator == $ base ) {
@@ -185,11 +183,11 @@ protected function validateTituloEleitor($attribute, $value)
185
183
return true ;
186
184
}
187
185
188
- protected function validateNis ($ attribute , $ value )
186
+ protected function validateNis ($ attribute , $ value ): bool
189
187
{
190
- $ nis = sprintf ('%011s ' , preg_replace ( ' {\D} ' , '' , $ value ));
188
+ $ nis = sprintf ('%011s ' , $ this -> removeCaracteresNaoNumericos ( $ value ));
191
189
192
- if (strlen ($ nis ) != 11 || preg_match ("/^ {$ nis [0 ]}{11}$/ " , $ nis )) {
190
+ if (mb_strlen ($ nis ) != 11 || preg_match ("/^ {$ nis [0 ]}{11}$/ " , $ nis )) {
193
191
return false ;
194
192
}
195
193
@@ -200,9 +198,9 @@ protected function validateNis($attribute, $value)
200
198
return ($ nis [10 ] == (((10 * $ d ) % 11 ) % 10 ));
201
199
}
202
200
203
- protected function validateCns ($ attribute , $ value )
201
+ protected function validateCns ($ attribute , $ value ): bool
204
202
{
205
- $ cns = preg_replace ( ' /[^\d]/ ' , '' , $ value );
203
+ $ cns = $ this -> removeCaracteresNaoNumericos ( $ value );
206
204
207
205
// CNSs definitivos começam em 1 ou 2 / CNSs provisórios em 7, 8 ou 9
208
206
if (preg_match ("/[1-2][0-9]{10}00[0-1][0-9]/ " , $ cns ) || preg_match ("/[7-9][0-9]{14}/ " , $ cns )) {
@@ -212,27 +210,26 @@ protected function validateCns($attribute, $value)
212
210
return false ;
213
211
}
214
212
215
- private function somaPonderadaCns ($ value )
213
+ private function somaPonderadaCns ($ value ): int
216
214
{
217
215
$ soma = 0 ;
218
216
219
- for ($ i = 0 ; $ i < strlen ($ value ); $ i ++) {
217
+ for ($ i = 0 ; $ i < mb_strlen ($ value ); $ i ++) {
220
218
$ soma += $ value [$ i ] * (15 - $ i );
221
219
}
222
220
223
221
return $ soma ;
224
222
}
225
-
223
+
226
224
/*
227
225
* CERTIDÃO DE NASCIMENTO/CASAMENTO/ÓBITO
228
226
* Fonte: http://ghiorzi.org/DVnew.htm#zc
229
227
*
230
228
* Nota: se o resto for "10", o DV será "1"
231
229
*/
232
- protected function validateCertidao ($ attribute , $ value )
230
+ protected function validateCertidao ($ attribute , $ value ): bool
233
231
{
234
- // Remove não numericos
235
- $ certidao = preg_replace ('/[^\d]/ ' , '' , $ value );
232
+ $ certidao = $ this ->removeCaracteresNaoNumericos ($ value );
236
233
237
234
if (!preg_match ("/[0-9]{32}/ " , $ certidao )) {
238
235
return false ;
@@ -249,16 +246,19 @@ protected function validateCertidao($attribute, $value)
249
246
// Compara o dv recebido com os dois numeros calculados
250
247
if ($ dv === $ dv1 .$ dv2 ) {
251
248
return true ;
252
- } else {
253
- return false ;
254
249
}
250
+
251
+ return false ;
255
252
}
256
253
257
- private function somaPonderadaCertidao ($ value ) {
254
+ private function somaPonderadaCertidao ($ value ): int
255
+ {
258
256
$ soma = 0 ;
259
257
260
- $ multiplicador = 32 - strlen ($ value );
261
- for ($ i = 0 ; $ i < strlen ($ value ); $ i ++) {
258
+ $ multiplicador = 32 - mb_strlen ($ value );
259
+
260
+ for ($ i = 0 ; $ i < mb_strlen ($ value ); $ i ++) {
261
+
262
262
$ soma += $ value [$ i ] * $ multiplicador ;
263
263
264
264
$ multiplicador += 1 ;
@@ -267,4 +267,9 @@ private function somaPonderadaCertidao($value) {
267
267
268
268
return $ soma ;
269
269
}
270
+
271
+ private function removeCaracteresNaoNumericos ($ value ): string
272
+ {
273
+ return preg_replace ('/[^\d]/ ' , '' , $ value );
274
+ }
270
275
}
0 commit comments