Version 1
[yaffs-website] / vendor / symfony / validator / Constraints / LuhnValidator.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Component\Validator\Constraints;
13
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;
18
19 /**
20  * Validates a PAN using the LUHN Algorithm.
21  *
22  * For a list of example card numbers that are used to test this
23  * class, please see the LuhnValidatorTest class.
24  *
25  * @see    http://en.wikipedia.org/wiki/Luhn_algorithm
26  *
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>
30  */
31 class LuhnValidator extends ConstraintValidator
32 {
33     /**
34      * Validates a credit card number with the Luhn algorithm.
35      *
36      * @param mixed      $value
37      * @param Constraint $constraint
38      *
39      * @throws UnexpectedTypeException when the given credit card number is no string
40      */
41     public function validate($value, Constraint $constraint)
42     {
43         if (!$constraint instanceof Luhn) {
44             throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Luhn');
45         }
46
47         if (null === $value || '' === $value) {
48             return;
49         }
50
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');
55         }
56
57         $value = (string) $value;
58
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)
64                     ->addViolation();
65             } else {
66                 $this->buildViolation($constraint->message)
67                     ->setParameter('{{ value }}', $this->formatValue($value))
68                     ->setCode(Luhn::INVALID_CHARACTERS_ERROR)
69                     ->addViolation();
70             }
71
72             return;
73         }
74
75         $checkSum = 0;
76         $length = strlen($value);
77
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
81         //      ^     ^     ^     ^     ^     ^
82         //    = 7  +  9  +  7  +  9  +  7  +  3
83         for ($i = $length - 1; $i >= 0; $i -= 2) {
84             $checkSum += $value[$i];
85         }
86
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
91         //         ^     ^     ^     ^     ^
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));
95         }
96
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)
102                     ->addViolation();
103             } else {
104                 $this->buildViolation($constraint->message)
105                     ->setParameter('{{ value }}', $this->formatValue($value))
106                     ->setCode(Luhn::CHECKSUM_FAILED_ERROR)
107                     ->addViolation();
108             }
109         }
110     }
111 }