MelliCode   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 8
eloc 18
c 2
b 0
f 1
dl 0
loc 41
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B rule() 0 27 8
1
<?php
2
3
namespace Iamfarhad\Validation\Rules;
4
5
use Iamfarhad\Validation\Contracts\AbstractValidationRule;
6
7
class MelliCode extends AbstractValidationRule
8
{
9
    /**
10
     * @var string
11
     */
12
    public $validationRule = 'melli_code';
13
14
    /**
15
     * @param $attribute
16
     * @param $value
17
     * @param $parameters
18
     * @param $validator
19
     * @return bool
20
     */
21
    public function rule($attribute, $value, $parameters, $validator): bool
22
    {
23
        if (! preg_match('/^\d{8,10}$/', $value) || preg_match('/^[0]{10}|[1]{10}|[2]{10}|[3]{10}|[4]{10}|[5]{10}|[6]{10}|[7]{10}|[8]{10}|[9]{10}$/', $value)) {
24
            return false;
25
        }
26
        $sub = 0;
27
        if (strlen($value) == 8) {
28
            $value = '00'.$value;
29
        } elseif (strlen($value) == 9) {
30
            $value = '0'.$value;
31
        }
32
33
        for ($i = 0; $i <= 8; $i++) {
34
            $sub = $sub + ($value[$i] * (10 - $i));
35
        }
36
37
        if (($sub % 11) < 2) {
38
            $control = ($sub % 11);
39
        } else {
40
            $control = 11 - ($sub % 11);
41
        }
42
43
        if ($value[9] == $control) {
44
            return true;
45
        }
46
47
        return false;
48
    }
49
}
50