Ogrn::isValid()   B
last analyzed

Complexity

Conditions 7
Paths 6

Size

Total Lines 30
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 7
eloc 15
c 1
b 0
f 1
nc 6
nop 1
dl 0
loc 30
rs 8.8333
1
<?php
2
3
namespace Kontrolio\Rules\Core;
4
5
use Kontrolio\Rules\AbstractRule;
6
7
class Ogrn 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
        $length = strlen($input);
26
        
27
        if ($length < 13 || $length > 15) {
28
            $this->violations[] = 'length';
29
30
            return false;
31
        }
32
        
33
        if ($length == 13) {
34
            $result = floor(($input / 10) % 11);
35
36
            return ($result == 10 ? $input[12] == 0 : $input[12] == $result);
37
        }
38
39
        if ($length == 15) {
40
            $result = floor(($input / 10) % 13);
41
42
            return ($input[14] == ($result % 10));
43
        }
44
45
        return false;
46
    }
47
}
48