LuhnValidator.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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\Context\ExecutionContextInterface;
  14. use Symfony\Component\Validator\Exception\UnexpectedTypeException;
  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. * @param Constraint $constraint
  34. *
  35. * @throws UnexpectedTypeException when the given credit card number is no string
  36. */
  37. public function validate($value, Constraint $constraint)
  38. {
  39. if (!$constraint instanceof Luhn) {
  40. throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Luhn');
  41. }
  42. if (null === $value || '' === $value) {
  43. return;
  44. }
  45. // Work with strings only, because long numbers are represented as floats
  46. // internally and don't work with strlen()
  47. if (!\is_string($value) && !(\is_object($value) && method_exists($value, '__toString'))) {
  48. throw new UnexpectedTypeException($value, 'string');
  49. }
  50. $value = (string) $value;
  51. if (!ctype_digit($value)) {
  52. if ($this->context instanceof ExecutionContextInterface) {
  53. $this->context->buildViolation($constraint->message)
  54. ->setParameter('{{ value }}', $this->formatValue($value))
  55. ->setCode(Luhn::INVALID_CHARACTERS_ERROR)
  56. ->addViolation();
  57. } else {
  58. $this->buildViolation($constraint->message)
  59. ->setParameter('{{ value }}', $this->formatValue($value))
  60. ->setCode(Luhn::INVALID_CHARACTERS_ERROR)
  61. ->addViolation();
  62. }
  63. return;
  64. }
  65. $checkSum = 0;
  66. $length = \strlen($value);
  67. // Starting with the last digit and walking left, add every second
  68. // digit to the check sum
  69. // e.g. 7 9 9 2 7 3 9 8 7 1 3
  70. // ^ ^ ^ ^ ^ ^
  71. // = 7 + 9 + 7 + 9 + 7 + 3
  72. for ($i = $length - 1; $i >= 0; $i -= 2) {
  73. $checkSum += $value[$i];
  74. }
  75. // Starting with the second last digit and walking left, double every
  76. // second digit and add it to the check sum
  77. // For doubles greater than 9, sum the individual digits
  78. // e.g. 7 9 9 2 7 3 9 8 7 1 3
  79. // ^ ^ ^ ^ ^
  80. // = 1+8 + 4 + 6 + 1+6 + 2
  81. for ($i = $length - 2; $i >= 0; $i -= 2) {
  82. $checkSum += array_sum(str_split($value[$i] * 2));
  83. }
  84. if (0 === $checkSum || 0 !== $checkSum % 10) {
  85. if ($this->context instanceof ExecutionContextInterface) {
  86. $this->context->buildViolation($constraint->message)
  87. ->setParameter('{{ value }}', $this->formatValue($value))
  88. ->setCode(Luhn::CHECKSUM_FAILED_ERROR)
  89. ->addViolation();
  90. } else {
  91. $this->buildViolation($constraint->message)
  92. ->setParameter('{{ value }}', $this->formatValue($value))
  93. ->setCode(Luhn::CHECKSUM_FAILED_ERROR)
  94. ->addViolation();
  95. }
  96. }
  97. }
  98. }