<?php
class NIP
{
private const WEIGHTS = [6, 5, 7, 2, 3, 4, 5, 6, 7];
private const NIP_MODULO = 11;
private const INVALID_CHECKSUM = 10;
public static function validate(string $nip): bool
{
$value = preg_replace('/[\s-]/', '', $nip);
if ('0000000000' === $value || !preg_match('/^\d{10}$/', $value)) {
return false;
}
$checkSum = self::getChecksum($value);
if (self::INVALID_CHECKSUM === $checkSum || $checkSum !== ((int) (substr($value, -1)))) {
return false;
}
return true;
}
private static function getChecksum(string $nip): int
{
$sum = 0;
foreach (self::WEIGHTS as $key => $weight) {
$sum += $weight * ((int) $nip[$key]);
}
return $sum % self::NIP_MODULO;
}
}
$nip_numbers = array(
"1074238796",
"5366239267",
"1071206063",
"1160045242",
"8123035307",
"5361869035",
"9322423343",
"1191232175",
"1079704010",
"1560650337"
);
$c = new NIP();
foreach ($nip_numbers as $a) {
var_dump($c->validate($a));
}
- Output for 8.1.0 - 8.1.30, 8.2.0 - 8.2.24, 8.3.0 - 8.3.12
- bool(true)
bool(true)
bool(true)
bool(true)
bool(true)
bool(true)
bool(true)
bool(true)
bool(true)
bool(true)
preferences:
46.45 ms | 406 KiB | 5 Q