Completed
Push — master ( 0de93f...7d590b )
by Ronan
02:51 queued 53s
created

Sedol::validate()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 21
rs 9.0534
cc 4
eloc 12
nc 4
nop 1
1
<?php
2
3
namespace IsoCodes;
4
5
/**
6
 * @link https://en.wikipedia.org/wiki/SEDOL
7
 *
8
 * @author Sullivan Senechal <[email protected]>
9
 */
10
class Sedol implements IsoCodeInterface
11
{
12
    /**
13
     * {@inheritdoc}
14
     */
15
    public static function validate($value)
16
    {
17
        if (strlen($value) !== 7) {
18
            return false;
19
        }
20
21
        $char6 = substr($value, 0, 6);
22
23
        if (!preg_match('/^[0-9BCDFGHJKLMNPQRSTVWXYZ]{6}$/', $char6)) {
24
            return false;
25
        }
26
27
        $weight = [1, 3, 1, 7, 3, 9, 1];
28
        $sum = 0;
29
        for ($i = 0; $i < 6; ++$i) {
30
            $sum += $weight[$i] * intval($char6[$i], 36);
31
        }
32
        $check = (10 - $sum % 10) % 10;
33
34
        return $value === $char6 . $check;
35
    }
36
}
37