1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | namespace cse\helpers; |
||
6 | |||
7 | /** |
||
8 | * Class Phone |
||
9 | * |
||
10 | * @package cse\helpers |
||
11 | */ |
||
12 | class Phone |
||
13 | { |
||
14 | const FORMAT_DEFAULT = '+$1 ($2) $3-$4-$5'; |
||
15 | const FORMAT_HIDE = '+$1 ($2) ***-**-$5'; |
||
16 | |||
17 | const MASK = '?'; |
||
18 | const PATTERN = '(.{2})(.{2})(.{3})(.{3})(.*)'; |
||
19 | const REVERT_MASK = ['$5' => '1$', '$4' => '2$', '$3' => '3$', '$2' => '4$', '$1' => '5$']; |
||
20 | |||
21 | const SIZE_MIN = 7; |
||
22 | const SIZE_MAX = 14; |
||
23 | |||
24 | /** |
||
25 | * Clear phone number |
||
26 | * |
||
27 | * @param $phone |
||
28 | * |
||
29 | * @return string |
||
30 | */ |
||
31 | public static function clear($phone): string |
||
32 | { |
||
33 | return preg_replace('/[^0-9]+/', '', $phone); |
||
34 | } |
||
35 | |||
36 | /** |
||
37 | * Format phone number |
||
38 | * |
||
39 | * @param $phone |
||
40 | * @param string $format |
||
41 | * @param bool $isMask |
||
42 | * @param string $pattern |
||
43 | * @param array $revert |
||
44 | * |
||
45 | * @return string |
||
46 | */ |
||
47 | public static function format($phone, string $format = self::FORMAT_DEFAULT, bool $isMask = true, string $pattern = self::PATTERN, array $revert = self::REVERT_MASK): string |
||
48 | { |
||
49 | $phone = self::clear($phone); |
||
50 | |||
51 | if ($isMask) $phone = str_pad($phone, 11, self::MASK, STR_PAD_LEFT); |
||
52 | |||
53 | $result = preg_replace( |
||
54 | '/' . $pattern . '/', |
||
55 | strrev(strtr($format, $revert)), |
||
56 | strrev($phone) |
||
57 | ); |
||
58 | |||
59 | if ($isMask) { |
||
60 | $mask_position = stripos($result, self::MASK); |
||
61 | if (is_int($mask_position)) { |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
62 | if ($result[$mask_position-1] == ')' || $result[$mask_position-1] == '-') $mask_position--; |
||
63 | $result = substr($result,0, $mask_position); |
||
64 | } |
||
65 | } |
||
66 | |||
67 | return trim(strrev($result)); |
||
68 | } |
||
69 | |||
70 | /** |
||
71 | * Hide phone number |
||
72 | * |
||
73 | * @param $phone |
||
74 | * @param string $format |
||
75 | * |
||
76 | * @return string |
||
77 | */ |
||
78 | public static function hide($phone, $format = self::FORMAT_HIDE): string |
||
79 | { |
||
80 | return self::format($phone, $format); |
||
81 | } |
||
82 | |||
83 | /** |
||
84 | * Extract phone number from string |
||
85 | * |
||
86 | * @param $string |
||
87 | * @param int $minSize |
||
88 | * @param int $maxSize |
||
89 | * |
||
90 | * @return null|string |
||
91 | */ |
||
92 | public static function extract($string, int $minSize = self::SIZE_MIN, int $maxSize = self::SIZE_MAX): ?string |
||
93 | { |
||
94 | $string = self::clear($string); |
||
95 | $size = strlen($string); |
||
96 | |||
97 | return $size >= $minSize && $size <= $maxSize ? $string : null; |
||
98 | } |
||
99 | |||
100 | /** |
||
101 | * Exist phone number to string |
||
102 | * |
||
103 | * @param $string |
||
104 | * @param int $minSize |
||
105 | * @param int $maxSize |
||
106 | * |
||
107 | * @return bool |
||
108 | */ |
||
109 | public static function exist($string, int $minSize = self::SIZE_MIN, int $maxSize = self::SIZE_MAX): bool |
||
110 | { |
||
111 | return (bool) self::extract($string, $minSize, $maxSize); |
||
112 | } |
||
113 | |||
114 | /** |
||
115 | * Check phone number |
||
116 | * |
||
117 | * @param $phone |
||
118 | * @param int $minSize |
||
119 | * @param int $maxSize |
||
120 | * |
||
121 | * @return bool |
||
122 | */ |
||
123 | public static function is($phone, int $minSize = self::SIZE_MIN, int $maxSize = self::SIZE_MAX): bool |
||
124 | { |
||
125 | $size = strlen((string) $phone); |
||
126 | return $size >= $minSize && $size <= $maxSize ? self::exist($phone, $minSize, $maxSize) : false; |
||
127 | } |
||
128 | } |