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