This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
symfony/src/Symfony/Component/Validator/Constraints/LuhnValidator.php

112 lines
4.0 KiB
PHP
Raw Normal View History

2012-07-03 23:45:52 +01:00
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Validator\Constraints;
use Symfony\Component\Validator\Context\ExecutionContextInterface;
2012-07-03 23:45:52 +01:00
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
2012-07-03 23:45:52 +01:00
/**
2014-12-21 17:00:50 +00:00
* Validates a PAN using the LUHN Algorithm.
2012-07-03 23:45:52 +01:00
*
* For a list of example card numbers that are used to test this
* class, please see the LuhnValidatorTest class.
*
* @see http://en.wikipedia.org/wiki/Luhn_algorithm
2014-12-21 17:00:50 +00:00
*
2012-07-03 23:45:52 +01:00
* @author Tim Nagel <t.nagel@infinite.net.au>
* @author Greg Knapp http://gregk.me/2011/php-implementation-of-bank-card-luhn-algorithm/
* @author Bernhard Schussek <bschussek@gmail.com>
2012-07-03 23:45:52 +01:00
*/
class LuhnValidator extends ConstraintValidator
{
/**
* Validates a credit card number with the Luhn algorithm.
2012-07-03 23:45:52 +01:00
*
* @param mixed $value
* @param Constraint $constraint
2014-07-26 13:09:47 +01:00
*
* @throws UnexpectedTypeException when the given credit card number is no string
2012-07-03 23:45:52 +01:00
*/
public function validate($value, Constraint $constraint)
{
if (!$constraint instanceof Luhn) {
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Luhn');
}
2012-07-03 23:45:52 +01:00
if (null === $value || '' === $value) {
return;
}
// Work with strings only, because long numbers are represented as floats
// internally and don't work with strlen()
if (!is_string($value) && !(is_object($value) && method_exists($value, '__toString'))) {
throw new UnexpectedTypeException($value, 'string');
}
$value = (string) $value;
if (!ctype_digit($value)) {
if ($this->context instanceof ExecutionContextInterface) {
$this->context->buildViolation($constraint->message)
->setParameter('{{ value }}', $this->formatValue($value))
->setCode(Luhn::INVALID_CHARACTERS_ERROR)
->addViolation();
} else {
$this->buildViolation($constraint->message)
->setParameter('{{ value }}', $this->formatValue($value))
->setCode(Luhn::INVALID_CHARACTERS_ERROR)
->addViolation();
}
2012-07-03 23:45:52 +01:00
return;
}
$checkSum = 0;
2012-07-03 23:45:52 +01:00
$length = strlen($value);
// Starting with the last digit and walking left, add every second
// digit to the check sum
// e.g. 7 9 9 2 7 3 9 8 7 1 3
// ^ ^ ^ ^ ^ ^
// = 7 + 9 + 7 + 9 + 7 + 3
for ($i = $length - 1; $i >= 0; $i -= 2) {
2016-10-30 09:34:06 +00:00
$checkSum += $value[$i];
}
// Starting with the second last digit and walking left, double every
// second digit and add it to the check sum
// For doubles greater than 9, sum the individual digits
// e.g. 7 9 9 2 7 3 9 8 7 1 3
// ^ ^ ^ ^ ^
// = 1+8 + 4 + 6 + 1+6 + 2
for ($i = $length - 2; $i >= 0; $i -= 2) {
2016-10-30 09:34:06 +00:00
$checkSum += array_sum(str_split($value[$i] * 2));
2012-07-03 23:45:52 +01:00
}
if (0 === $checkSum || 0 !== $checkSum % 10) {
if ($this->context instanceof ExecutionContextInterface) {
$this->context->buildViolation($constraint->message)
->setParameter('{{ value }}', $this->formatValue($value))
->setCode(Luhn::CHECKSUM_FAILED_ERROR)
->addViolation();
} else {
$this->buildViolation($constraint->message)
->setParameter('{{ value }}', $this->formatValue($value))
->setCode(Luhn::CHECKSUM_FAILED_ERROR)
->addViolation();
}
2012-07-03 23:45:52 +01:00
}
}
}