|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of Respect/Validation. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Alexandre Gomes Gaigalas <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the "LICENSE.md" |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Respect\Validation\Rules; |
|
13
|
|
|
|
|
14
|
|
|
use Respect\Validation\Exceptions\ComponentException; |
|
15
|
|
|
|
|
16
|
|
|
class CreditCard extends AbstractRule |
|
17
|
|
|
{ |
|
18
|
|
|
const AMERICAN_EXPRESS = 'American Express'; |
|
19
|
|
|
const DINERS_CLUB = 'Diners Club'; |
|
20
|
|
|
const DISCOVER = 'Discover'; |
|
21
|
|
|
const JCB = 'JCB'; |
|
22
|
|
|
const MASTERCARD = 'MasterCard'; |
|
23
|
|
|
const VISA = 'Visa'; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var string |
|
27
|
|
|
*/ |
|
28
|
|
|
public $brand; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var array |
|
32
|
|
|
*/ |
|
33
|
|
|
private $brands = [ |
|
34
|
|
|
self::AMERICAN_EXPRESS => '/^3[47]\d{13}$/', |
|
35
|
|
|
self::DINERS_CLUB => '/^3(?:0[0-5]|[68]\d)\d{11}$/', |
|
36
|
|
|
self::DISCOVER => '/^6(?:011|5\d{2})\d{12}$/', |
|
37
|
|
|
self::JCB => '/^(?:2131|1800|35\d{3})\d{11}$/', |
|
38
|
|
|
self::MASTERCARD => '/(5[1-5]|2[2-7])\d{14}$/', |
|
39
|
|
|
self::VISA => '/^4\d{12}(?:\d{3})?$/', |
|
40
|
|
|
]; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @param string $brand Optional credit card brand |
|
44
|
|
|
*/ |
|
45
|
3 |
|
public function __construct($brand = null) |
|
46
|
|
|
{ |
|
47
|
3 |
|
if (null !== $brand && !isset($this->brands[$brand])) { |
|
48
|
1 |
|
$brands = implode(', ', array_keys($this->brands)); |
|
49
|
1 |
|
$message = sprintf('"%s" is not a valid credit card brand (Available: %s).', $brand, $brands); |
|
50
|
1 |
|
throw new ComponentException($message); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
2 |
|
$this->brand = $brand; |
|
54
|
2 |
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* {@inheritdoc} |
|
58
|
|
|
*/ |
|
59
|
28 |
|
public function validate($input) |
|
60
|
|
|
{ |
|
61
|
28 |
|
$input = preg_replace('([^0-9])', '', $input); |
|
62
|
|
|
|
|
63
|
28 |
|
if (empty($input)) { |
|
64
|
3 |
|
return false; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
25 |
|
if (!(new Luhn())->validate($input)) { |
|
68
|
3 |
|
return false; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
22 |
|
return $this->verifyBrand($input); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Returns whether the input matches the defined credit card brand or not. |
|
76
|
|
|
* |
|
77
|
|
|
* @param string $input |
|
78
|
|
|
* |
|
79
|
|
|
* @return bool |
|
80
|
|
|
*/ |
|
81
|
22 |
|
private function verifyBrand($input) |
|
82
|
|
|
{ |
|
83
|
22 |
|
if (null === $this->brand) { |
|
84
|
8 |
|
return true; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
14 |
|
$pattern = $this->brands[$this->brand]; |
|
88
|
|
|
|
|
89
|
14 |
|
return preg_match($pattern, $input) > 0; |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|