Snils::isValid()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 28
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 4
eloc 15
c 1
b 0
f 1
nc 4
nop 1
dl 0
loc 28
rs 9.7666
1
<?php
2
3
namespace Kontrolio\Rules\Core;
4
5
use Kontrolio\Rules\AbstractRule;
6
7
class Snils extends AbstractRule
8
{
9
    /**
10
     * Validates input.
11
     *
12
     * @param mixed $input
13
     *
14
     * @return bool
15
     */
16
    public function isValid($input = null)
17
    {
18
        if (!is_numeric($input)) {
19
            $this->violations[] = 'numeric';
20
21
            return false;
22
        }
23
24
        $input = (string) $input;
25
26
        if (strlen($input) != 11) {
27
            $this->violations[] = 'length';
28
29
            return false;
30
        }
31
32
        $factor = range(9, 1);
33
        $index = 0;
34
        $checksum = 0;
35
36
        while ($index <= 8) {
37
            $checksum = $checksum + intval($input[$index]) + $factor[$index];
38
            $index++;
39
        }
40
41
        $value = $checksum % 101;
42
43
        return substr($input, 9, 2) == $value;
44
    }
45
}
46