|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Digitonic\Validation\Validators; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Validation\Concerns\ValidatesAttributes; |
|
6
|
|
|
use libphonenumber\NumberParseException; |
|
7
|
|
|
use libphonenumber\PhoneNumberUtil; |
|
8
|
|
|
use libphonenumber\PhoneNumberFormat; |
|
9
|
|
|
|
|
10
|
|
|
class AllowedRecipientsValidatorSanitiser |
|
11
|
|
|
{ |
|
12
|
|
|
use ValidatesAttributes; |
|
13
|
|
|
|
|
14
|
|
|
protected $libPhoneNumber; |
|
15
|
|
|
|
|
16
|
2 |
|
public function __construct() |
|
17
|
|
|
{ |
|
18
|
2 |
|
$this->libPhoneNumber = PhoneNumberUtil::getInstance(); |
|
19
|
2 |
|
} |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @param $attribute |
|
23
|
|
|
* @param $value |
|
24
|
|
|
* @param $parameters |
|
25
|
|
|
* @param $validator |
|
26
|
|
|
* @return bool |
|
27
|
|
|
*/ |
|
28
|
1 |
|
public function validate($attribute, $value, $parameters, $validator): bool |
|
|
|
|
|
|
29
|
|
|
{ |
|
30
|
1 |
|
$phoneNumbers = collect(explode(',', $value)); |
|
31
|
|
|
|
|
32
|
1 |
|
$invalidNumbers = []; |
|
33
|
|
|
|
|
34
|
1 |
|
foreach ($phoneNumbers as $phoneNumber) { |
|
35
|
1 |
|
$proto = $this->getPhoneNumberObject($phoneNumber); |
|
36
|
|
|
|
|
37
|
1 |
|
if (!$proto || |
|
38
|
1 |
|
!in_array( |
|
39
|
1 |
|
$this->libPhoneNumber->getRegionCodeForNumber($proto), |
|
40
|
1 |
|
config('digitonic.validation.allowed_mobile_origins') |
|
41
|
|
|
) |
|
42
|
|
|
) { |
|
43
|
1 |
|
$invalidNumbers[] = $phoneNumber; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
1 |
|
return count($invalidNumbers) === 0; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Get formatted phone number in E.164 standard without (+). |
|
53
|
|
|
* This method will return false if the number is not valid. |
|
54
|
|
|
* |
|
55
|
|
|
* @param string $phoneNumber |
|
56
|
|
|
* |
|
57
|
|
|
* @return mixed |
|
58
|
|
|
*/ |
|
59
|
1 |
|
public function getFormattedNumber(string $phoneNumber) |
|
60
|
|
|
{ |
|
61
|
1 |
|
$phoneNumberObject = $this->getPhoneNumberObject($phoneNumber); |
|
62
|
|
|
|
|
63
|
1 |
|
if ($phoneNumberObject) { |
|
64
|
1 |
|
if (in_array( |
|
65
|
1 |
|
$this->libPhoneNumber->getRegionCodeForNumber($phoneNumberObject), |
|
66
|
1 |
|
config('digitonic.validation.allowed_mobile_origins') |
|
67
|
|
|
)) { |
|
68
|
|
|
// Format number to E.164 standard and remove '+' |
|
69
|
1 |
|
$formattedPhone = str_replace( |
|
70
|
1 |
|
'+', |
|
71
|
1 |
|
'', |
|
72
|
1 |
|
$this->libPhoneNumber->format($phoneNumberObject, PhoneNumberFormat::E164) |
|
73
|
|
|
); |
|
74
|
|
|
|
|
75
|
1 |
|
return $formattedPhone; |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
1 |
|
return false; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Get phoneNumberObject. |
|
84
|
|
|
* |
|
85
|
|
|
* @param string $phoneNumber |
|
86
|
|
|
* |
|
87
|
|
|
* @return mixed |
|
88
|
|
|
*/ |
|
89
|
2 |
|
protected function getPhoneNumberObject(string $phoneNumber) |
|
90
|
|
|
{ |
|
91
|
|
|
try { |
|
92
|
2 |
|
if (!preg_match('/[A-Za-z]/', $phoneNumber)) { |
|
93
|
2 |
|
$phoneNumberObject = $this->libPhoneNumber->parse($this->sanitisePhoneNumber($phoneNumber), null); |
|
94
|
2 |
|
if ($this->libPhoneNumber->isPossibleNumber($phoneNumberObject)) { |
|
95
|
2 |
|
return $phoneNumberObject; |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
1 |
|
} catch (NumberParseException $exception) { |
|
99
|
|
|
// Do nothing |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
2 |
|
return false; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* @param string $phoneNumber |
|
107
|
|
|
* @return string |
|
108
|
|
|
*/ |
|
109
|
2 |
|
protected function sanitisePhoneNumber(string $phoneNumber): string |
|
110
|
|
|
{ |
|
111
|
2 |
|
$phoneNumber = $this->getDigits($phoneNumber); |
|
112
|
|
|
|
|
113
|
2 |
|
return $this->formatNumber($phoneNumber); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* @param string $phoneNumber |
|
118
|
|
|
* @return string |
|
119
|
|
|
*/ |
|
120
|
2 |
|
protected function getDigits(string $phoneNumber): string |
|
121
|
|
|
{ |
|
122
|
2 |
|
return preg_replace('#\D+#', '', $phoneNumber); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* @param string $phoneNumber |
|
127
|
|
|
* @return string |
|
128
|
|
|
*/ |
|
129
|
2 |
|
protected function formatNumber(string $phoneNumber): string |
|
130
|
|
|
{ |
|
131
|
|
|
// Format UK numbers starting with 07 or 7 |
|
132
|
2 |
|
preg_match('/^0*(7\d{9})$/', $phoneNumber, $outputUkPhone); |
|
133
|
|
|
|
|
134
|
2 |
|
if ($outputUkPhone) { |
|
|
|
|
|
|
135
|
2 |
|
return '+44' . $outputUkPhone[1]; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
2 |
|
return '+' . $phoneNumber; |
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.