2022-12-24 22:10:40 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
2024-01-31 22:15:08 +08:00
|
|
|
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
|
|
|
|
* SPDX-License-Identifier: MIT
|
2022-12-24 22:10:40 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace Respect\Validation\Rules;
|
|
|
|
|
2024-01-31 22:15:08 +08:00
|
|
|
use function is_scalar;
|
2022-12-24 22:10:40 +08:00
|
|
|
use function ord;
|
|
|
|
use function preg_match;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Validates whether the input is a Polish identity card (Dowód Osobisty).
|
|
|
|
*
|
|
|
|
* @see https://en.wikipedia.org/wiki/Polish_identity_card
|
|
|
|
*
|
|
|
|
* @author Henrique Moody <henriquemoody@gmail.com>
|
|
|
|
*/
|
|
|
|
final class PolishIdCard extends AbstractRule
|
|
|
|
{
|
|
|
|
private const ASCII_CODE_0 = 48;
|
|
|
|
private const ASCII_CODE_7 = 55;
|
|
|
|
private const ASCII_CODE_9 = 57;
|
|
|
|
private const ASCII_CODE_A = 65;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
public function validate($input): bool
|
|
|
|
{
|
2024-01-31 22:15:08 +08:00
|
|
|
if (!is_scalar($input)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$input = (string) $input;
|
|
|
|
|
2022-12-24 22:10:40 +08:00
|
|
|
if (!preg_match('/^[A-Z0-9]{9}$/', $input)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$weights = [7, 3, 1, 0, 7, 3, 1, 7, 3];
|
|
|
|
$weightedSum = 0;
|
|
|
|
for ($i = 0; $i < 9; ++$i) {
|
|
|
|
$code = ord($input[$i]);
|
|
|
|
if ($i < 3 && $code <= self::ASCII_CODE_9) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($i > 2 && $code >= self::ASCII_CODE_A) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$difference = $code <= self::ASCII_CODE_9 ? self::ASCII_CODE_0 : self::ASCII_CODE_7;
|
|
|
|
$weightedSum += ($code - $difference) * $weights[$i];
|
|
|
|
}
|
|
|
|
|
|
|
|
return $weightedSum % 10 == $input[3];
|
|
|
|
}
|
|
|
|
}
|