CreditCard   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 1
dl 0
loc 77
ccs 15
cts 15
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A validate() 0 10 3
A validateChecksum() 0 6 1
A validateFormat() 0 10 3
1
<?php
2
/**
3
 * Particle.
4
 *
5
 * @link      http://github.com/particle-php for the canonical source repository
6
 * @copyright Copyright (c) 2005-2016 Particle (http://particle-php.com)
7
 * @license   https://github.com/particle-php/validator/blob/master/LICENSE New BSD License
8
 */
9
namespace Particle\Validator\Rule;
10
11
use byrokrat\checkdigit\Luhn;
12
use Particle\Validator\Rule;
13
14
/**
15
 * This rule is for validating if a value is a valid credit card number.
16
 *
17
 * @package Particle\Validator\Rule
18
 */
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)
58
    {
59 15
        if (!$this->validateFormat($value)) {
60 3
            return $this->error(self::INVALID_FORMAT);
61 12
        } elseif (!$this->validateChecksum($value)) {
62 4
            return $this->error(self::INVALID_CHECKSUM);
63
        }
64
65 8
        return true;
66
    }
67
68
    /**
69
     * @param $value
70
     *
71
     * @return bool
72
     */
73 12
    private function validateChecksum($value)
74
    {
75 12
        $luhn = new Luhn();
76
77 12
        return $luhn->isValid($value);
78
    }
79
80
    /**
81
     * @param $value
82
     *
83
     * @return bool
84
     */
85 15
    private function validateFormat($value)
86
    {
87 15
        foreach ($this->validationRegExps as $validationRegExp) {
88 15
            if (preg_match($validationRegExp, $value) === 1) {
89 12
                return true;
90
            }
91 12
        }
92
93 3
        return false;
94
    }
95
}
96