1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Zenapply\DataTypes; |
4
|
|
|
|
5
|
|
|
use libphonenumber\PhoneNumberUtil as Util; |
6
|
|
|
use libphonenumber\PhoneNumberFormat as Format; |
7
|
|
|
use libphonenumber\PhoneNumberType as Type; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @see https://en.wikipedia.org/wiki/Telephone_numbering_plan#Structure |
11
|
|
|
*/ |
12
|
|
|
class Telephone |
13
|
|
|
{ |
14
|
|
|
//Telephone variations |
15
|
|
|
protected $format_E164; |
16
|
|
|
protected $format_international; |
17
|
|
|
protected $format_national; |
18
|
|
|
protected $format_RFC3966; |
19
|
|
|
|
20
|
|
|
//Telephone structure |
21
|
|
|
protected $country_code; |
22
|
|
|
protected $area_code; |
23
|
|
|
protected $subscriber_code; |
24
|
|
|
protected $extension; |
25
|
|
|
|
26
|
|
|
//Telephone extras |
27
|
|
|
protected $region; |
28
|
|
|
protected $input; |
29
|
|
|
|
30
|
|
|
//Telephone types |
31
|
|
|
protected $typeMap = [ |
32
|
|
|
Type::FIXED_LINE => 'FIXED_LINE', |
33
|
|
|
Type::MOBILE => 'MOBILE', |
34
|
|
|
Type::FIXED_LINE_OR_MOBILE => 'FIXED_LINE_OR_MOBILE', |
35
|
|
|
Type::TOLL_FREE => 'TOLL_FREE', |
36
|
|
|
Type::PREMIUM_RATE => 'PREMIUM_RATE', |
37
|
|
|
Type::SHARED_COST => 'SHARED_COST', |
38
|
|
|
Type::VOIP => 'VOIP', |
39
|
|
|
Type::PERSONAL_NUMBER => 'PERSONAL_NUMBER', |
40
|
|
|
Type::PAGER => 'PAGER', |
41
|
|
|
Type::UAN => 'UAN', |
42
|
|
|
Type::UNKNOWN => 'UNKNOWN', |
43
|
|
|
Type::EMERGENCY => 'EMERGENCY', |
44
|
|
|
Type::VOICEMAIL => 'VOICEMAIL', |
45
|
|
|
Type::SHORT_CODE => 'SHORT_CODE', |
46
|
|
|
Type::STANDARD_RATE => 'STANDARD_RATE', |
47
|
|
|
]; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param string $input [description] |
51
|
|
|
* @param string $region [description] |
52
|
|
|
*/ |
53
|
3 |
|
public function __construct($input,$region = 'US') |
54
|
|
|
{ |
55
|
3 |
|
$this->input = $input; |
56
|
3 |
|
$this->region = $region; |
57
|
3 |
|
$this->setup(); |
58
|
3 |
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @return void |
62
|
|
|
*/ |
63
|
3 |
|
protected function setup() |
64
|
|
|
{ |
65
|
3 |
|
$util = Util::getInstance(); |
66
|
3 |
|
$number = $util->parse($this->input, $this->region); |
67
|
|
|
|
68
|
|
|
//Meta |
69
|
3 |
|
$this->valid = $util->isValidNumber($number); |
|
|
|
|
70
|
3 |
|
$this->type = $this->typeMap[$util->getNumberType($number)]; |
|
|
|
|
71
|
|
|
|
72
|
|
|
//Formats |
73
|
3 |
|
$this->format_E164 = $util->format($number, Format::E164); |
74
|
3 |
|
$this->format_international = $util->format($number, Format::INTERNATIONAL); |
75
|
3 |
|
$this->format_national = $util->format($number, Format::NATIONAL); |
76
|
3 |
|
$this->format_RFC3966 = $util->format($number, Format::RFC3966); |
77
|
|
|
|
78
|
|
|
//Get the parts |
79
|
3 |
|
$this->country_code = $number->getCountryCode(); |
80
|
3 |
|
$this->extension = $number->getExtension(); |
81
|
3 |
|
$this->area_code = $this->extractAreaCode(); |
82
|
3 |
|
} |
83
|
|
|
|
84
|
3 |
|
protected function extractAreaCode() |
85
|
|
|
{ |
86
|
3 |
|
if($this->region === 'US' && $this->isValid()){ |
87
|
3 |
|
$exp = '/^\+?(1)?\(?(\d{3})\)?(?:\s|\.|\-)?(\d{3})(?:\s|\.|\-)?(\d{4})$/'; |
88
|
3 |
|
preg_match($exp,$this->format_E164,$matches); |
89
|
3 |
|
if(count($matches) >= 2){ |
90
|
3 |
|
return $matches[2]; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
return null; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/*===================================== |
98
|
|
|
= Ouput Methods = |
99
|
|
|
=====================================*/ |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @return string |
103
|
|
|
*/ |
104
|
|
|
public function getRFC3966() |
105
|
|
|
{ |
106
|
|
|
return $this->format_RFC3966; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @return string |
111
|
|
|
*/ |
112
|
|
|
public function getNational() |
113
|
|
|
{ |
114
|
|
|
return $this->format_national; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @return string |
119
|
|
|
*/ |
120
|
|
|
public function getInternational() |
121
|
|
|
{ |
122
|
|
|
return $this->format_international; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @return string |
127
|
|
|
*/ |
128
|
|
|
public function getE164() |
129
|
|
|
{ |
130
|
|
|
return $this->format_E164; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @return string |
136
|
|
|
*/ |
137
|
|
|
public function getOriginal() |
138
|
|
|
{ |
139
|
|
|
return $this->input; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @return string |
144
|
|
|
*/ |
145
|
|
|
public function getAreaCode() |
146
|
|
|
{ |
147
|
|
|
return $this->area_code; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @return string |
152
|
|
|
*/ |
153
|
|
|
public function getCountryCode() |
154
|
|
|
{ |
155
|
|
|
return $this->country_code; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @return string |
160
|
|
|
*/ |
161
|
|
|
public function getSubscriberCode() |
162
|
|
|
{ |
163
|
|
|
return $this->subscriber_code; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* @return string |
168
|
|
|
*/ |
169
|
3 |
|
public function isValid() |
170
|
|
|
{ |
171
|
3 |
|
return $this->valid; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* @return string |
176
|
|
|
*/ |
177
|
|
|
public function __toString() |
178
|
|
|
{ |
179
|
|
|
return $this->getE164(); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/*===== End of Ouput Methods ======*/ |
183
|
|
|
} |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: