Completed
Pull Request — master (#98)
by
unknown
02:06
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
     * @link http://datavaluetalk.com/data-quality/remarkable-facts-on-dutch-national-personal-identification-number-burgerservicenummer-bsn/
23
     */
24
    public static function validate($value)
25
    {
26
        if (!is_numeric($value)) {
27
            return false;
28
        }
29
30
        $stringLength = strlen($value);
31
32
        if ($stringLength !== 9 && $stringLength !== 8) {
33
            return false;
34
        }
35
36
        $sum = 0;
37
        $multiplier = $stringLength;
38
        for ($counter = 0; $counter < $stringLength; $counter++, $multiplier--) {
39
            if ($multiplier == 1) {
40
                $multiplier = -1;
41
            }
42
43
            $sum += substr($value, $counter, 1) * $multiplier;
44
        }
45
46
        return $sum % 11 === 0;
47
    }
48
}
49