ShebaNumber   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 11
eloc 21
c 2
b 0
f 1
dl 0
loc 43
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B rule() 0 29 11
1
<?php
2
3
namespace Iamfarhad\Validation\Rules;
4
5
use Iamfarhad\Validation\Contracts\AbstractValidationRule;
6
7
class ShebaNumber extends AbstractValidationRule
8
{
9
    /**
10
     * @var string
11
     */
12
    public $validationRule = 'sheba_number';
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
        $ibanReplaceValues = [];
24
25
        if (empty($value)) {
26
            return false;
27
        }
28
29
        $value = preg_replace('/[\W_]+/', '', strtoupper($value));
30
        if ((4 > strlen($value) || strlen($value) > 34) || (is_numeric($value[0]) || is_numeric($value[1])) || (! is_numeric($value[2]) || ! is_numeric($value[3]))) {
31
            return false;
32
        }
33
        $ibanReplaceChars = range('A', 'Z');
34
        foreach (range(10, 35) as $tempvalue) {
35
            $ibanReplaceValues[] = strval($tempvalue);
36
        }
37
        $tmpIBAN = substr($value, 4).substr($value, 0, 4);
38
        $tmpIBAN = str_replace($ibanReplaceChars, $ibanReplaceValues, $tmpIBAN);
39
        $tmpValue = intval(substr($tmpIBAN, 0, 1));
40
        for ($i = 1; $i < strlen($tmpIBAN); $i++) {
41
            $tmpValue *= 10;
42
            $tmpValue += intval(substr($tmpIBAN, $i, 1));
43
            $tmpValue %= 97;
44
        }
45
        if ($tmpValue != 1) {
46
            return false;
47
        }
48
49
        return true;
50
    }
51
}
52