Passed
Pull Request — 1.x (#77)
by
unknown
11:51
created

ValidIranCompanyId::passes()   B

Complexity

Conditions 7
Paths 11

Size

Total Lines 41
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 30
c 1
b 0
f 0
nc 11
nop 2
dl 0
loc 41
rs 8.5066
1
<?php
2
3
namespace Milwad\LaravelValidate\Rules;
4
5
use Illuminate\Contracts\Validation\Rule;
6
7
class ValidIranCompanyId implements Rule
8
{
9
    /**
10
     * Check company id is valid.
11
     *
12
     * @param  string  $attribute
13
     * @param  mixed  $value
14
     * @return bool
15
     */
16
    public function passes($attribute, $value)
17
    {
18
        if (empty($value))
19
            return false;
20
21
        if(strlen($value) != 11)
22
            return false;
23
24
        $invalidIds = [
25
            00000000000,
26
            11111111111,
27
            22222222222,
28
            33333333333,
29
            44444444444,
30
            55555555555,
31
            66666666666,
32
            77777777777,
33
            88888888888,
34
            99999999999
35
        ];
36
37
        if (in_array($value, $invalidIds)) 
38
            return false;
39
        
40
        $multiplier = [29, 27, 23, 19, 17, 29, 27, 23, 19, 17];
41
        $checkNumber = substr($value, 10, 1);
42
        $decimalNumber = substr($value, 9, 1);
43
        $multiplication = $decimalNumber + 2;
44
        $sum = 0;
45
46
        for ($i = 0; $i < 10; $i++)
47
            $sum += (substr($value, $i, 1) + $multiplication) * $multiplier[$i];
48
49
        $remain = $sum % 11;
50
        if($remain == 10)
51
            $remain = 0;
52
53
        if ($remain == $checkNumber)
54
            return true;
55
        
56
        return false;
57
    }
58
59
    /**
60
     * Get the validation error message.
61
     *
62
     * @return string
63
     */
64
    public function message()
65
    {
66
        return __('validate.company-id');
67
    }
68
}
69