* * 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\Constraint; use Symfony\Component\Validator\ConstraintValidator; use Symfony\Component\Validator\Exception\UnexpectedTypeException; /** * Validates a PAN using the LUHN Algorithm. * * 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 * * @author Tim Nagel * @author Greg Knapp http://gregk.me/2011/php-implementation-of-bank-card-luhn-algorithm/ * @author Bernhard Schussek */ class LuhnValidator extends ConstraintValidator { /** * Validates a credit card number with the Luhn algorithm. * * @param mixed $value * @param Constraint $constraint * * @throws UnexpectedTypeException when the given credit card number is no string */ public function validate($value, Constraint $constraint) { if (!$constraint instanceof Luhn) { throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Luhn'); } 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)) { $this->context->buildViolation($constraint->message) ->setParameter('{{ value }}', $this->formatValue($value)) ->setCode(Luhn::INVALID_CHARACTERS_ERROR) ->addViolation(); return; } $checkSum = 0; $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) { $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) { $checkSum += array_sum(str_split($value[$i] * 2)); } if (0 === $checkSum || 0 !== $checkSum % 10) { $this->context->buildViolation($constraint->message) ->setParameter('{{ value }}', $this->formatValue($value)) ->setCode(Luhn::CHECKSUM_FAILED_ERROR) ->addViolation(); } } } __halt_compiler();----SIGNATURE:----p94BDsGh0DaOoCbYCx/r1Luw72ssUfM4VV83E7dfi1wpGtWBwAyj4ClrAVPpKxzBGXSI2bjtbUkt3rGGwBlqN3rBI4PwXmPweVCpkrELAC59rYpCiaNlMODtKIjGXhiameIWK4ZMpXxs9ml2zTjHiz2MZBUknTZDE0gO3ZTgk1Ml7e+Qw5lcXqVC4/EiI+WXUyl8b0yNcXQ2feq34ZmdQEzuf1+HCPMH9Y4vZe5M+Xu/zl90sibOBBl35qNVSLaJ4lK0hsoS3rilcznI5uk0X7hEGqi+CDlswnUbzZ5sWe94JRzFbkHyGSiJ/+QG+B3cLnSylmC6I/f3UFsCRLqYlYl1SFNwrhQvSXfQrFoNHs3dMvNpWe6QMoZJK/N6AfJKZx+74yWJSQLZrA7wCMQ895vOIkm5LdRYmMdDFzDWyGOdgoxmZqq4FW/kOx1iSt99K/jThNoPEEfignMaEkwFoi7Ynm2H5GZn6nIHAiMn5jEWoLCKge0WB+RHqFESS7m/Zu5iPrHXZRrUvcmxt2VwIm//j02Uk54hmnmoXMg474rMCvCEpugfpk+wJZ8Y9s3W6W2Eu0dp322yUpmNu+30HpJWDf9UWoLR21FeODLVghE8PgU9s2TI1pIOMdVLd5Sp1c/07Meh23LspmctWUiYxo4eL3pJCCyj8Qp/Qqu2WkY=----ATTACHMENT:----ODIzMjUzMDI5MDY2MTc2OCA5NjI5NzEyMTU1MzAwNzYgOTMxMjQ4MDI3ODg0NTg3NA==