2015-11-26 20:46:51 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is part of Respect/Validation.
|
|
|
|
*
|
|
|
|
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
|
|
|
|
*
|
2019-05-23 16:21:34 +02:00
|
|
|
* For the full copyright and license information, please view the LICENSE file
|
|
|
|
* that was distributed with this source code.
|
2015-11-26 20:46:51 +01:00
|
|
|
*/
|
|
|
|
|
2017-11-19 19:01:31 +01:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2015-11-26 20:46:51 +01:00
|
|
|
namespace Respect\Validation\Rules;
|
|
|
|
|
2020-04-04 22:13:44 +02:00
|
|
|
use function is_scalar;
|
2018-09-07 20:55:21 +02:00
|
|
|
use function preg_match;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Validates PESEL (Polish human identification number).
|
|
|
|
*
|
|
|
|
* @author Danilo Correa <danilosilva87@gmail.com>
|
|
|
|
* @author Henrique Moody <henriquemoody@gmail.com>
|
|
|
|
* @author Tomasz Regdos <tomek@regdos.com>
|
|
|
|
*/
|
|
|
|
final class Pesel extends AbstractRule
|
2015-11-26 20:46:51 +01:00
|
|
|
{
|
2018-09-07 20:55:21 +02:00
|
|
|
/**
|
2019-04-05 14:33:00 +02:00
|
|
|
* {@inheritDoc}
|
2018-09-07 20:55:21 +02:00
|
|
|
*/
|
2018-01-28 15:41:31 +01:00
|
|
|
public function validate($input): bool
|
2015-11-26 20:46:51 +01:00
|
|
|
{
|
2020-04-04 22:13:44 +02:00
|
|
|
if (!is_scalar($input)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$stringInput = (string) $input;
|
|
|
|
if (!preg_match('/^\d{11}$/', (string) $stringInput)) {
|
2015-11-26 20:46:51 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-01-14 00:10:09 +01:00
|
|
|
$weights = [1, 3, 7, 9, 1, 3, 7, 9, 1, 3];
|
|
|
|
|
2020-04-04 22:13:44 +02:00
|
|
|
$targetControlNumber = $stringInput[10];
|
2015-11-26 20:46:51 +01:00
|
|
|
$calculateControlNumber = 0;
|
|
|
|
|
2016-10-30 10:39:23 +01:00
|
|
|
for ($i = 0; $i < 10; ++$i) {
|
2020-07-22 14:02:39 +02:00
|
|
|
$calculateControlNumber += (int) $stringInput[$i] * $weights[$i];
|
2015-11-26 20:46:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$calculateControlNumber = (10 - $calculateControlNumber % 10) % 10;
|
|
|
|
|
|
|
|
return $targetControlNumber == $calculateControlNumber;
|
|
|
|
}
|
|
|
|
}
|