CardScheme   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 31
c 2
b 0
f 0
dl 0
loc 85
rs 10
wmc 7

1 Method

Rating   Name   Duplication   Size   Complexity  
B isValid() 0 23 7
1
<?php
2
3
namespace Kontrolio\Rules\Core;
4
5
use Kontrolio\Rules\AbstractRule;
6
7
/**
8
 * Card scheme validation rule.
9
 *
10
 * @package Kontrolio\Rules\Core
11
 */
12
class CardScheme extends AbstractRule
13
{
14
    /**
15
     * Card schemes.
16
     *
17
     * @var array
18
     */
19
    protected static $schemes = [
20
        // American Express card numbers start with 34 or 37 and have 15 digits.
21
        'AMEX' => ['/^3[47][0-9]{13}$/'],
22
23
        // China UnionPay cards start with 62 and have between 16 and 19 digits.
24
        // Please note that these cards do not follow Luhn Algorithm as a checksum.
25
        'CHINA_UNIONPAY' => ['/^62[0-9]{14,17}$/'],
26
27
        // Diners Club card numbers begin with 300 through 305, 36 or 38. All have 14 digits.
28
        // There are Diners Club cards that begin with 5 and have 16 digits.
29
        // These are a joint venture between Diners Club and MasterCard, and should be processed like a MasterCard.
30
        'DINERS' => ['/^3(?:0[0-5]|[68][0-9])[0-9]{11}$/'],
31
32
        // Discover card numbers begin with 6011, 622126 through 622925, 644 through 649 or 65.
33
        // All have 16 digits.
34
        'DISCOVER' => [
35
            '/^6011[0-9]{12}$/',
36
            '/^64[4-9][0-9]{13}$/',
37
            '/^65[0-9]{14}$/',
38
            '/^622(12[6-9]|1[3-9][0-9]|[2-8][0-9][0-9]|91[0-9]|92[0-5])[0-9]{10}$/',
39
        ],
40
41
        // InstaPayment cards begin with 637 through 639 and have 16 digits.
42
        'INSTAPAYMENT' => ['/^63[7-9][0-9]{13}$/'],
43
44
        // JCB cards beginning with 2131 or 1800 have 15 digits.
45
        // JCB cards beginning with 35 have 16 digits.
46
        'JCB' => ['/^(?:2131|1800|35[0-9]{3})[0-9]{11}$/'],
47
48
        // Laser cards begin with either 6304, 6706, 6709 or 6771 and have between 16 and 19 digits.
49
        'LASER' => ['/^(6304|670[69]|6771)[0-9]{12,15}$/'],
50
51
        // Maestro international cards begin with 675900..675999 and have between 12 and 19 digits.
52
        // Maestro UK cards begin with either 500000..509999 or 560000..699999 and have between 12 and 19 digits.
53
        'MAESTRO' => [
54
            '/^(6759[0-9]{2})[0-9]{6,13}$/',
55
            '/^(50[0-9]{4})[0-9]{6,13}$/',
56
            '/^5[6-9][0-9]{10,17}$/',
57
            '/^6[0-9]{11,18}$/',
58
        ],
59
60
        // All MasterCard numbers start with the numbers 51 through 55. All have 16 digits.
61
        'MASTERCARD' => ['/^5[1-5][0-9]{14}$/'],
62
63
        // All Visa card numbers start with a 4. New cards have 16 digits. Old cards have 13.
64
        'VISA' => ['/^4([0-9]{12}|[0-9]{15})$/'],
65
    ];
66
67
    /**
68
     * Validates input.
69
     *
70
     * @param mixed $input
71
     *
72
     * @return bool
73
     */
74
    public function isValid($input = null)
75
    {
76
        if ($input === null || $input === '') {
77
            return false;
78
        }
79
80
        if (!is_numeric($input)) {
81
            $this->violations[] = 'numeric';
82
83
            return false;
84
        }
85
86
        foreach (static::$schemes as $scheme => $regexes) {
87
            foreach ($regexes as $regex) {
88
                if (preg_match($regex, $input)) {
89
                    return true;
90
                }
91
            }
92
        }
93
94
        $this->violations[] = 'scheme';
95
96
        return false;
97
    }
98
}
99