-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathPeselNumberRule.php
35 lines (29 loc) · 1.01 KB
/
PeselNumberRule.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<?php
namespace KDuma\Validator;
use Illuminate\Contracts\Validation\Rule;
class PeselNumberRule implements Rule
{
public function passes($attribute, $value)
{
if (! preg_match('/^[0-9]{11}$/', $value)) { //sprawdzamy czy ciąg ma 11 cyfr
return false;
}
$arrSteps = [1, 3, 7, 9, 1, 3, 7, 9, 1, 3]; // tablica z odpowiednimi wagami
$intSum = 0;
for ($i = 0; $i < 10; $i++) {
$intSum += $arrSteps[$i] * $value[$i]; //mnożymy każdy ze znaków przez wagć i sumujemy wszystko
}
$int = 10 - $intSum % 10; //obliczamy sumć kontrolną
$intControlNr = ($int == 10) ? 0 : $int;
if ($intControlNr == $value[10]) { //sprawdzamy czy taka sama suma kontrolna jest w ciągu
return true;
}
return false;
}
public function message()
{
return trans('validation.pesel') != 'validation.pesel'
? trans('validation.pesel')
: trans('kd-validator::validation.pesel');
}
}