Completed
Push — master ( 70fcc2...406cf9 )
by Ronan
01:53
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
 * 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
 * @link https://nl.wikipedia.org/wiki/Elfproef (dutch)
11
 * @link https://nl.wikipedia.org/wiki/Burgerservicenummer (dutch)
12
 * @link http://datavaluetalk.com/data-quality/remarkable-facts-on-dutch-national-personal-identification-number-burgerservicenummer-bsn/
13
 *
14
 * @author  Albert Bakker <[email protected]>
15
 */
16
final class Bsn implements IsoCodeInterface
17
{
18
    /**
19
     * @param string $value
20
     *
21
     * @return bool
22
     */
23
    public static function validate($value)
24
    {
25
        if (!is_numeric($value)) {
26
            return false;
27
        }
28
29
        $stringLength = strlen($value);
30
31
        if ($stringLength !== 9 && $stringLength !== 8) {
32
            return false;
33
        }
34
35
        $sum = 0;
36
        $multiplier = $stringLength;
37
        for ($counter = 0; $counter < $stringLength; $counter++, $multiplier--) {
38
            if ($multiplier == 1) {
39
                $multiplier = -1;
40
            }
41
42
            $sum += substr($value, $counter, 1) * $multiplier;
43
        }
44
45
        return $sum % 11 === 0;
46
    }
47
}
48