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_numbers = '\x{06F0}-\x{06F9}'; |
||
14 | |||
15 | private static $arabic_numbers = '\x{0660}-\x{0669}'; |
||
16 | |||
17 | private static $persian_alphabets = '\x{0621}-\x{06CC}'; |
||
18 | |||
19 | private static $persian_text = '\x{0600}-\x{06FF}'; |
||
20 | |||
21 | private static $arabic_common_chars = "\x{0629}\x{0643}\x{0649}-\x{064B}\x{064D}\x{06D5}"; |
||
22 | |||
23 | private static $spaces = '\x{0020}\x{2000}-\x{200F}\x{2028}-\x{202F}'; |
||
24 | |||
25 | private static $banks_names = [ |
||
26 | 'bmi' => '603799', |
||
27 | 'bim' => '627961', |
||
28 | 'bki' => '603770', |
||
29 | 'bpi' => '502229', |
||
30 | 'bsi' => '603769', |
||
31 | 'edbi' => '627648', |
||
32 | 'sb24' => '621986', |
||
33 | 'sbank' => '639607', |
||
34 | 'ttbank' => '502908', |
||
35 | 'enbank' => '627412', |
||
36 | 'mebank' => '639370', |
||
37 | 'resalat' => '504172', |
||
38 | 'bank-day' => '502938', |
||
39 | 'postbank' => '627760', |
||
40 | 'sinabank' => '639346', |
||
41 | 'banksepah' => '589210', |
||
42 | 'ansarbank' => '627381', |
||
43 | 'refah-bank' => '589463', |
||
44 | 'bankmellat' => '610433', |
||
45 | 'shahr-bank' => '502806', |
||
46 | 'bank-maskan' => '628023', |
||
47 | 'tejaratbank' => '627353', |
||
48 | 'parsian-bank' => '622106', |
||
49 | 'karafarinbank' => '627488', |
||
50 | ]; |
||
51 | |||
52 | public static $nameMaxLimit = 60; |
||
53 | |||
54 | public static $nameMinLimit = 3; |
||
55 | |||
56 | public function IsPersianNumber(string $number): bool |
||
57 | { |
||
58 | return (bool)preg_match("/(^[" . self::$persian_numbers . "]+$)/u", $number); |
||
59 | } |
||
60 | |||
61 | public function IsArabicNumber(string $number): bool |
||
62 | { |
||
63 | return (bool)preg_match("/(^[" . self::$arabic_numbers . "]+$)/u", $number); |
||
64 | } |
||
65 | |||
66 | public function IsPersianOrArabicNumber(string $number): bool |
||
67 | { |
||
68 | return (bool)preg_match("/(^[" . |
||
69 | self::$arabic_numbers . |
||
70 | self::$persian_numbers . |
||
71 | "]+$)/u", $number); |
||
72 | } |
||
73 | |||
74 | public function IsEmail(string $email): bool |
||
75 | { |
||
76 | return (bool)filter_var($email, FILTER_VALIDATE_EMAIL); |
||
77 | } |
||
78 | |||
79 | public function IsCellphone(string $number): bool |
||
80 | { |
||
81 | return (bool)preg_match('/^(^((98)|(\+98)|0)?(9){1}[0-9]{9})+$/', $number); |
||
82 | } |
||
83 | |||
84 | public function IsIban(string $value): bool |
||
85 | { |
||
86 | if (empty($value)) { |
||
87 | return false; |
||
88 | } |
||
89 | |||
90 | $ibanReplaceValues = []; |
||
91 | $value = preg_replace('/[\W_]+/', '', strtoupper($value)); |
||
92 | if ((4 > strlen($value) || strlen($value) > 34) || (is_numeric($value [0]) || is_numeric($value [1])) || (!is_numeric($value [2]) || !is_numeric($value [3]))) { |
||
93 | return false; |
||
94 | } |
||
95 | |||
96 | $ibanReplaceChars = range('A', 'Z'); |
||
97 | foreach (range(10, 35) as $tempvalue) { |
||
98 | $ibanReplaceValues[] = strval($tempvalue); |
||
99 | } |
||
100 | |||
101 | $tmpIBAN = substr($value, 4) . substr($value, 0, 4); |
||
102 | $tmpIBAN = str_replace($ibanReplaceChars, $ibanReplaceValues, $tmpIBAN); |
||
103 | $tmpValue = intval(substr($tmpIBAN, 0, 1)); |
||
104 | for ($i = 1; $i < strlen($tmpIBAN); $i++) { |
||
105 | $tmpValue *= 10; |
||
106 | $tmpValue += intval(substr($tmpIBAN, $i, 1)); |
||
107 | $tmpValue %= 97; |
||
108 | } |
||
109 | |||
110 | return $tmpValue != 1 ? false : true; |
||
111 | } |
||
112 | |||
113 | /** |
||
114 | * @codeCoverageIgnore |
||
115 | */ |
||
116 | public function IsNationalCode(string $value): bool |
||
117 | { |
||
118 | if ( |
||
119 | preg_match('/^\d{8,10}$/', $value) == false || |
||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
![]() |
|||
120 | preg_match('/^[0]{10}|[1]{10}|[2]{10}|[3]{10}|[4]{10}|[5]{10}|[6]{10}|[7]{10}|[8]{10}|[9]{10}$/', $value) |
||
121 | ) { |
||
122 | return false; |
||
123 | } |
||
124 | |||
125 | $sub = 0; |
||
126 | switch (strlen($value)) { |
||
127 | case 8: |
||
128 | $value = '00' . $value; |
||
129 | break; |
||
130 | case 9: |
||
131 | $value = '0' . $value; |
||
132 | break; |
||
133 | } |
||
134 | |||
135 | for ($i = 0; $i <= 8; $i++) { |
||
136 | $sub = $sub + ($value[$i] * (10 - $i)); |
||
137 | } |
||
138 | |||
139 | $control = ($sub % 11) < 2 ? $sub % 11 : 11 - ($sub % 11); |
||
140 | |||
141 | return $value[9] == $control ? true : false; |
||
142 | } |
||
143 | |||
144 | public function IsCardNumber(string $value): bool |
||
145 | { |
||
146 | if (!preg_match('/^\d{16}$/', $value) || !in_array(substr($value, 0, 6), static::$banks_names)) { |
||
0 ignored issues
–
show
|
|||
147 | return false; |
||
148 | } |
||
149 | |||
150 | $sum = 0; |
||
151 | for ($position = 1; $position <= 16; $position++) { |
||
152 | $temp = $value[$position - 1]; |
||
153 | $temp = $position % 2 === 0 ? $temp : $temp * 2; |
||
154 | $temp = $temp > 9 ? $temp - 9 : $temp; |
||
155 | $sum += $temp; |
||
156 | } |
||
157 | |||
158 | return $sum % 10 === 0; |
||
159 | } |
||
160 | |||
161 | public function IsPostalCode(string $value): bool |
||
162 | { |
||
163 | return (bool)preg_match("/^(\d{5}-?\d{5})$/", $value); |
||
164 | } |
||
165 | |||
166 | public function IsPersianText(string $value): bool |
||
167 | { |
||
168 | return (bool)preg_match("/^[" . |
||
169 | self::$persian_text |
||
170 | . "\x{200c}\x{064b}\x{064d}\x{064c}\x{064e}\x{064f}\x{0650}\x{0651}\x{002E}" . |
||
171 | "\s]+$/u", $value); |
||
172 | } |
||
173 | |||
174 | public function IsPersianAlphabet(string $chars): bool |
||
175 | { |
||
176 | return (bool)preg_match("/(^[" . |
||
177 | self::$arabic_common_chars . |
||
178 | self::$persian_alphabets . |
||
179 | self::$spaces . |
||
180 | "]+$)/u", $chars); |
||
181 | } |
||
182 | |||
183 | public function IsPersianName(string $name): bool |
||
184 | { |
||
185 | $nameLen = strlen($name); |
||
186 | |||
187 | return $this->IsPersianAlphabet($name) && |
||
188 | $nameLen <= self::$nameMaxLimit && |
||
189 | $nameLen >= self::$nameMinLimit; |
||
190 | } |
||
191 | |||
192 | public function IsWithoutPersianAlphabet(string $value): bool |
||
193 | { |
||
194 | $hasPersianChar = (bool)preg_match("/[" . |
||
195 | self::$persian_text . |
||
196 | "]/u", $value); |
||
197 | |||
198 | return !$hasPersianChar; |
||
199 | } |
||
200 | |||
201 | public function IsWithoutNumber(string $value): bool |
||
202 | { |
||
203 | $hasPersianNumber = (bool)preg_match("/([" . |
||
204 | self::$persian_numbers . |
||
205 | self::$arabic_numbers . |
||
206 | "]+)/u", $value); |
||
207 | |||
208 | return !$hasPersianNumber; |
||
209 | } |
||
210 | } |
||
211 |