4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Validator\Constraints;
14 use Symfony\Component\Validator\Constraint;
15 use Symfony\Component\Validator\ConstraintValidator;
16 use Symfony\Component\Validator\Exception\UnexpectedTypeException;
19 * Validates that a card number belongs to a specified scheme.
21 * @author Tim Nagel <t.nagel@infinite.net.au>
22 * @author Bernhard Schussek <bschussek@gmail.com>
24 * @see http://en.wikipedia.org/wiki/Bank_card_number
25 * @see http://www.regular-expressions.info/creditcard.html
26 * @see http://www.barclaycard.co.uk/business/files/Ranges_and_Rules_September_2014.pdf
28 class CardSchemeValidator extends ConstraintValidator
30 protected $schemes = array(
31 // American Express card numbers start with 34 or 37 and have 15 digits.
35 // China UnionPay cards start with 62 and have between 16 and 19 digits.
36 // Please note that these cards do not follow Luhn Algorithm as a checksum.
37 'CHINA_UNIONPAY' => array(
40 // Diners Club card numbers begin with 300 through 305, 36 or 38. All have 14 digits.
41 // There are Diners Club cards that begin with 5 and have 16 digits.
42 // These are a joint venture between Diners Club and MasterCard, and should be processed like a MasterCard.
44 '/^3(?:0[0-5]|[68][0-9])[0-9]{11}$/',
46 // Discover card numbers begin with 6011, 622126 through 622925, 644 through 649 or 65.
47 // All have 16 digits.
50 '/^64[4-9][0-9]{13}$/',
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}$/',
54 // InstaPayment cards begin with 637 through 639 and have 16 digits.
55 'INSTAPAYMENT' => array(
56 '/^63[7-9][0-9]{13}$/',
58 // JCB cards beginning with 2131 or 1800 have 15 digits.
59 // JCB cards beginning with 35 have 16 digits.
61 '/^(?:2131|1800|35[0-9]{3})[0-9]{11}$/',
63 // Laser cards begin with either 6304, 6706, 6709 or 6771 and have between 16 and 19 digits.
65 '/^(6304|670[69]|6771)[0-9]{12,15}$/',
67 // Maestro international cards begin with 675900..675999 and have between 12 and 19 digits.
68 // Maestro UK cards begin with either 500000..509999 or 560000..699999 and have between 12 and 19 digits.
70 '/^(6759[0-9]{2})[0-9]{6,13}$/',
71 '/^(50[0-9]{4})[0-9]{6,13}$/',
72 '/^5[6-9][0-9]{10,17}$/',
75 // All MasterCard numbers start with the numbers 51 through 55. All have 16 digits.
76 // October 2016 MasterCard numbers can also start with 222100 through 272099.
77 'MASTERCARD' => array(
78 '/^5[1-5][0-9]{14}$/',
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})$/',
81 // All Visa card numbers start with a 4 and have a length of 13, 16, or 19 digits.
83 '/^4([0-9]{12}|[0-9]{15}|[0-9]{18})$/',
88 * Validates a creditcard belongs to a specified scheme.
91 * @param Constraint $constraint
93 public function validate($value, Constraint $constraint)
95 if (!$constraint instanceof CardScheme) {
96 throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\CardScheme');
99 if (null === $value || '' === $value) {
103 if (!is_numeric($value)) {
104 $this->context->buildViolation($constraint->message)
105 ->setParameter('{{ value }}', $this->formatValue($value))
106 ->setCode(CardScheme::NOT_NUMERIC_ERROR)
112 $schemes = array_flip((array) $constraint->schemes);
113 $schemeRegexes = array_intersect_key($this->schemes, $schemes);
115 foreach ($schemeRegexes as $regexes) {
116 foreach ($regexes as $regex) {
117 if (preg_match($regex, $value)) {
123 $this->context->buildViolation($constraint->message)
124 ->setParameter('{{ value }}', $this->formatValue($value))
125 ->setCode(CardScheme::INVALID_FORMAT_ERROR)