Conditions | 7 |
Paths | 12 |
Total Lines | 27 |
Code Lines | 18 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
1 | <?php |
||
20 | public static function check($luhn, $length, $unDecorate = true, $hyphens = []) |
||
21 | { |
||
22 | $luhn = $unDecorate ? self::unDecorate($luhn, $hyphens) : $luhn; |
||
23 | if (strlen($luhn) != $length) { |
||
24 | return false; |
||
25 | } |
||
26 | $expr = sprintf('/\\d{%d}/i', $length); |
||
27 | if (!preg_match($expr, $luhn)) { |
||
28 | return false; |
||
29 | } |
||
30 | if (0 === (int) $luhn) { |
||
31 | return false; |
||
32 | } |
||
33 | $check = 0; |
||
34 | |||
35 | for ($i = 0; $i < $length; $i += 2) { |
||
36 | if ($length % 2 == 0) { |
||
37 | $check += 3 * (int) substr($luhn, $i, 1); |
||
38 | $check += (int) substr($luhn, $i + 1, 1); |
||
39 | } else { |
||
40 | $check += (int) substr($luhn, $i, 1); |
||
41 | $check += 3 * (int) substr($luhn, $i + 1, 1); |
||
42 | } |
||
43 | } |
||
44 | |||
45 | return $check % 10 == 0; |
||
46 | } |
||
47 | |||
65 |