Mercurial > hg > isophonics-drupal-site
comparison vendor/symfony/validator/Constraints/CardSchemeValidator.php @ 0:4c8ae668cc8c
Initial import (non-working)
author | Chris Cannam |
---|---|
date | Wed, 29 Nov 2017 16:09:58 +0000 |
parents | |
children | 1fec387a4317 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4c8ae668cc8c |
---|---|
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\Constraint; | |
15 use Symfony\Component\Validator\ConstraintValidator; | |
16 use Symfony\Component\Validator\Exception\UnexpectedTypeException; | |
17 | |
18 /** | |
19 * Validates that a card number belongs to a specified scheme. | |
20 * | |
21 * @author Tim Nagel <t.nagel@infinite.net.au> | |
22 * @author Bernhard Schussek <bschussek@gmail.com> | |
23 * | |
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 | |
27 */ | |
28 class CardSchemeValidator extends ConstraintValidator | |
29 { | |
30 protected $schemes = array( | |
31 // American Express card numbers start with 34 or 37 and have 15 digits. | |
32 'AMEX' => array( | |
33 '/^3[47][0-9]{13}$/', | |
34 ), | |
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( | |
38 '/^62[0-9]{14,17}$/', | |
39 ), | |
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. | |
43 'DINERS' => array( | |
44 '/^3(?:0[0-5]|[68][0-9])[0-9]{11}$/', | |
45 ), | |
46 // Discover card numbers begin with 6011, 622126 through 622925, 644 through 649 or 65. | |
47 // All have 16 digits. | |
48 'DISCOVER' => array( | |
49 '/^6011[0-9]{12}$/', | |
50 '/^64[4-9][0-9]{13}$/', | |
51 '/^65[0-9]{14}$/', | |
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}$/', | |
53 ), | |
54 // InstaPayment cards begin with 637 through 639 and have 16 digits. | |
55 'INSTAPAYMENT' => array( | |
56 '/^63[7-9][0-9]{13}$/', | |
57 ), | |
58 // JCB cards beginning with 2131 or 1800 have 15 digits. | |
59 // JCB cards beginning with 35 have 16 digits. | |
60 'JCB' => array( | |
61 '/^(?:2131|1800|35[0-9]{3})[0-9]{11}$/', | |
62 ), | |
63 // Laser cards begin with either 6304, 6706, 6709 or 6771 and have between 16 and 19 digits. | |
64 'LASER' => array( | |
65 '/^(6304|670[69]|6771)[0-9]{12,15}$/', | |
66 ), | |
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. | |
69 'MAESTRO' => array( | |
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}$/', | |
73 '/^6[0-9]{11,18}$/', | |
74 ), | |
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})$/', | |
80 ), | |
81 // All Visa card numbers start with a 4. New cards have 16 digits. Old cards have 13. | |
82 'VISA' => array( | |
83 '/^4([0-9]{12}|[0-9]{15})$/', | |
84 ), | |
85 ); | |
86 | |
87 /** | |
88 * Validates a creditcard belongs to a specified scheme. | |
89 * | |
90 * @param mixed $value | |
91 * @param Constraint $constraint | |
92 */ | |
93 public function validate($value, Constraint $constraint) | |
94 { | |
95 if (!$constraint instanceof CardScheme) { | |
96 throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\CardScheme'); | |
97 } | |
98 | |
99 if (null === $value || '' === $value) { | |
100 return; | |
101 } | |
102 | |
103 if (!is_numeric($value)) { | |
104 $this->context->buildViolation($constraint->message) | |
105 ->setParameter('{{ value }}', $this->formatValue($value)) | |
106 ->setCode(CardScheme::NOT_NUMERIC_ERROR) | |
107 ->addViolation(); | |
108 | |
109 return; | |
110 } | |
111 | |
112 $schemes = array_flip((array) $constraint->schemes); | |
113 $schemeRegexes = array_intersect_key($this->schemes, $schemes); | |
114 | |
115 foreach ($schemeRegexes as $regexes) { | |
116 foreach ($regexes as $regex) { | |
117 if (preg_match($regex, $value)) { | |
118 return; | |
119 } | |
120 } | |
121 } | |
122 | |
123 $this->context->buildViolation($constraint->message) | |
124 ->setParameter('{{ value }}', $this->formatValue($value)) | |
125 ->setCode(CardScheme::INVALID_FORMAT_ERROR) | |
126 ->addViolation(); | |
127 } | |
128 } |