2015-10-16 21:46:08 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
2023-02-17 05:13:51 +01:00
|
|
|
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
|
|
|
|
* SPDX-License-Identifier: MIT
|
2015-10-16 21:46:08 +02:00
|
|
|
*/
|
|
|
|
|
2017-11-19 19:01:31 +01:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2015-10-16 21:46:08 +02:00
|
|
|
namespace Respect\Validation\Rules;
|
|
|
|
|
2018-06-10 05:41:24 +02:00
|
|
|
use function ctype_digit;
|
2023-02-13 05:59:06 +01:00
|
|
|
use function intval;
|
2023-02-18 17:38:47 +01:00
|
|
|
use function is_scalar;
|
2018-06-10 05:41:24 +02:00
|
|
|
use function mb_strlen;
|
2023-02-13 05:59:06 +01:00
|
|
|
use function strval;
|
2018-06-10 05:41:24 +02:00
|
|
|
|
2015-10-16 21:46:08 +02:00
|
|
|
/**
|
2015-10-24 05:32:07 +02:00
|
|
|
* Validates a Dutch citizen service number (BSN).
|
2015-10-16 21:46:08 +02:00
|
|
|
*
|
|
|
|
* @see https://nl.wikipedia.org/wiki/Burgerservicenummer
|
2018-06-10 05:41:24 +02:00
|
|
|
*
|
|
|
|
* @author Henrique Moody <henriquemoody@gmail.com>
|
|
|
|
* @author Ronald Drenth <ronalddrenth@gmail.com>
|
|
|
|
* @author William Espindola <oi@williamespindola.com.br>
|
2015-10-16 21:46:08 +02:00
|
|
|
*/
|
2018-06-10 05:41:24 +02:00
|
|
|
final class Bsn extends AbstractRule
|
2015-10-16 21:46:08 +02:00
|
|
|
{
|
|
|
|
/**
|
2019-04-05 14:33:00 +02:00
|
|
|
* {@inheritDoc}
|
2015-10-16 21:46:08 +02:00
|
|
|
*/
|
2018-01-28 15:41:31 +01:00
|
|
|
public function validate($input): bool
|
2015-10-16 21:46:08 +02:00
|
|
|
{
|
2023-02-18 17:38:47 +01:00
|
|
|
if (!is_scalar($input)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$input = (string) $input;
|
|
|
|
|
2015-10-16 21:46:08 +02:00
|
|
|
if (!ctype_digit($input)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-02-13 05:59:06 +01:00
|
|
|
if (mb_strlen(strval($input)) !== 9) {
|
2015-10-16 21:46:08 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-02-18 17:38:47 +01:00
|
|
|
$sum = -1 * intval($input[8]);
|
2015-10-24 05:32:07 +02:00
|
|
|
for ($i = 9; $i > 1; --$i) {
|
2023-02-18 17:38:47 +01:00
|
|
|
$sum += $i * intval($input[9 - $i]);
|
2015-10-16 21:46:08 +02:00
|
|
|
}
|
|
|
|
|
2019-02-09 13:28:08 +01:00
|
|
|
return $sum !== 0 && $sum % 11 === 0;
|
2015-10-16 21:46:08 +02:00
|
|
|
}
|
|
|
|
}
|