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

Bsn   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 6
c 2
b 0
f 1
lcom 0
cbo 0
dl 0
loc 34
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B validate() 0 24 6
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