forked from s421507/eOSP2
1
0
Fork 0

Prześlij pliki do 'App/Rules'

This commit is contained in:
Krzysztof Strzelecki 2019-09-17 17:28:36 +00:00
parent 999d7aa317
commit 9605c5e638
1 changed files with 60 additions and 0 deletions

60
App/Rules/Pesel.php Normal file
View File

@ -0,0 +1,60 @@
<?php
namespace App\Rules;
use Illuminate\Contracts\Validation\Rule;
class Pesel implements Rule
{
/**
* Create a new rule instance.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Determine if the validation rule passes.
*
* @param string $attribute
* @param mixed $value
* @return bool
*/
// za: phpedia.pl/wiki/Walidacja_numeru_PESEL
public function passes($attribute, $value)
{
//
if (!preg_match('/^[0-9]{11}$/',$value)) //sprawdzamy czy ciąg ma 11 cyfr
{
return false;
}
$arrSteps = array(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;
}
/**
* Get the validation error message.
*
* @return string
*/
public function message()
{
return 'Niepoprawny numer PESEL.';
}
}