CardSchemeValidator.php 5.3 KB

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