respect-validation/library/Rules/Luhn.php

59 lines
1.3 KiB
PHP
Raw Permalink Normal View History

2017-08-18 13:47:36 +02:00
<?php
/*
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
* SPDX-License-Identifier: MIT
2017-08-18 13:47:36 +02:00
*/
declare(strict_types=1);
2017-08-18 13:47:36 +02:00
namespace Respect\Validation\Rules;
use function array_map;
use function count;
use function str_split;
2017-08-18 13:47:36 +02:00
/**
* Validate whether a given input is a Luhn number.
*
* @see https://en.wikipedia.org/wiki/Luhn_algorithm
*
* @author Alexander Gorshkov <mazanax@yandex.ru>
* @author Danilo Correa <danilosilva87@gmail.com>
* @author Henrique Moody <henriquemoody@gmail.com>
2017-08-18 13:47:36 +02:00
*/
final class Luhn extends AbstractRule
2017-08-18 13:47:36 +02:00
{
/**
* {@inheritDoc}
2017-08-18 13:47:36 +02:00
*/
public function validate($input): bool
2017-08-18 13:47:36 +02:00
{
if (!(new Digit())->validate($input)) {
return false;
}
return $this->isValid((string) $input);
2017-08-18 13:47:36 +02:00
}
private function isValid(string $input): bool
2017-08-18 13:47:36 +02:00
{
$sum = 0;
$digits = array_map('intval', str_split($input));
$numDigits = count($digits);
2017-08-18 13:47:36 +02:00
$parity = $numDigits % 2;
for ($i = 0; $i < $numDigits; ++$i) {
$digit = $digits[$i];
if ($parity == $i % 2) {
2017-08-18 13:47:36 +02:00
$digit <<= 1;
if (9 < $digit) {
$digit = $digit - 9;
}
}
$sum += $digit;
}
return $sum % 10 == 0;
2017-08-18 13:47:36 +02:00
}
}