Version 1
[yaffs-website] / vendor / symfony / validator / Constraints / CardSchemeValidator.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 that a card number belongs to a specified scheme.
21  *
22  * @author Tim Nagel <t.nagel@infinite.net.au>
23  * @author Bernhard Schussek <bschussek@gmail.com>
24  *
25  * @see http://en.wikipedia.org/wiki/Bank_card_number
26  * @see http://www.regular-expressions.info/creditcard.html
27  * @see http://www.barclaycard.co.uk/business/files/Ranges_and_Rules_September_2014.pdf
28  */
29 class CardSchemeValidator extends ConstraintValidator
30 {
31     protected $schemes = array(
32         // American Express card numbers start with 34 or 37 and have 15 digits.
33         'AMEX' => array(
34             '/^3[47][0-9]{13}$/',
35         ),
36         // China UnionPay cards start with 62 and have between 16 and 19 digits.
37         // Please note that these cards do not follow Luhn Algorithm as a checksum.
38         'CHINA_UNIONPAY' => array(
39             '/^62[0-9]{14,17}$/',
40         ),
41         // Diners Club card numbers begin with 300 through 305, 36 or 38. All have 14 digits.
42         // There are Diners Club cards that begin with 5 and have 16 digits.
43         // These are a joint venture between Diners Club and MasterCard, and should be processed like a MasterCard.
44         'DINERS' => array(
45             '/^3(?:0[0-5]|[68][0-9])[0-9]{11}$/',
46         ),
47         // Discover card numbers begin with 6011, 622126 through 622925, 644 through 649 or 65.
48         // All have 16 digits.
49         'DISCOVER' => array(
50             '/^6011[0-9]{12}$/',
51             '/^64[4-9][0-9]{13}$/',
52             '/^65[0-9]{14}$/',
53             '/^622(12[6-9]|1[3-9][0-9]|[2-8][0-9][0-9]|91[0-9]|92[0-5])[0-9]{10}$/',
54         ),
55         // InstaPayment cards begin with 637 through 639 and have 16 digits.
56         'INSTAPAYMENT' => array(
57             '/^63[7-9][0-9]{13}$/',
58         ),
59         // JCB cards beginning with 2131 or 1800 have 15 digits.
60         // JCB cards beginning with 35 have 16 digits.
61         'JCB' => array(
62             '/^(?:2131|1800|35[0-9]{3})[0-9]{11}$/',
63         ),
64         // Laser cards begin with either 6304, 6706, 6709 or 6771 and have between 16 and 19 digits.
65         'LASER' => array(
66             '/^(6304|670[69]|6771)[0-9]{12,15}$/',
67         ),
68         // Maestro international cards begin with 675900..675999 and have between 12 and 19 digits.
69         // Maestro UK cards begin with either 500000..509999 or 560000..699999 and have between 12 and 19 digits.
70         'MAESTRO' => array(
71             '/^(6759[0-9]{2})[0-9]{6,13}$/',
72             '/^(50[0-9]{4})[0-9]{6,13}$/',
73             '/^5[6-9][0-9]{10,17}$/',
74             '/^6[0-9]{11,18}$/',
75         ),
76         // All MasterCard numbers start with the numbers 51 through 55. All have 16 digits.
77         // October 2016 MasterCard numbers can also start with 222100 through 272099.
78         'MASTERCARD' => array(
79             '/^5[1-5][0-9]{14}$/',
80             '/^2(22[1-9][0-9]{12}|2[3-9][0-9]{13}|[3-6][0-9]{14}|7[0-1][0-9]{13}|720[0-9]{12})$/',
81         ),
82         // All Visa card numbers start with a 4. New cards have 16 digits. Old cards have 13.
83         'VISA' => array(
84             '/^4([0-9]{12}|[0-9]{15})$/',
85         ),
86     );
87
88     /**
89      * Validates a creditcard belongs to a specified scheme.
90      *
91      * @param mixed      $value
92      * @param Constraint $constraint
93      */
94     public function validate($value, Constraint $constraint)
95     {
96         if (!$constraint instanceof CardScheme) {
97             throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\CardScheme');
98         }
99
100         if (null === $value || '' === $value) {
101             return;
102         }
103
104         if (!is_numeric($value)) {
105             if ($this->context instanceof ExecutionContextInterface) {
106                 $this->context->buildViolation($constraint->message)
107                     ->setParameter('{{ value }}', $this->formatValue($value))
108                     ->setCode(CardScheme::NOT_NUMERIC_ERROR)
109                     ->addViolation();
110             } else {
111                 $this->buildViolation($constraint->message)
112                     ->setParameter('{{ value }}', $this->formatValue($value))
113                     ->setCode(CardScheme::NOT_NUMERIC_ERROR)
114                     ->addViolation();
115             }
116
117             return;
118         }
119
120         $schemes = array_flip((array) $constraint->schemes);
121         $schemeRegexes = array_intersect_key($this->schemes, $schemes);
122
123         foreach ($schemeRegexes as $regexes) {
124             foreach ($regexes as $regex) {
125                 if (preg_match($regex, $value)) {
126                     return;
127                 }
128             }
129         }
130
131         if ($this->context instanceof ExecutionContextInterface) {
132             $this->context->buildViolation($constraint->message)
133                 ->setParameter('{{ value }}', $this->formatValue($value))
134                 ->setCode(CardScheme::INVALID_FORMAT_ERROR)
135                 ->addViolation();
136         } else {
137             $this->buildViolation($constraint->message)
138                 ->setParameter('{{ value }}', $this->formatValue($value))
139                 ->setCode(CardScheme::INVALID_FORMAT_ERROR)
140                 ->addViolation();
141         }
142     }
143 }