1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Milwad\LaravelValidate\Utils; |
4
|
|
|
|
5
|
|
|
class CountryPhoneCallback |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* Create a new phone validator instance. |
9
|
|
|
* |
10
|
|
|
* @param mixed $value The phone number to validate. |
11
|
|
|
* @param string $code The country codes to validate against. String can be separated by comma |
12
|
|
|
* @param string|null $attribute |
13
|
|
|
*/ |
14
|
|
|
public function __construct(private mixed $value, private string $code, string|null $attribute = null) |
15
|
|
|
{ |
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
// TODO: Add a feature to add validate method for your own country! |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Validate Iran phone number. |
22
|
|
|
* |
23
|
|
|
* @return false|int |
24
|
|
|
*/ |
25
|
|
|
protected function validateIR() |
26
|
|
|
{ |
27
|
|
|
return preg_match('/^(\+98|0)?9\d{9}$/', $this->value); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Validate Iran phone number. |
32
|
|
|
* |
33
|
|
|
* @return false|int |
34
|
|
|
*/ |
35
|
|
|
protected function validateEN() |
36
|
|
|
{ |
37
|
|
|
return preg_match('/^(?:\+44|0)7\d{9}$/', $this->value); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Validate Nigeria phone number. |
42
|
|
|
* |
43
|
|
|
* @return false|int |
44
|
|
|
*/ |
45
|
|
|
protected function validateNE() |
46
|
|
|
{ |
47
|
|
|
return preg_match('/^(\+227|00227|227)?\d{8}$/', $this->value); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Validate Saudi Arabia phone number. |
52
|
|
|
* |
53
|
|
|
* @return false|int |
54
|
|
|
*/ |
55
|
|
|
protected function validateSA() |
56
|
|
|
{ |
57
|
|
|
return preg_match('/^((\+966)|0)?5\d{8}$/', $this->value); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Validate Germany phone number. |
62
|
|
|
* |
63
|
|
|
* @return false|int |
64
|
|
|
*/ |
65
|
|
|
protected function validateDE() |
66
|
|
|
{ |
67
|
|
|
return preg_match('/^((\+49)|(0))(1|15|16|17|19|30|31|32|33|34|40|41|42|43|44|49|151|152|153|155|156|157|159|160|162|163|180|181|182|183|184|185|186|187|188|170|171|172|173|174|175|176|177|178|179)\d{7,8}$/', $this->value); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Validate Greece phone number. |
72
|
|
|
* |
73
|
|
|
* @return false|int |
74
|
|
|
*/ |
75
|
|
|
protected function validateGR() |
76
|
|
|
{ |
77
|
|
|
return preg_match('/^\+30[2-9]\d{2}\d{3}\d{4}$/', $this->value); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Validate Spain phone number. |
82
|
|
|
* |
83
|
|
|
* @return false|int |
84
|
|
|
*/ |
85
|
|
|
protected function validateES() |
86
|
|
|
{ |
87
|
|
|
return preg_match('/^(?:\+34|0034|34)?[6789]\d{8}$/', $this->value); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Validate France phone number. |
92
|
|
|
* |
93
|
|
|
* @return false|int |
94
|
|
|
*/ |
95
|
|
|
protected function validateFR() |
96
|
|
|
{ |
97
|
|
|
return preg_match('/^(?:\+33|0033|0)(?:(?:[1-9](?:\d{2}){4})|(?:[67]\d{8}))$/', $this->value); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Validate India phone number. |
102
|
|
|
* |
103
|
|
|
* @return false|int |
104
|
|
|
*/ |
105
|
|
|
protected function validateIN() |
106
|
|
|
{ |
107
|
|
|
return preg_match('/^(?:(?:\+|0{0,2})91(\s|-)?)?[6789]\d{9}$/', $this->value); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Validate Indonesia phone number. |
112
|
|
|
* |
113
|
|
|
* @return false|int |
114
|
|
|
*/ |
115
|
|
|
protected function validateID() |
116
|
|
|
{ |
117
|
|
|
return preg_match('/^(?:\+62|0)(?:\d{2,3}\s?){1,2}\d{4,8}$/', $this->value); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Call the phone validator method for each country code and return the results. |
122
|
|
|
* |
123
|
|
|
* @return array An array of validation results, where each key is a country code and the value is either `true` or `false`. |
124
|
|
|
* |
125
|
|
|
* @throws \BadMethodCallException if the validator method for a country code does not exist. |
126
|
|
|
*/ |
127
|
|
|
public function callPhoneValidator(): array |
128
|
|
|
{ |
129
|
|
|
$results = []; |
130
|
|
|
$codes = explode(',', $this->code); |
131
|
|
|
$codes = array_map('strtoupper', $codes); |
132
|
|
|
|
133
|
|
|
foreach ($codes as $code) { |
134
|
|
|
$methodName = 'validate'.$code; |
135
|
|
|
|
136
|
|
|
if (method_exists($this, $methodName)) { |
137
|
|
|
$results[$code] = $this->{$methodName}(); |
138
|
|
|
} else { |
139
|
|
|
throw new \BadMethodCallException("Validator method '{$methodName}' does not exist."); |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
return $results; |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|