Completed
Push — master ( c777ad...d0e678 )
by WEBEWEB
01:18
created

LuhnAlgorithmHelper   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 0
dl 0
loc 33
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A check() 0 24 4
1
<?php
2
3
/*
4
 * This file is part of the core-library package.
5
 *
6
 * (c) 2018 WEBEWEB
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace WBW\Library\Core\Utility;
13
14
/**
15
 * Luhn algorithm helper.
16
 *
17
 * @author webeweb <https://github.com/webeweb/>
18
 * @package WBW\Library\Core\Utility
19
 */
20
class LuhnAlgorithmHelper {
21
22
    /**
23
     * Check.
24
     *
25
     * @param string $str The string.
26
     * @return bool Returns true in case of success, false otherwise.
27
     */
28
    public static function check($str) {
29
30
        $sum    = 0;
31
        $length = strlen($str);
32
        $parity = $length % 2;
33
34
        $sum += substr($str, $length - 1);
35
36
        for ($i = $length - 2; 0 <= $i; --$i) {
37
38
            $digit = intval(substr($str, $i, 1));
39
40
            if ($parity === $i % 2) {
41
                $digit *= 2;
42
            }
43
            if (9 < $digit) {
44
                $digit -= 9;
45
            }
46
47
            $sum += $digit;
48
        }
49
50
        return 0 === $sum % 10;
51
    }
52
}
53