Total Complexity | 8 |
Total Lines | 60 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
1 | <?php |
||
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() |
||
67 | } |
||
68 | } |
||
69 |