1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Coreproc\MsisdnPh; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
|
7
|
|
|
class Msisdn |
8
|
|
|
{ |
9
|
|
|
|
10
|
|
|
private $msisdn; |
11
|
|
|
|
12
|
|
|
private $smartPrefixes = null; |
13
|
|
|
|
14
|
|
|
private $globePrefixes = null; |
15
|
|
|
|
16
|
|
|
private $sunPrefixes = null; |
17
|
|
|
|
18
|
|
|
private $cherryPrefixes = null; |
19
|
|
|
|
20
|
|
|
private $absCbnPrefixes = null; |
21
|
|
|
|
22
|
|
|
private $extelcomPrefixes = null; |
23
|
|
|
|
24
|
12 |
|
private $nextPrefixes = null; |
25
|
|
|
|
26
|
12 |
|
private $prefix = null; |
27
|
3 |
|
|
28
|
|
|
private $operator = null; |
29
|
3 |
|
|
30
|
3 |
|
protected $countryPrefix = '+63'; |
31
|
|
|
|
32
|
3 |
|
public function __construct($msisdn) |
33
|
|
|
{ |
34
|
|
|
if (Msisdn::validate($msisdn) === false) { |
35
|
9 |
|
throw new Exception( |
36
|
9 |
|
'The supplied MSISDN is not valid. ' . |
37
|
|
|
'You can use the `Msisdn::validate()` method ' . |
38
|
|
|
'to validate the MSISDN being passed.', |
39
|
|
|
400 |
40
|
|
|
); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
$this->msisdn = Msisdn::clean($msisdn); |
44
|
|
|
} |
45
|
3 |
|
|
46
|
|
|
/** |
47
|
3 |
|
* Returns a formatted mobile number |
48
|
3 |
|
* |
49
|
|
|
* @param bool|false $countryCode |
50
|
3 |
|
* @param string $separator |
51
|
3 |
|
* @return mixed|string |
52
|
3 |
|
*/ |
53
|
3 |
|
function get($countryCode = false, $separator = '') |
|
|
|
|
54
|
|
|
{ |
55
|
3 |
|
if ($countryCode == false) { |
|
|
|
|
56
|
|
|
$formattedNumber = '0' . $this->msisdn; |
57
|
3 |
|
|
58
|
|
|
if (! empty($separator)) { |
59
|
3 |
|
$formattedNumber = substr_replace($formattedNumber, $separator, 4, 0); |
60
|
3 |
|
$formattedNumber = substr_replace($formattedNumber, $separator, 8, 0); |
61
|
3 |
|
} |
62
|
3 |
|
|
63
|
3 |
|
return $formattedNumber; |
64
|
|
|
} else { |
65
|
3 |
|
$formattedNumber = $this->countryPrefix . $this->msisdn; |
66
|
|
|
|
67
|
|
|
if (! empty($separator)) { |
68
|
|
|
$formattedNumber = substr_replace($formattedNumber, $separator, strlen($this->countryPrefix), 0); |
69
|
|
|
$formattedNumber = substr_replace($formattedNumber, $separator, 7, 0); |
70
|
|
|
$formattedNumber = substr_replace($formattedNumber, $separator, 11, 0); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return $formattedNumber; |
74
|
6 |
|
} |
75
|
|
|
} |
76
|
6 |
|
|
77
|
6 |
|
/** |
78
|
6 |
|
* Returns the prefix of the MSISDN number. |
79
|
|
|
* |
80
|
6 |
|
* @return string The prefix of the MSISDN number |
81
|
|
|
*/ |
82
|
|
|
public function getPrefix() |
83
|
|
|
{ |
84
|
|
|
if ($this->prefix == null) { |
85
|
|
|
$this->prefix = substr($this->msisdn, 0, 3); |
86
|
|
|
} |
87
|
|
|
|
88
|
3 |
|
return $this->prefix; |
89
|
|
|
} |
90
|
3 |
|
|
91
|
|
|
/** |
92
|
3 |
|
* Determines the operator of this number |
93
|
|
|
* |
94
|
|
|
* @return string The operator of this number |
95
|
|
|
*/ |
96
|
3 |
|
public function getOperator() |
97
|
3 |
|
{ |
98
|
3 |
|
$this->setPrefixes(); |
99
|
3 |
|
|
100
|
3 |
|
if (! empty($this->operator)) { |
101
|
3 |
|
return $this->operator; |
102
|
3 |
|
} |
103
|
3 |
|
|
104
|
|
|
$prefix = $this->getPrefix(); |
105
|
|
|
|
106
|
3 |
|
switch ($prefix) { |
107
|
|
|
case in_array($prefix, $this->smartPrefixes): |
108
|
|
|
$this->operator = 'SMART'; |
109
|
3 |
|
break; |
110
|
|
|
case in_array($prefix, $this->globePrefixes): |
111
|
3 |
|
$this->operator = 'GLOBE'; |
112
|
3 |
|
break; |
113
|
3 |
|
case in_array($prefix, $this->sunPrefixes): |
114
|
|
|
$this->operator = 'SUN'; |
115
|
3 |
|
break; |
116
|
3 |
|
case in_array($prefix, $this->absCbnPrefixes): |
117
|
3 |
|
$this->operator = 'ABS-CBN MOBILE'; |
118
|
|
|
break; |
119
|
3 |
|
case in_array($prefix, $this->cherryPrefixes): |
120
|
3 |
|
$this->operator = 'CHERRY MOBILE'; |
121
|
3 |
|
break; |
122
|
3 |
|
case in_array($prefix, $this->nextPrefixes): |
123
|
|
|
$this->operator = 'NEXT MOBILE'; |
124
|
|
|
break; |
125
|
|
|
case in_array($prefix, $this->extelcomPrefixes): |
126
|
|
|
$this->operator = 'EXTELCOM'; |
127
|
|
|
break; |
128
|
|
|
default: |
129
|
|
|
$this->operator = 'UNKNOWN'; |
130
|
18 |
|
} |
131
|
|
|
|
132
|
18 |
|
return $this->operator; |
133
|
|
|
} |
134
|
18 |
|
|
135
|
18 |
|
private function setPrefixes() |
136
|
18 |
|
{ |
137
|
|
|
if (empty($this->smartPrefixes)) { |
138
|
|
|
$this->smartPrefixes = json_decode(file_get_contents(__DIR__ . '/prefixes/smart.json')); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
if (empty($this->globePrefixes)) { |
142
|
|
|
$this->globePrefixes = json_decode(file_get_contents(__DIR__ . '/prefixes/globe.json')); |
143
|
|
|
} |
144
|
|
|
|
145
|
18 |
|
if (empty($this->sunPrefixes)) { |
146
|
|
|
$this->sunPrefixes = json_decode(file_get_contents(__DIR__ . '/prefixes/sun.json')); |
147
|
18 |
|
} |
148
|
|
|
|
149
|
|
|
if (empty($this->cherryPrefixes)) { |
150
|
18 |
|
$this->cherryPrefixes = json_decode(file_get_contents(__DIR__ . '/prefixes/cherry.json')); |
151
|
15 |
|
} |
152
|
18 |
|
|
153
|
9 |
|
if (empty($this->absCbnPrefixes)) { |
154
|
9 |
|
$this->absCbnPrefixes = json_decode(file_get_contents(__DIR__ . '/prefixes/abs-cbn.json')); |
155
|
|
|
} |
156
|
18 |
|
|
157
|
|
|
if (empty($this->extelcomPrefixes)) { |
158
|
|
|
$this->extelcomPrefixes = json_decode(file_get_contents(__DIR__ . '/prefixes/extelcom.json')); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
if (empty($this->nextPrefixes)) { |
162
|
|
|
$this->nextPrefixes = json_decode(file_get_contents(__DIR__ . '/prefixes/next.json')); |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Validate a given mobile number |
168
|
|
|
* |
169
|
|
|
* @param string $mobileNumber |
170
|
|
|
* @return bool |
171
|
|
|
*/ |
172
|
|
|
public static function validate($mobileNumber) |
173
|
|
|
{ |
174
|
|
|
$mobileNumber = Msisdn::clean($mobileNumber); |
175
|
|
|
|
176
|
|
|
return ! empty($mobileNumber) && |
177
|
|
|
strlen($mobileNumber) === 10 && |
178
|
|
|
is_numeric($mobileNumber); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Cleans the string |
183
|
|
|
* |
184
|
|
|
* @param string $msisdn |
185
|
|
|
* @return string The clean MSISDN |
186
|
|
|
*/ |
187
|
|
|
private static function clean($msisdn) |
188
|
|
|
{ |
189
|
|
|
$msisdn = preg_replace("/[^0-9]/", "", $msisdn); |
190
|
|
|
|
191
|
|
|
// We remove the 0 or 63 from the number |
192
|
|
|
if (substr($msisdn, 0, 1) == '0') { |
193
|
|
|
$msisdn = substr($msisdn, 1, strlen($msisdn)); |
194
|
|
|
} else { |
195
|
|
|
if (substr($msisdn, 0, 2) == '63') { |
196
|
|
|
$msisdn = substr($msisdn, 2, strlen($msisdn)); |
197
|
|
|
} |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
return $msisdn; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* Sets the country prefix - this defaults to +63 |
205
|
|
|
* |
206
|
|
|
* @param $countryPrefix |
207
|
|
|
*/ |
208
|
|
|
public function setCountryPrefix($countryPrefix) |
209
|
|
|
{ |
210
|
|
|
$this->countryPrefix = $countryPrefix; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
} |
214
|
|
|
|
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.