Completed
Push — master ( a7e2c9...8b5a78 )
by Ronan
01:11
created

Cusip   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

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

1 Method

Rating   Name   Duplication   Size   Complexity  
B validate() 0 30 9
1
<?php
2
3
namespace IsoCodes;
4
5
/**
6
 * Class Cusip.
7
 * National Securities Identification Number for products issued from both the United States and Canada.
8
 * The acronym, pronounced as "kyoo-sip," derives from Committee on Uniform Security Identification Procedures.
9
 *
10
 * @see https://www.investopedia.com/ask/answers/what-is-a-cusip-number/
11
 * @see https://en.wikipedia.org/wiki/CUSIP
12
 */
13
class Cusip implements IsoCodeInterface
14
{
15
    public static function validate($cusip)
16
    {
17
        if (9 !== strlen($cusip)) {
18
            return false;
19
        }
20
        $sum = 0;
21
        for ($i = 0; $i <= 7; ++$i) {
22
            $c = $cusip[$i];
23
            if (ctype_digit($c)) {
24
                $v = intval($c);
25
            } elseif (ctype_alpha($c)) {
26
                $position = ord(strtoupper($c)) - ord('A') + 1;
27
                $v = $position + 9;
28
            } elseif ('*' == $c) {
29
                $v = 36;
30
            } elseif ('@' == $c) {
31
                $v = 37;
32
            } elseif ('#' == $c) {
33
                $v = 38;
34
            } else {
35
                return false;
36
            }
37
            if (1 == $i % 2) {
38
                $v *= 2;
39
            }
40
            $sum += floor($v / 10) + ($v % 10);
41
        }
42
43
        return ord($cusip[8]) - 48 == (10 - ($sum % 10)) % 10;
44
    }
45
}
46