1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace APiChef\SlPhoneNumber; |
4
|
|
|
|
5
|
|
|
class PhoneNumber |
6
|
|
|
{ |
7
|
|
|
const TYPE_MOBILE = 'mobile'; |
8
|
|
|
const TYPE_FIXED = 'fixed'; |
9
|
|
|
|
10
|
|
|
private $areaCodes = [ |
11
|
|
|
'011' => ['province' => 'Western', 'district' => 'Colombo', 'area' => 'Colombo'], |
12
|
|
|
'021' => ['province' => 'Northern', 'district' => 'Jaffna', 'area' => 'Jaffna'], |
13
|
|
|
'023' => ['province' => 'Northern', 'district' => 'Mannar', 'area' => 'Mannar'], |
14
|
|
|
'024' => ['province' => 'Northern', 'district' => 'Vavuniya', 'area' => 'Vavuniya'], |
15
|
|
|
'025' => ['province' => 'North Central', 'district' => 'Anuradhapura', 'area' => 'Anuradhapura'], |
16
|
|
|
'026' => ['province' => 'Eastern', 'district' => 'Trincomalee', 'area' => 'Trincomalee'], |
17
|
|
|
'027' => ['province' => 'North Central', 'district' => 'Polonnaruwa', 'area' => 'Polonnaruwa'], |
18
|
|
|
'031' => ['province' => 'Western', 'district' => 'Gampaha', 'area' => 'Negombo'], |
19
|
|
|
'032' => ['province' => 'North Western', 'district' => 'Puttalam', 'area' => 'Chilaw'], |
20
|
|
|
'033' => ['province' => 'Western', 'district' => 'Gampaha', 'area' => 'Gampaha'], |
21
|
|
|
'034' => ['province' => 'Western', 'district' => 'Kalutara', 'area' => 'Kalutara'], |
22
|
|
|
'035' => ['province' => 'Sabaragamuwa', 'district' => 'Kegalle', 'area' => 'Kegalle'], |
23
|
|
|
'036' => [ |
24
|
|
|
'province' => 'Western,Sabaragamuwa', |
25
|
|
|
'district' => 'Colombo,Rathnapura,Kegalle', |
26
|
|
|
'area' => 'Avissawella' |
27
|
|
|
], |
28
|
|
|
'037' => ['province' => 'North Western', 'district' => 'Kurunegala', 'area' => 'Kurunegala'], |
29
|
|
|
'038' => ['province' => 'Western', 'district' => 'Kalutara', 'area' => 'Panadura'], |
30
|
|
|
'041' => ['province' => 'Southern', 'district' => 'Matara', 'area' => 'Matara'], |
31
|
|
|
'045' => ['province' => 'Sabaragamuwa', 'district' => 'Ratnapura', 'area' => 'Ratnapura'], |
32
|
|
|
'047' => ['province' => 'Southern', 'district' => 'Hambantota', 'area' => 'Hambantota'], |
33
|
|
|
'051' => ['province' => 'Central', 'district' => 'Nuwara Eliya', 'area' => 'Hatton'], |
34
|
|
|
'052' => ['province' => 'Central', 'district' => 'Nuwara Eliya', 'area' => 'Nuwara Eliya'], |
35
|
|
|
'054' => ['province' => 'Central', 'district' => 'Kandy', 'area' => 'Nawalapitiya'], |
36
|
|
|
'055' => ['province' => 'Uva', 'district' => 'Monaragala,Badulla', 'area' => 'Monaragala,Badulla'], |
37
|
|
|
'057' => ['province' => 'Uva', 'district' => 'Badulla', 'area' => 'Bandarawela'], |
38
|
|
|
'063' => ['province' => 'Eastern', 'district' => 'Ampara', 'area' => 'Ampara'], |
39
|
|
|
'065' => ['province' => 'Eastern', 'district' => 'Batticaloa', 'area' => 'Batticaloa'], |
40
|
|
|
'066' => ['province' => 'Central', 'district' => 'Matale', 'area' => 'Matale'], |
41
|
|
|
'067' => ['province' => 'Eastern', 'district' => 'Ampara', 'area' => 'Kalmunai'], |
42
|
|
|
'081' => ['province' => 'Central', 'district' => 'Kandy', 'area' => 'Kandy'], |
43
|
|
|
'091' => ['province' => 'Southern', 'district' => 'Galle', 'area' => 'Galle'], |
44
|
|
|
]; |
45
|
|
|
|
46
|
|
|
private $operatorCodes = [ |
47
|
|
|
'0' => ['name' => 'Lanka Bell', 'type' => 'Fixed LTE'], |
48
|
|
|
'2' => ['name' => 'Sri Lanka Telecom', 'type' => 'Fixed Fibre or Copper'], |
49
|
|
|
'3' => ['name' => 'Sri Lanka Telecom', 'type' => 'Fixed CDMA or LTE'], |
50
|
|
|
'4' => ['name' => 'Dialog', 'type' => 'Fixed LTE'], |
51
|
|
|
'5' => ['name' => 'Lanka Bell', 'type' => 'Fixed CDMA'], |
52
|
|
|
'7' => ['name' => 'Dialog', 'type' => 'Fixed LTE'], |
53
|
|
|
'9' => ['name' => 'Tritel', 'type' => 'Public Payphones'], |
54
|
|
|
]; |
55
|
|
|
|
56
|
|
|
private $mobileOperatorCodes = [ |
57
|
|
|
'070' => ['name' => 'Mobitel'], |
58
|
|
|
'071' => ['name' => 'Mobitel'], |
59
|
|
|
'072' => ['name' => 'Hutch'], |
60
|
|
|
'074' => ['name' => 'Dialog'], |
61
|
|
|
'075' => ['name' => 'Airtel'], |
62
|
|
|
'076' => ['name' => 'Dialog'], |
63
|
|
|
'077' => ['name' => 'Dialog'], |
64
|
|
|
'078' => ['name' => 'Hutch'], |
65
|
|
|
]; |
66
|
|
|
|
67
|
|
|
/** @var string */ |
68
|
|
|
private $number; |
69
|
|
|
|
70
|
120 |
|
public function __construct(string $number) |
71
|
|
|
{ |
72
|
120 |
|
$this->number = $number; |
73
|
120 |
|
} |
74
|
|
|
|
75
|
102 |
|
public function getDetails(): PhoneNumberDetails |
76
|
|
|
{ |
77
|
102 |
|
if ($this->isFormatValid()) { |
78
|
48 |
|
$number = $this->toLocalFormat(); |
79
|
48 |
|
$prefix = substr($number, 0, 3); |
80
|
|
|
|
81
|
48 |
|
if (array_key_exists($prefix, $this->mobileOperatorCodes)) { |
82
|
30 |
|
return new PhoneNumberDetails($number, self::TYPE_MOBILE, $this->mobileOperatorCodes[$prefix]['name']); |
83
|
|
|
} |
84
|
|
|
|
85
|
21 |
|
if (array_key_exists($prefix, $this->areaCodes)) { |
86
|
21 |
|
$operatorCode = substr($number, 3, 1); |
87
|
21 |
|
if (array_key_exists($operatorCode, $this->operatorCodes)) { |
88
|
21 |
|
return new PhoneNumberDetails( |
89
|
21 |
|
$number, |
90
|
21 |
|
self::TYPE_FIXED, |
91
|
21 |
|
$this->operatorCodes[$operatorCode]['name'], |
92
|
21 |
|
$this->areaCodes[$prefix] |
93
|
|
|
); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
54 |
|
throw new \InvalidArgumentException('Invalid phone number.'); |
99
|
|
|
} |
100
|
|
|
|
101
|
45 |
|
public function isValid(): bool |
102
|
|
|
{ |
103
|
|
|
try { |
104
|
45 |
|
$this->getDetails(); |
105
|
|
|
|
106
|
18 |
|
return true; |
107
|
27 |
|
} catch (\InvalidArgumentException $exception) { |
108
|
27 |
|
return false; |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
112
|
102 |
|
private function isFormatValid(): bool |
113
|
|
|
{ |
114
|
102 |
|
return preg_match('/^0[0-9]{9}$/', $this->number) || |
115
|
78 |
|
preg_match('/^\+94[0-9]{9}$/', $this->number) || |
116
|
102 |
|
preg_match('/^0094[0-9]{9}$/', $this->number); |
117
|
|
|
} |
118
|
|
|
|
119
|
66 |
|
public function toLocalFormat(): string |
120
|
|
|
{ |
121
|
66 |
|
$number = $this->number; |
122
|
|
|
|
123
|
66 |
|
if (substr($this->number, 0, 3) === '+94') { |
124
|
18 |
|
$number = '0' . substr($this->number, 3); |
125
|
|
|
} |
126
|
|
|
|
127
|
66 |
|
if (substr($this->number, 0, 4) === '0094') { |
128
|
18 |
|
$number = '0' . substr($this->number, 4); |
129
|
|
|
} |
130
|
|
|
|
131
|
66 |
|
return $number; |
132
|
|
|
} |
133
|
|
|
|
134
|
9 |
|
public function toIDDFormat(): string |
135
|
|
|
{ |
136
|
9 |
|
return '+94' . substr($this->toLocalFormat(), 1); |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|