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\Context\ExecutionContextInterface;
15 use Symfony\Component\Validator\Constraint;
16 use Symfony\Component\Validator\ConstraintValidator;
17 use Symfony\Component\Validator\Exception\UnexpectedTypeException;
20 * Validates that a card number belongs to a specified scheme.
22 * @author Tim Nagel <t.nagel@infinite.net.au>
23 * @author Bernhard Schussek <bschussek@gmail.com>
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
29 class CardSchemeValidator extends ConstraintValidator
31 protected $schemes = array(
32 // American Express card numbers start with 34 or 37 and have 15 digits.
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(
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.
45 '/^3(?:0[0-5]|[68][0-9])[0-9]{11}$/',
47 // Discover card numbers begin with 6011, 622126 through 622925, 644 through 649 or 65.
48 // All have 16 digits.
51 '/^64[4-9][0-9]{13}$/',
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}$/',
55 // InstaPayment cards begin with 637 through 639 and have 16 digits.
56 'INSTAPAYMENT' => array(
57 '/^63[7-9][0-9]{13}$/',
59 // JCB cards beginning with 2131 or 1800 have 15 digits.
60 // JCB cards beginning with 35 have 16 digits.
62 '/^(?:2131|1800|35[0-9]{3})[0-9]{11}$/',
64 // Laser cards begin with either 6304, 6706, 6709 or 6771 and have between 16 and 19 digits.
66 '/^(6304|670[69]|6771)[0-9]{12,15}$/',
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.
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}$/',
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})$/',
82 // All Visa card numbers start with a 4. New cards have 16 digits. Old cards have 13.
84 '/^4([0-9]{12}|[0-9]{15})$/',
89 * Validates a creditcard belongs to a specified scheme.
92 * @param Constraint $constraint
94 public function validate($value, Constraint $constraint)
96 if (!$constraint instanceof CardScheme) {
97 throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\CardScheme');
100 if (null === $value || '' === $value) {
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)
111 $this->buildViolation($constraint->message)
112 ->setParameter('{{ value }}', $this->formatValue($value))
113 ->setCode(CardScheme::NOT_NUMERIC_ERROR)
120 $schemes = array_flip((array) $constraint->schemes);
121 $schemeRegexes = array_intersect_key($this->schemes, $schemes);
123 foreach ($schemeRegexes as $regexes) {
124 foreach ($regexes as $regex) {
125 if (preg_match($regex, $value)) {
131 if ($this->context instanceof ExecutionContextInterface) {
132 $this->context->buildViolation($constraint->message)
133 ->setParameter('{{ value }}', $this->formatValue($value))
134 ->setCode(CardScheme::INVALID_FORMAT_ERROR)
137 $this->buildViolation($constraint->message)
138 ->setParameter('{{ value }}', $this->formatValue($value))
139 ->setCode(CardScheme::INVALID_FORMAT_ERROR)