|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Digitonic\Validation\Services; |
|
4
|
|
|
|
|
5
|
|
|
use libphonenumber\NumberParseException; |
|
6
|
|
|
use libphonenumber\PhoneNumberUtil; |
|
7
|
|
|
use libphonenumber\PhoneNumberFormat; |
|
8
|
|
|
|
|
9
|
|
|
class AllowedRecipientsSanitiser |
|
10
|
|
|
{ |
|
11
|
|
|
protected $libPhoneNumber; |
|
12
|
|
|
|
|
13
|
4 |
|
public function __construct() |
|
14
|
|
|
{ |
|
15
|
4 |
|
$this->libPhoneNumber = PhoneNumberUtil::getInstance(); |
|
16
|
4 |
|
} |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Get formatted phone number in E.164 standard without (+). |
|
20
|
|
|
* This method will return false if the number is not valid. |
|
21
|
|
|
* |
|
22
|
|
|
* @param string $phoneNumber |
|
23
|
|
|
* |
|
24
|
|
|
* @return mixed |
|
25
|
|
|
*/ |
|
26
|
1 |
|
public function getFormattedNumber(string $phoneNumber) |
|
27
|
|
|
{ |
|
28
|
1 |
|
$phoneNumberObject = $this->getPhoneNumberObject($phoneNumber); |
|
29
|
|
|
|
|
30
|
1 |
|
if ($phoneNumberObject) { |
|
31
|
1 |
|
if (in_array( |
|
32
|
1 |
|
$this->libPhoneNumber->getRegionCodeForNumber($phoneNumberObject), |
|
33
|
1 |
|
config('digitonic.validation.allowed_mobile_origins') |
|
34
|
|
|
)) { |
|
35
|
|
|
// Format number to E.164 standard and remove '+' |
|
36
|
1 |
|
$formattedPhone = str_replace( |
|
37
|
1 |
|
'+', |
|
38
|
1 |
|
'', |
|
39
|
1 |
|
$this->libPhoneNumber->format($phoneNumberObject, PhoneNumberFormat::E164) |
|
40
|
|
|
); |
|
41
|
|
|
|
|
42
|
1 |
|
return $formattedPhone; |
|
43
|
|
|
} |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
1 |
|
return false; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Get phoneNumberObject. |
|
51
|
|
|
* |
|
52
|
|
|
* @param string $phoneNumber |
|
53
|
|
|
* |
|
54
|
|
|
* @return mixed |
|
55
|
|
|
*/ |
|
56
|
3 |
|
public function getPhoneNumberObject(string $phoneNumber) |
|
57
|
|
|
{ |
|
58
|
|
|
try { |
|
59
|
3 |
|
if (!preg_match('/[A-Za-z]/', $phoneNumber)) { |
|
60
|
3 |
|
$phoneNumberObject = $this->libPhoneNumber->parse($this->sanitisePhoneNumber($phoneNumber), null); |
|
61
|
3 |
|
if ($this->libPhoneNumber->isPossibleNumber($phoneNumberObject)) { |
|
62
|
3 |
|
return $phoneNumberObject; |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
1 |
|
} catch (NumberParseException $exception) { |
|
66
|
|
|
// Do nothing |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
2 |
|
return false; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @param string $phoneNumber |
|
74
|
|
|
* @return string |
|
75
|
|
|
*/ |
|
76
|
4 |
|
public function sanitisePhoneNumber(string $phoneNumber): string |
|
77
|
|
|
{ |
|
78
|
4 |
|
$phoneNumber = $this->getDigits($phoneNumber); |
|
79
|
|
|
|
|
80
|
4 |
|
return $this->formatNumber($phoneNumber); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @param string $phoneNumber |
|
85
|
|
|
* @return string |
|
86
|
|
|
*/ |
|
87
|
4 |
|
protected function getDigits(string $phoneNumber): string |
|
88
|
|
|
{ |
|
89
|
4 |
|
return preg_replace('#\D+#', '', $phoneNumber); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @param string $phoneNumber |
|
94
|
|
|
* @return string |
|
95
|
|
|
*/ |
|
96
|
4 |
|
protected function formatNumber(string $phoneNumber): string |
|
97
|
|
|
{ |
|
98
|
|
|
// Format UK numbers starting with 07 or 7 |
|
99
|
4 |
|
preg_match('/^0*(7\d{9})$/', $phoneNumber, $outputUkPhone); |
|
100
|
|
|
|
|
101
|
4 |
|
if ($outputUkPhone) { |
|
|
|
|
|
|
102
|
4 |
|
return '+44' . $outputUkPhone[1]; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
4 |
|
return '+' . $phoneNumber; |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.