-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPhone.php
59 lines (46 loc) · 1.48 KB
/
Phone.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<?php
class RM_Phone {
public static $validationRegex = '/^\+?[0-9]{8,14}$/';
private $_phone;
public static function clearPhoneNumber($phone) {
return preg_replace('/\D/', '', $phone);
}
public function __construct($phone) {
$this->setPhoneNumber($phone, false);
}
public function setPhoneNumber($phoneNumber, $validate = true) {
$this->_phone = self::clearPhoneNumber($phoneNumber);
if ($validate) $this->validate();
}
public function clear() {
$this->_phone = '';
}
public function getPhoneNumber() {
return $this->_phone;
}
public function getNumberWithCode($code) {
return preg_replace('/.*(\d{10})/', '+' . $code . '${1}', $this->getPhoneNumber());
}
public function validate() {
if (!preg_match(static::$validationRegex, $this->getPhoneNumber()))
throw new Exception('Wrong phone format');
return true;
}
public function isEmpty() {
return $this->getPhoneNumber() == '';
}
public function getFormatedPhoneNumber() {
return str_replace(array(
'+'
), array(
''
), $this->getPhoneNumber());
}
public function getPrettyPhoneFormat() {
return preg_replace(
'/^(\+[0-9]{2})?([0-9]{3})([0-9]*)([0-9]{2})([0-9]{2})$/',
'${1} (${2}) ${3} ${4} ${5}',
$this->getPhoneNumber()
);
}
}