PHP_CodeSniffer / PHP_CodeSniffer (push) Successful in 1m34s
PHPStan / PHPStan with PHP 8.5 prefer-lowest (push) Successful in 1m42s
PHPStan / PHPStan with PHP 8.1 prefer-stable (push) Successful in 1m56s
PHPStan / PHPStan with PHP 8.1 prefer-lowest (push) Successful in 1m56s
PHPStan / PHPStan with PHP 8.5 prefer-stable (push) Successful in 55s
PHPStan / PHPStan with PHP 8.6 prefer-source (push) Successful in 1m0s
PHPUnit / PHPUnit with PHP 8.1 prefer-lowest (push) Successful in 1m19s
PHPUnit / PHPUnit with PHP 8.1 prefer-stable (push) Successful in 1m23s
PHPUnit / PHPUnit with PHP 8.5 prefer-lowest (push) Successful in 55s
Smoke / Produce an output (push) Successful in 17s
PHPUnit / PHPUnit with PHP 8.5 prefer-stable (push) Successful in 57s
Smoke / Consume the output (push) Successful in 13s
PHPUnit / PHPUnit with PHP 8.6 prefer-source (push) Successful in 53s
52 lines
1.1 KiB
PHP
52 lines
1.1 KiB
PHP
<?php
|
|
|
|
declare(strict_types = 1);
|
|
|
|
namespace Systopia\PipelineTest;
|
|
|
|
final class IbanValidator {
|
|
|
|
private const LENGTHS = [
|
|
'AT' => 20,
|
|
'BE' => 16,
|
|
'CH' => 21,
|
|
'DE' => 22,
|
|
'ES' => 24,
|
|
'FR' => 27,
|
|
'IT' => 27,
|
|
'LU' => 20,
|
|
'NL' => 18,
|
|
];
|
|
|
|
public function isValid(string $iban): bool {
|
|
$normalized = $this->normalize($iban);
|
|
|
|
if (1 !== preg_match('/^[A-Z]{2}[0-9]{2}[A-Z0-9]+$/', $normalized)) {
|
|
return FALSE;
|
|
}
|
|
|
|
if (strlen($normalized) !== (self::LENGTHS[substr($normalized, 0, 2)] ?? NULL)) {
|
|
return FALSE;
|
|
}
|
|
|
|
return 1 === $this->mod97(substr($normalized, 4) . substr($normalized, 0, 4));
|
|
}
|
|
|
|
public function normalize(string $iban): string {
|
|
return strtoupper(str_replace([' ', '-'], '', $iban));
|
|
}
|
|
|
|
private function mod97(string $rearranged): int {
|
|
$remainder = 0;
|
|
foreach (str_split($rearranged) as $character) {
|
|
$digits = ctype_digit($character) ? $character : (string) (ord($character) - 55);
|
|
foreach (str_split($digits) as $digit) {
|
|
$remainder = ($remainder * 10 + (int) $digit) % 97;
|
|
}
|
|
}
|
|
|
|
return $remainder;
|
|
}
|
|
|
|
}
|