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
|
|
|
public function __construct($brand = null) |
46
|
|
|
{ |
47
|
|
|
if (null !== $brand && !isset($this->brands[$brand])) { |
48
|
|
|
$brands = implode(', ', array_keys($this->brands)); |
49
|
|
|
$message = sprintf('"%s" is not a valid credit card brand (Available: %s).', $brand, $brands); |
50
|
|
|
throw new ComponentException($message); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
$this->brand = $brand; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* {@inheritdoc} |
58
|
|
|
*/ |
59
|
|
|
public function validate($input) |
60
|
|
|
{ |
61
|
|
|
$input = preg_replace('([^0-9])', '', $input); |
62
|
|
|
|
63
|
|
|
if (empty($input)) { |
64
|
|
|
return false; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
if (!$this->verifyMod10($input)) { |
68
|
|
|
return false; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return $this->verifyBrand($input); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Returns whether the input matches the Luhn algorithm or not. |
76
|
|
|
* |
77
|
|
|
* @param string $input |
78
|
|
|
* |
79
|
|
|
* @return bool |
80
|
|
|
*/ |
81
|
|
|
private function verifyMod10($input) |
82
|
|
|
{ |
83
|
|
|
$sum = 0; |
84
|
|
|
$input = strrev($input); |
85
|
|
|
for ($i = 0; $i < mb_strlen($input); ++$i) { |
86
|
|
|
$current = mb_substr($input, $i, 1); |
87
|
|
|
if ($i % 2 == 1) { |
88
|
|
|
$current *= 2; |
89
|
|
|
if ($current > 9) { |
90
|
|
|
$firstDigit = $current % 10; |
91
|
|
|
$secondDigit = ($current - $firstDigit) / 10; |
92
|
|
|
$current = $firstDigit + $secondDigit; |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
$sum += $current; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
return $sum % 10 == 0; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Returns whether the input matches the defined credit card brand or not. |
103
|
|
|
* |
104
|
|
|
* @param string $input |
105
|
|
|
* |
106
|
|
|
* @return bool |
107
|
|
|
*/ |
108
|
|
|
private function verifyBrand($input) |
109
|
|
|
{ |
110
|
|
|
if (null === $this->brand) { |
111
|
|
|
return true; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
$pattern = $this->brands[$this->brand]; |
115
|
|
|
|
116
|
|
|
return preg_match($pattern, $input) > 0; |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|