|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* |
|
4
|
|
|
* This file is part of Aura for PHP. |
|
5
|
|
|
* |
|
6
|
|
|
* @license http://opensource.org/licenses/bsd-license.php BSD |
|
7
|
|
|
* |
|
8
|
|
|
*/ |
|
9
|
|
|
namespace Aura\Filter\Rule\Validate; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* |
|
13
|
|
|
* Validates the value as a credit card number. |
|
14
|
|
|
* |
|
15
|
|
|
* @package Aura.Filter |
|
16
|
|
|
* |
|
17
|
|
|
*/ |
|
18
|
|
|
class CreditCard |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* |
|
22
|
|
|
* Validates the value as a credit card number. |
|
23
|
|
|
* |
|
24
|
|
|
* @param object $subject The subject to be filtered. |
|
25
|
|
|
* |
|
26
|
|
|
* @param string $field The subject field name. |
|
27
|
|
|
* |
|
28
|
|
|
* @return bool True if valid, false if not. |
|
29
|
|
|
* |
|
30
|
|
|
*/ |
|
31
|
11 |
|
public function __invoke($subject, $field) |
|
32
|
|
|
{ |
|
33
|
|
|
// get the value; remove spaces, dashes, and dots |
|
34
|
11 |
|
$value = str_replace(array(' ', '-', '.'), '', (string) $subject->$field); |
|
35
|
|
|
|
|
36
|
|
|
// is it composed only of digits? |
|
37
|
11 |
|
if (! ctype_digit($value)) { |
|
38
|
4 |
|
return false; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
// luhn mod-10 algorithm: https://gist.github.com/1287893 |
|
42
|
|
|
$sumTable = array( |
|
43
|
7 |
|
array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9), |
|
44
|
|
|
array(0, 2, 4, 6, 8, 1, 3, 5, 7, 9), |
|
45
|
|
|
); |
|
46
|
|
|
|
|
47
|
7 |
|
$sum = 0; |
|
48
|
7 |
|
$flip = 0; |
|
49
|
|
|
|
|
50
|
7 |
|
for ($i = strlen($value) - 1; $i >= 0; $i--) { |
|
51
|
7 |
|
$sum += $sumTable[$flip++ & 0x1][$value[$i]]; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
7 |
|
return $sum % 10 === 0; |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
|