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;
|
|
|
|
|
|
|
|
use function ctype_digit;
|
2024-01-31 22:15:08 +08:00
|
|
|
use function intval;
|
|
|
|
use function is_scalar;
|
2022-12-24 22:10:40 +08:00
|
|
|
use function mb_strlen;
|
2024-01-31 22:15:08 +08:00
|
|
|
use function strval;
|
2022-12-24 22:10:40 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Validates a Dutch citizen service number (BSN).
|
|
|
|
*
|
|
|
|
* @see https://nl.wikipedia.org/wiki/Burgerservicenummer
|
|
|
|
*
|
|
|
|
* @author Henrique Moody <henriquemoody@gmail.com>
|
|
|
|
* @author Ronald Drenth <ronalddrenth@gmail.com>
|
|
|
|
* @author William Espindola <oi@williamespindola.com.br>
|
|
|
|
*/
|
|
|
|
final class Bsn extends AbstractRule
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* {@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 (!ctype_digit($input)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2024-01-31 22:15:08 +08:00
|
|
|
if (mb_strlen(strval($input)) !== 9) {
|
2022-12-24 22:10:40 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2024-01-31 22:15:08 +08:00
|
|
|
$sum = -1 * intval($input[8]);
|
2022-12-24 22:10:40 +08:00
|
|
|
for ($i = 9; $i > 1; --$i) {
|
2024-01-31 22:15:08 +08:00
|
|
|
$sum += $i * intval($input[9 - $i]);
|
2022-12-24 22:10:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return $sum !== 0 && $sum % 11 === 0;
|
|
|
|
}
|
|
|
|
}
|