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