Luhn.php 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <?php
  2. /*
  3. * Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
  4. * SPDX-License-Identifier: MIT
  5. */
  6. declare(strict_types=1);
  7. namespace Respect\Validation\Rules;
  8. use function array_map;
  9. use function count;
  10. use function str_split;
  11. /**
  12. * Validate whether a given input is a Luhn number.
  13. *
  14. * @see https://en.wikipedia.org/wiki/Luhn_algorithm
  15. *
  16. * @author Alexander Gorshkov <mazanax@yandex.ru>
  17. * @author Danilo Correa <danilosilva87@gmail.com>
  18. * @author Henrique Moody <henriquemoody@gmail.com>
  19. */
  20. final class Luhn extends AbstractRule
  21. {
  22. /**
  23. * {@inheritDoc}
  24. */
  25. public function validate($input): bool
  26. {
  27. if (!(new Digit())->validate($input)) {
  28. return false;
  29. }
  30. return $this->isValid((string) $input);
  31. }
  32. private function isValid(string $input): bool
  33. {
  34. $sum = 0;
  35. $digits = array_map('intval', str_split($input));
  36. $numDigits = count($digits);
  37. $parity = $numDigits % 2;
  38. for ($i = 0; $i < $numDigits; ++$i) {
  39. $digit = $digits[$i];
  40. if ($parity == $i % 2) {
  41. $digit <<= 1;
  42. if (9 < $digit) {
  43. $digit = $digit - 9;
  44. }
  45. }
  46. $sum += $digit;
  47. }
  48. return $sum % 10 == 0;
  49. }
  50. }