Skip to content

Commit 40d0134

Browse files
authored
Merge pull request #52 from Sysvale/feature/valida-cns
Cria função para validação do CNS
2 parents 562d99a + 6fc5fb7 commit 40d0134

3 files changed

Lines changed: 119 additions & 1 deletion

File tree

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"version": "2.1",
2+
"version": "2.2",
33
"name": "sysvale/helpers",
44
"description": "Helper function in PHP",
55
"type": "library",

src/Helpers/Validate.php

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,4 +115,95 @@ public static function isValidMobilePhone($phone)
115115
$phone
116116
);
117117
}
118+
119+
/**
120+
* @param string $cns only string
121+
* @return bool
122+
*/
123+
public static function isValidCns($cns)
124+
{
125+
$cns = preg_replace('/\D/', '', $cns);
126+
127+
if (strlen($cns) != 15) {
128+
return false;
129+
}
130+
131+
$first = (int) $cns[0];
132+
133+
if (in_array($first, [7, 8, 9])) {
134+
return Validate::validate_cns_tmp($cns);
135+
}
136+
137+
if (in_array($first, [1, 2])) {
138+
return Validate::validate_cns_fixed($cns);
139+
}
140+
return false;
141+
}
142+
143+
private static function validate_cns_fixed($cns)
144+
{
145+
$pis = substr($cns, 0, 11);
146+
147+
$soma = (((substr($pis, 0, 1)) * 15) +
148+
((substr($pis, 1, 1)) * 14) +
149+
((substr($pis, 2, 1)) * 13) +
150+
((substr($pis, 3, 1)) * 12) +
151+
((substr($pis, 4, 1)) * 11) +
152+
((substr($pis, 5, 1)) * 10) +
153+
((substr($pis, 6, 1)) * 9) +
154+
((substr($pis, 7, 1)) * 8) +
155+
((substr($pis, 8, 1)) * 7) +
156+
((substr($pis, 9, 1)) * 6) +
157+
((substr($pis, 10, 1)) * 5));
158+
159+
$resto = fmod($soma, 11);
160+
$dv = 11 - $resto;
161+
if ($dv == 11) {
162+
$dv = 0;
163+
}
164+
if ($dv == 10) {
165+
$soma = ((((substr($pis, 0, 1)) * 15) +
166+
((substr($pis, 1, 1)) * 14) +
167+
((substr($pis, 2, 1)) * 13) +
168+
((substr($pis, 3, 1)) * 12) +
169+
((substr($pis, 4, 1)) * 11) +
170+
((substr($pis, 5, 1)) * 10) +
171+
((substr($pis, 6, 1)) * 9) +
172+
((substr($pis, 7, 1)) * 8) +
173+
((substr($pis, 8, 1)) * 7) +
174+
((substr($pis, 9, 1)) * 6) +
175+
((substr($pis, 10, 1)) * 5)) + 2);
176+
177+
$resto = fmod($soma, 11);
178+
$dv = 11 - $resto;
179+
$resultado = $pis . '001' . $dv;
180+
} else {
181+
$resultado = $pis . '000' . $dv;
182+
}
183+
184+
return $cns === $resultado;
185+
}
186+
187+
private static function validate_cns_tmp($cns)
188+
{
189+
$soma = (((substr($cns, 0, 1)) * 15) +
190+
((substr($cns, 1, 1)) * 14) +
191+
((substr($cns, 2, 1)) * 13) +
192+
((substr($cns, 3, 1)) * 12) +
193+
((substr($cns, 4, 1)) * 11) +
194+
((substr($cns, 5, 1)) * 10) +
195+
((substr($cns, 6, 1)) * 9) +
196+
((substr($cns, 7, 1)) * 8) +
197+
((substr($cns, 8, 1)) * 7) +
198+
((substr($cns, 9, 1)) * 6) +
199+
((substr($cns, 10, 1)) * 5) +
200+
((substr($cns, 11, 1)) * 4) +
201+
((substr($cns, 12, 1)) * 3) +
202+
((substr($cns, 13, 1)) * 2) +
203+
((substr($cns, 14, 1)) * 1));
204+
$resto = fmod($soma, 11);
205+
206+
return $resto == 0;
207+
}
208+
118209
}

tests/Helpers/ValidateTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,4 +140,31 @@ public function phonesMobileProvider()
140140
['', false],
141141
];
142142
}
143+
144+
/**
145+
* @dataProvider cnsProvider
146+
*/
147+
public function testValidCns($value, $expected_result)
148+
{
149+
$isValid = Validate::isValidCns($value);
150+
151+
$this->assertSame($expected_result, $isValid);
152+
}
153+
154+
public function cnsProvider()
155+
{
156+
return [
157+
['929086483480003', true],
158+
['191949203510003', true],
159+
['229421127850005', true],
160+
['796368354370018', true],
161+
['796 3683 5437 0018', true],
162+
['796 3683 54370018', true],
163+
['7963683 54370018', true],
164+
['696368354370018', false],
165+
['7963683543700120', false],
166+
['', false],
167+
[null, false],
168+
];
169+
}
143170
}

0 commit comments

Comments
 (0)