1 | <?php |
||
19 | class CreditCard extends Rule |
||
20 | { |
||
21 | /** |
||
22 | * A constant that will be used when the value is not a valid credit card number. |
||
23 | */ |
||
24 | const INVALID_FORMAT = 'CreditCard::INVALID_FORMAT'; |
||
25 | const INVALID_CHECKSUM = 'CreditCard::INVALID_CHECKSUM'; |
||
26 | |||
27 | /** |
||
28 | * Regular expressions to validate the different credit card brands |
||
29 | * |
||
30 | * @var array |
||
31 | */ |
||
32 | protected $validationRegExps = [ |
||
33 | '/^4[0-9]{12}(?:[0-9]{3})?$/', // Visa |
||
34 | '/^5[1-5][0-9]{14}$/', // Mastercard |
||
35 | '/^3[47][0-9]{13}$/', // American Express |
||
36 | '/^3(?:0[0-5]|[68][0-9])[0-9]{11}$/', // Diners Club |
||
37 | '/^6(?:011|5[0-9]{2})[0-9]{12}$/', // Discover |
||
38 | '/^(?:2131|1800|35\d{3})\d{11}$/', // JCB |
||
39 | ]; |
||
40 | |||
41 | /** |
||
42 | * The message templates which can be returned by this validator. |
||
43 | * |
||
44 | * @var array |
||
45 | */ |
||
46 | protected $messageTemplates = [ |
||
47 | self::INVALID_FORMAT => '{{ name }} must have a valid credit card number format', |
||
48 | self::INVALID_CHECKSUM => '{{ name }} must be a valid credit card number', |
||
49 | ]; |
||
50 | |||
51 | /** |
||
52 | * Validates if the value is a valid credit card number. |
||
53 | * |
||
54 | * @param mixed $value |
||
55 | * @return bool |
||
56 | */ |
||
57 | 15 | public function validate($value) |
|
67 | |||
68 | /** |
||
69 | * @param $value |
||
70 | * |
||
71 | * @return bool |
||
72 | */ |
||
73 | 12 | private function validateChecksum($value) |
|
79 | |||
80 | /** |
||
81 | * @param $value |
||
82 | * |
||
83 | * @return bool |
||
84 | */ |
||
85 | 15 | private function validateFormat($value) |
|
95 | } |
||
96 |