Completed
Pull Request — master (#98)
by
unknown
02:05
created

Bsn::validate()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 24
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 24
rs 8.5125
cc 6
eloc 13
nc 5
nop 1
1
<?php
2
3
namespace IsoCodes;
4
5
/**
6
 * Class Bsn: In the Netherlands, the citizen service number (BSN) is a unique personal number
7
 * allocated to everyone registered in the Municipal Personal Records Database.
8
 *
9
 * @link https://www.government.nl/topics/identification-documents/contents/the-citizen-service-number
10
 *
11
 * @author  Albert Bakker <[email protected]>
12
 */
13
final class Bsn implements IsoCodeInterface
14
{
15
    /**
16
     * BSN validator.
17
     *
18
     * @param string $value
19
     *
20
     * @return bool
21
     */
22
    public static function validate($value)
23
    {
24
        if (!is_numeric($value)) {
25
            return false;
26
        }
27
28
        $stringLength = strlen($value);
29
30
        if ($stringLength !== 9 && $stringLength !== 8) {
31
            return false;
32
        }
33
34
        $sum = 0;
35
        $multiplier = $stringLength;
36
        for ($counter = 0; $counter < $stringLength; $counter++, $multiplier--) {
37
            if ($multiplier == 1) {
38
                $multiplier = -1;
39
            }
40
41
            $sum += substr($value, $counter, 1) * $multiplier;
42
        }
43
44
        return $sum % 11 === 0;
45
    }
46
}
47