Passed
Pull Request — master (#3)
by S.Hossein
08:26
created

Pregex::IsPersianAlphabet()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: sedhossein
5
 * Date: 11/21/2018
6
 * Time: 10:48 AM
7
 */
8
9
namespace Sedhossein\Pregex;
10
11
class Pregex implements PersianValidator
12
{
13
    private static $persian_number = '\x{06F0}-\x{06F9}';
14
15
    private static $arabic_numbers = '\x{0660}-\x{0669}';
16
17
    private static $persian_alphabets = '\x0600-\x06FF';
0 ignored issues
show
introduced by
The private property $persian_alphabets is not used, and could be removed.
Loading history...
18
19
    private static $banks_names = [
20
        'bmi' => '603799',
21
        'bim' => '627961',
22
        'bki' => '603770',
23
        'bpi' => '502229',
24
        'bsi' => '603769',
25
        'edbi' => '627648',
26
        'sb24' => '621986',
27
        'sbank' => '639607',
28
        'ttbank' => '502908',
29
        'enbank' => '627412',
30
        'mebank' => '639370',
31
        'resalat' => '504172',
32
        'bank-day' => '502938',
33
        'postbank' => '627760',
34
        'sinabank' => '639346',
35
        'banksepah' => '589210',
36
        'ansarbank' => '627381',
37
        'refah-bank' => '589463',
38
        'bankmellat' => '610433',
39
        'shahr-bank' => '502806',
40
        'bank-maskan' => '628023',
41
        'tejaratbank' => '627353',
42
        'parsian-bank' => '622106',
43
        'karafarinbank' => '627488',
44
    ];
45
46
    private static $nameLimit = 40;
47
48
    public function IsPersianNumber(string $number): bool
49
    {
50
        return (bool)preg_match("/(^[" . self::$persian_number . "]+$)/u", $number);
51
    }
52
53
    public function IsArabicNumber(string $number): bool
54
    {
55
        return (bool)preg_match("/(^[" . self::$arabic_numbers . "]+$)/u", $number);
56
    }
57
58
    public function IsPersianOrArabicNumber(string $number): bool
59
    {
60
        return (bool)preg_match("/(^[" .
61
            self::$arabic_numbers .
62
            self::$persian_number .
63
            "]+$)/u", $number);
64
    }
65
66
    public function IsEmail(string $email): bool
67
    {
68
        return (bool)filter_var($email, FILTER_VALIDATE_EMAIL);
69
    }
70
71
    public function IsCellphone(string $number): bool
72
    {
73
        return (bool)preg_match('/^(^((98)|(\+98)|0)?(9){1}[0-9]{9})+$/', $number);
74
    }
75
76
    public function IsIban(string $value): bool
77
    {
78
        if (empty($value)) {
79
            return false;
80
        }
81
82
        $ibanReplaceValues = [];
83
        $value = preg_replace('/[\W_]+/', '', strtoupper($value));
84
        if ((4 > strlen($value) || strlen($value) > 34) || (is_numeric($value [0]) || is_numeric($value [1])) || (!is_numeric($value [2]) || !is_numeric($value [3]))) {
85
            return false;
86
        }
87
88
        $ibanReplaceChars = range('A', 'Z');
89
        foreach (range(10, 35) as $tempvalue) {
90
            $ibanReplaceValues[] = strval($tempvalue);
91
        }
92
93
        $tmpIBAN = substr($value, 4) . substr($value, 0, 4);
94
        $tmpIBAN = str_replace($ibanReplaceChars, $ibanReplaceValues, $tmpIBAN);
95
        $tmpValue = intval(substr($tmpIBAN, 0, 1));
96
        for ($i = 1; $i < strlen($tmpIBAN); $i++) {
97
            $tmpValue *= 10;
98
            $tmpValue += intval(substr($tmpIBAN, $i, 1));
99
            $tmpValue %= 97;
100
        }
101
102
        return $tmpValue != 1 ? false : true;
103
    }
104
105
    /**
106
     * @codeCoverageIgnore
107
     */
108
    public function IsNationalCode(string $value): bool
109
    {
110
        if (
111
            preg_match('/^\d{8,10}$/', $value) == false ||
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing preg_match('/^\d{8,10}$/', $value) of type integer to the boolean false. If you are specifically checking for 0, consider using something more explicit like === 0 instead.
Loading history...
112
            preg_match('/^[0]{10}|[1]{10}|[2]{10}|[3]{10}|[4]{10}|[5]{10}|[6]{10}|[7]{10}|[8]{10}|[9]{10}$/', $value)
113
        ) {
114
            return false;
115
        }
116
117
        $sub = 0;
118
        switch (strlen($value)) {
119
            case 8:
120
                $value = '00' . $value;
121
                break;
122
            case 9:
123
                $value = '0' . $value;
124
                break;
125
        }
126
127
        for ($i = 0; $i <= 8; $i++) {
128
            $sub = $sub + ($value[$i] * (10 - $i));
129
        }
130
131
        $control = ($sub % 11) < 2 ? $sub % 11 : 11 - ($sub % 11);
132
133
        return $value[9] == $control ? true : false;
134
    }
135
136
    public function IsCardNumber(string $value): bool
137
    {
138
        if (!preg_match('/^\d{16}$/', $value) || !in_array(substr($value, 0, 6), static::$banks_names)) {
0 ignored issues
show
Bug introduced by
Since $banks_names is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $banks_names to at least protected.
Loading history...
139
            return false;
140
        }
141
142
        $sum = 0;
143
        for ($position = 1; $position <= 16; $position++) {
144
            $temp = $value[$position - 1];
145
            $temp = $position % 2 === 0 ? $temp : $temp * 2;
146
            $temp = $temp > 9 ? $temp - 9 : $temp;
147
            $sum += $temp;
148
        }
149
150
        return $sum % 10 === 0;
151
    }
152
153
    public function IsPostalCode(string $value): bool
154
    {
155
        return (bool)preg_match("/^(\d{5}-?\d{5})$/", $value);
156
    }
157
158
    public function IsPersianText(string $value): bool
159
    {
160
        return (bool)preg_match("/^[\x{600}-\x{6FF}\x{200c}\x{064b}\x{064d}\x{064c}\x{064e}\x{064f}\x{0650}\x{0651}\x{002E}\s]+$/u", $value);
161
    }
162
163
    public function IsPersianName(string $name): bool
164
    {
165
        return $this->IsPersianAlphabet($name) && strlen($name) < self::$nameLimit;
166
    }
167
168
    public function IsPersianAlphabet(string $chars): bool
169
    {
170
        return (bool)preg_match("/^[\u0600-\u06FF\s]+$/u", $chars);
171
    }
172
173
    public function IsWithoutPersianAlphabet(string $value): bool
174
    {
175
        return !$this->IsPersianAlphabet($value);
176
    }
177
178
    public function IsWithoutNumber(string $value): bool
179
    {
180
        return (bool)preg_match("/[" . self::$persian_number . self::$arabic_numbers . "]$/u", $value);
181
    }
182
}
183