1 | <?php |
||
2 | |||
3 | namespace Validate; |
||
4 | |||
5 | class Phone implements \Validate\Contracts\Validate |
||
6 | { |
||
7 | |||
8 | public static function toDatabase(string $phone) |
||
9 | { |
||
10 | $phone = preg_replace('/[^0-9]/', '', $phone); |
||
11 | if (substr((string) $phone, 0, 2)=='55') { |
||
12 | return $phone; |
||
13 | } |
||
14 | if (empty($phone) || $phone=='55') { |
||
15 | return null; |
||
16 | } |
||
17 | if (strlen($phone)>11) { |
||
18 | return $phone; |
||
19 | } |
||
20 | return '55'.$phone; |
||
21 | } |
||
22 | |||
23 | public static function toUser($phone) |
||
24 | { |
||
25 | return $phone; |
||
26 | } |
||
27 | |||
28 | public static function validate($phoneNumber) |
||
29 | { |
||
30 | $phone = self::break($phoneNumber); |
||
31 | |||
32 | if ((int) $phone['country'] === 0 ) { |
||
33 | return false; |
||
34 | } |
||
35 | |||
36 | if ((int) $phone['region'] === 0 ) { |
||
37 | return false; |
||
38 | } |
||
39 | |||
40 | if ((int) $phone['number'] === 0 ) { |
||
41 | return false; |
||
42 | } |
||
43 | |||
44 | if (strlen(static::toDatabase($phoneNumber)) < 12 || strlen(static::toDatabase($phoneNumber)) > 13) { |
||
45 | return false; |
||
46 | } |
||
47 | |||
48 | return true; |
||
49 | } |
||
50 | |||
51 | public static function break(string $phone) |
||
52 | { |
||
53 | $phone = static::toDatabase($phone); |
||
54 | $data['country'] = '55'; |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
![]() |
|||
55 | $data['region'] = '61'; |
||
56 | |||
57 | if (strlen($phone)>=12) { |
||
58 | $data['country'] = substr((string) $phone, 0, 2); |
||
59 | $phone = substr((string) $phone, 2, strlen($phone)-2); |
||
60 | } |
||
61 | |||
62 | if (strlen($phone)>=10) { |
||
63 | $data['region'] = substr((string) $phone, 0, 2); |
||
64 | $phone = substr((string) $phone, 2, strlen($phone)-2); |
||
65 | } |
||
66 | |||
67 | $data['number'] = $phone; |
||
68 | return $data; |
||
69 | } |
||
70 | |||
71 | public static function isSame(string $to, string $from) |
||
72 | { |
||
73 | return (self::toDatabase($to)===self::toDatabase($from)); |
||
74 | } |
||
75 | |||
76 | } |
||
77 |