Okpo::isValid()   B
last analyzed

Complexity

Conditions 6
Paths 10

Size

Total Lines 41
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 6
eloc 23
c 1
b 0
f 1
nc 10
nop 1
dl 0
loc 41
rs 8.9297
1
<?php
2
3
namespace Kontrolio\Rules\Core;
4
5
use Kontrolio\Rules\AbstractRule;
6
7
class Okpo 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) != 8) {
27
            $this->violations[] = 'length';
28
29
            return false;
30
        }
31
32
        $factor1 = range(1, 7);
33
        $factor2 = range(3, 9);
34
35
        $checksum1 = 0;
36
        $index = 0;
37
38
        while ($index <= 6) {
39
            $checksum1 = $checksum1 + intval($input[$index]) * $factor1[$index];
40
            $index++;
41
        }
42
43
        $value1 = $checksum1 % 11;
44
45
        $index = 0;
46
        $checksum2 = 0;
47
48
        while ($index <= 6) {
49
            $checksum2 = $checksum2 + intval($input[$index]) * $factor2[$index];
50
            $index++;
51
        }
52
53
        $value2 = $checksum2 % 11;
54
        $check = ($value1 > 9 ? $value2 : $value1);
55
56
        return $input[7] == $check;
57
    }
58
}
59