4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Validator\Constraints;
14 use Symfony\Component\Validator\Context\ExecutionContextInterface;
15 use Symfony\Component\Validator\Constraint;
16 use Symfony\Component\Validator\ConstraintValidator;
17 use Symfony\Component\Validator\Exception\UnexpectedTypeException;
20 * Validates a PAN using the LUHN Algorithm.
22 * For a list of example card numbers that are used to test this
23 * class, please see the LuhnValidatorTest class.
25 * @see http://en.wikipedia.org/wiki/Luhn_algorithm
27 * @author Tim Nagel <t.nagel@infinite.net.au>
28 * @author Greg Knapp http://gregk.me/2011/php-implementation-of-bank-card-luhn-algorithm/
29 * @author Bernhard Schussek <bschussek@gmail.com>
31 class LuhnValidator extends ConstraintValidator
34 * Validates a credit card number with the Luhn algorithm.
37 * @param Constraint $constraint
39 * @throws UnexpectedTypeException when the given credit card number is no string
41 public function validate($value, Constraint $constraint)
43 if (!$constraint instanceof Luhn) {
44 throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Luhn');
47 if (null === $value || '' === $value) {
51 // Work with strings only, because long numbers are represented as floats
52 // internally and don't work with strlen()
53 if (!is_string($value) && !(is_object($value) && method_exists($value, '__toString'))) {
54 throw new UnexpectedTypeException($value, 'string');
57 $value = (string) $value;
59 if (!ctype_digit($value)) {
60 if ($this->context instanceof ExecutionContextInterface) {
61 $this->context->buildViolation($constraint->message)
62 ->setParameter('{{ value }}', $this->formatValue($value))
63 ->setCode(Luhn::INVALID_CHARACTERS_ERROR)
66 $this->buildViolation($constraint->message)
67 ->setParameter('{{ value }}', $this->formatValue($value))
68 ->setCode(Luhn::INVALID_CHARACTERS_ERROR)
76 $length = strlen($value);
78 // Starting with the last digit and walking left, add every second
79 // digit to the check sum
80 // e.g. 7 9 9 2 7 3 9 8 7 1 3
82 // = 7 + 9 + 7 + 9 + 7 + 3
83 for ($i = $length - 1; $i >= 0; $i -= 2) {
84 $checkSum += $value[$i];
87 // Starting with the second last digit and walking left, double every
88 // second digit and add it to the check sum
89 // For doubles greater than 9, sum the individual digits
90 // e.g. 7 9 9 2 7 3 9 8 7 1 3
92 // = 1+8 + 4 + 6 + 1+6 + 2
93 for ($i = $length - 2; $i >= 0; $i -= 2) {
94 $checkSum += array_sum(str_split($value[$i] * 2));
97 if (0 === $checkSum || 0 !== $checkSum % 10) {
98 if ($this->context instanceof ExecutionContextInterface) {
99 $this->context->buildViolation($constraint->message)
100 ->setParameter('{{ value }}', $this->formatValue($value))
101 ->setCode(Luhn::CHECKSUM_FAILED_ERROR)
104 $this->buildViolation($constraint->message)
105 ->setParameter('{{ value }}', $this->formatValue($value))
106 ->setCode(Luhn::CHECKSUM_FAILED_ERROR)