LuhnValidator.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Validator\Constraints;
  11. use Symfony\Component\Validator\Constraint;
  12. use Symfony\Component\Validator\ConstraintValidator;
  13. use Symfony\Component\Validator\Exception\UnexpectedTypeException;
  14. use Symfony\Component\Validator\Exception\UnexpectedValueException;
  15. /**
  16. * Validates a PAN using the LUHN Algorithm.
  17. *
  18. * For a list of example card numbers that are used to test this
  19. * class, please see the LuhnValidatorTest class.
  20. *
  21. * @see http://en.wikipedia.org/wiki/Luhn_algorithm
  22. *
  23. * @author Tim Nagel <t.nagel@infinite.net.au>
  24. * @author Greg Knapp http://gregk.me/2011/php-implementation-of-bank-card-luhn-algorithm/
  25. * @author Bernhard Schussek <bschussek@gmail.com>
  26. */
  27. class LuhnValidator extends ConstraintValidator
  28. {
  29. /**
  30. * Validates a credit card number with the Luhn algorithm.
  31. *
  32. * @param mixed $value
  33. *
  34. * @throws UnexpectedTypeException when the given credit card number is no string
  35. */
  36. public function validate($value, Constraint $constraint)
  37. {
  38. if (!$constraint instanceof Luhn) {
  39. throw new UnexpectedTypeException($constraint, Luhn::class);
  40. }
  41. if (null === $value || '' === $value) {
  42. return;
  43. }
  44. // Work with strings only, because long numbers are represented as floats
  45. // internally and don't work with strlen()
  46. if (!\is_string($value) && !(\is_object($value) && method_exists($value, '__toString'))) {
  47. throw new UnexpectedValueException($value, 'string');
  48. }
  49. $value = (string) $value;
  50. if (!ctype_digit($value)) {
  51. $this->context->buildViolation($constraint->message)
  52. ->setParameter('{{ value }}', $this->formatValue($value))
  53. ->setCode(Luhn::INVALID_CHARACTERS_ERROR)
  54. ->addViolation();
  55. return;
  56. }
  57. $checkSum = 0;
  58. $length = \strlen($value);
  59. // Starting with the last digit and walking left, add every second
  60. // digit to the check sum
  61. // e.g. 7 9 9 2 7 3 9 8 7 1 3
  62. // ^ ^ ^ ^ ^ ^
  63. // = 7 + 9 + 7 + 9 + 7 + 3
  64. for ($i = $length - 1; $i >= 0; $i -= 2) {
  65. $checkSum += $value[$i];
  66. }
  67. // Starting with the second last digit and walking left, double every
  68. // second digit and add it to the check sum
  69. // For doubles greater than 9, sum the individual digits
  70. // e.g. 7 9 9 2 7 3 9 8 7 1 3
  71. // ^ ^ ^ ^ ^
  72. // = 1+8 + 4 + 6 + 1+6 + 2
  73. for ($i = $length - 2; $i >= 0; $i -= 2) {
  74. $checkSum += array_sum(str_split((int) $value[$i] * 2));
  75. }
  76. if (0 === $checkSum || 0 !== $checkSum % 10) {
  77. $this->context->buildViolation($constraint->message)
  78. ->setParameter('{{ value }}', $this->formatValue($value))
  79. ->setCode(Luhn::CHECKSUM_FAILED_ERROR)
  80. ->addViolation();
  81. }
  82. }
  83. }