Issues (88)

src/Utils/CountryPhoneCallback.php (2 issues)

1
<?php
2
3
/**
4
 * This file is part of Dimtrovich/Validation.
5
 *
6
 * (c) 2023 Dimitri Sitchet Tomkeu <[email protected]>
7
 *
8
 * For the full copyright and license information, please view
9
 * the LICENSE file that was distributed with this source code.
10
 */
11
12
namespace Dimtrovich\Validation\Utils;
13
14
use BadMethodCallException;
15
16
/**
17
 * @credit <a href="https://github.com/milwad-dev/laravel-validate">milwad/laravel-validate - Milwad\LaravelValidate\Utils\CountryPhoneCallback</a>
18
 */
19
class CountryPhoneCallback
20
{
21
    /**
22
     * Create a new phone validator instance.
23
     *
24
     * @param mixed  $value The phone number to validate.
25
     * @param string $code  The country codes to validate against. String can be separated by comma
26
     */
27
    public function __construct(protected $value, protected string $code)
28
    {
29
    }
30
31
    // TODO: Add a feature to add validate method for your own country!
32
33
    /**
34
     * Validate Iran phone number.
35
     *
36
     * @return false|int
37
     */
38
    protected function validateIR()
39
    {
40
        return preg_match('/^(\+98|0)?9\d{9}$/', $this->value);
41
    }
42
43
    /**
44
     * Validate Iran phone number.
45
     *
46
     * @return false|int
47
     */
48
    protected function validateEN()
49
    {
50
        return preg_match('/^(?:\+44|0)7\d{9}$/', $this->value);
51
    }
52
53
    protected function validateTG()
54
    {
55
        return preg_match('/^(\+228|00228|228)?\d{8}$/', $this->value);
56
    }
57
58
    /**
59
     * Validate Nigeria phone number.
60
     *
61
     * @return false|int
62
     */
63
    protected function validateNE()
64
    {
65
        return preg_match('/^(\+227|00227|227)?\d{8}$/', $this->value);
66
    }
67
68
    /**
69
     * Validate Saudi Arabia phone number.
70
     *
71
     * @return false|int
72
     */
73
    protected function validateSA()
74
    {
75
        return preg_match('/^((\+966)|0)?5\d{8}$/', $this->value);
76
    }
77
78
    /**
79
     * Validate Germany phone number.
80
     *
81
     * @return false|int
82
     */
83
    protected function validateDE()
84
    {
85
        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);
86
    }
87
88
    /**
89
     * Validate Greece phone number.
90
     *
91
     * @return false|int
92
     */
93
    protected function validateGR()
94
    {
95
        return preg_match('/^\+30[2-9]\d{2}\d{3}\d{4}$/', $this->value);
96
    }
97
98
    /**
99
     * Validate Spain phone number.
100
     *
101
     * @return false|int
102
     */
103
    protected function validateES()
104
    {
105
        return preg_match('/^(?:\+34|0034|34)?[6789]\d{8}$/', $this->value);
106
    }
107
108
    protected function validateGW()
109
    {
110
        return preg_match('/^(?:\+245|00245|245)?\d{7,8}$/', $this->value);
111
    }
112
113
    protected function validateTD()
114
    {
115
        return preg_match('/^(?:\+235|00235|235)?\d{8}$/', $this->value);
116
    }
117
118
    protected function validateCM()
119
    {
120
        return
121
            preg_match('/^(?:\+237\s?|00237\s?|237\s?)?6\s?[5-9]{1}[0-9]{1}[-. ]?([0-9]{2}[-. ]?){3}$/', $this->value) // Orange, MTN, Nextel
122
            || preg_match('/^(?:\+237\s?|00237\s?|237\s?)?(2|3|4)\s?[2-3]{1}[0-9]{1}[-. ]?([0-9]{2}[-. ]?){3}$/', $this->value); // Camtel
123
    }
124
125
    /**
126
     * Validate France phone number.
127
     *
128
     * @return false|int
129
     */
130
    protected function validateFR()
131
    {
132
        return preg_match('/^(?:\+33|0033|0)(?:(?:[1-9](?:\d{2}){4})|(?:[67]\d{8}))$/', $this->value);
133
    }
134
135
    /**
136
     * Validate India phone number.
137
     *
138
     * @return false|int
139
     */
140
    protected function validateIN()
141
    {
142
        return preg_match('/^(?:(?:\+|0{0,2})91(\s|-)?)?[6789]\d{9}$/', $this->value);
143
    }
144
145
    /**
146
     * Validate Indonesia phone number.
147
     *
148
     * @return false|int
149
     */
150
    protected function validateID()
151
    {
152
        return preg_match('/^(?:\+62|0)(?:\d{2,3}\s?){1,2}\d{4,8}$/', $this->value);
153
    }
154
155
    /**
156
     * Validate Italy phone number.
157
     *
158
     * @return false|int
159
     */
160
    protected function validateIT()
161
    {
162
        return preg_match('/^\+39\d{8,10}$/', $this->value);
163
    }
164
165
    /**
166
     * Validate Japanese phone number.
167
     *
168
     * @return false|int
169
     */
170
    protected function validateJA()
171
    {
172
        return preg_match('/(\d{2,3})-?(\d{3,4})-?(\d{4})/', $this->value);
173
    }
174
175
    /**
176
     * Validate Korean phone number.
177
     *
178
     * @return false|int
179
     */
180
    protected function validateKO()
181
    {
182
        return preg_match('/^(?:\+82|0)(?:10|1[1-9])-?\d{3,4}-?\d{4}$/', $this->value);
183
    }
184
185
    /**
186
     * Validate Russian phone number.
187
     *
188
     * @return false|int
189
     */
190
    protected function validateRU()
191
    {
192
        return preg_match('/^(?:\+7|8)(?:\s?\(?\d{3}\)?\s?\d{3}(?:-?\d{2}){2}|\s?\d{2}(?:\s?\d{2}){3})$/', $this->value);
193
    }
194
195
    /**
196
     * Validate Sweden phone number.
197
     *
198
     * @return false|int
199
     */
200
    protected function validateSE()
201
    {
202
        return preg_match('/^(?:\+46|0) ?(?:[1-9]\d{1,2}-?\d{2}(?:\s?\d{2}){2}|7\d{2}-?\d{2}(?:\s?\d{2}){2})$/', $this->value);
203
    }
204
205
    /**
206
     * Validate Turkey phone number.
207
     *
208
     * @return false|int
209
     */
210
    protected function validateTR()
211
    {
212
        return preg_match('/^(?:\+90|0)(?:\s?[1-9]\d{2}\s?\d{3}\s?\d{2}\s?\d{2}|[1-9]\d{2}-?\d{3}-?\d{2}-?\d{2})$/', $this->value);
213
    }
214
215
    /**
216
     * Validate Chinese phone number.
217
     *
218
     * @return false|int
219
     */
220
    protected function validateZH()
221
    {
222
        return preg_match('/^(?:\+86)?1[3-9]\d{9}$/', $this->value);
223
    }
224
225
    protected function validateBF()
226
    {
227
        return preg_match('/^(\+226|00226|226)?\d{8}$/', $this->value);
228
    }
229
230
    protected function validateAO(): bool
231
    {
232
        return preg_match('/^(\+244|00244|244)?[9,2][1-9]\d{7}$/', $this->value);
0 ignored issues
show
Bug Best Practice introduced by
The expression return preg_match('/^(\+...\d{7}$/', $this->value) returns the type integer which is incompatible with the type-hinted return boolean.
Loading history...
233
    }
234
235
    protected function validateBJ(): bool
236
    {
237
        return preg_match('/^(\+229|00229|229)?\d{8}$/', $this->value);
0 ignored issues
show
Bug Best Practice introduced by
The expression return preg_match('/^(\+...\d{8}$/', $this->value) returns the type integer which is incompatible with the type-hinted return boolean.
Loading history...
238
    }
239
240
    /**
241
     * Call the phone validator method for each country code and return the results.
242
     *
243
     * @return array An array of validation results, where each key is a country code and the value is either `true` or `false`.
244
     *
245
     * @throws BadMethodCallException if the validator method for a country code does not exist.
246
     */
247
    public function callPhoneValidator(): array
248
    {
249
        $results = [];
250
251
        $codes = explode(',', $this->code);
252
253
        $codes = array_map('strtoupper', $codes);
254
255
        foreach ($codes as $code) {
256
            $methodName = 'validate' . $code;
257
258
            if (method_exists($this, $methodName)) {
259
                $results[$code] = $this->{$methodName}();
260
            } else {
261
                throw new BadMethodCallException("Validator method '{$methodName}' does not exist.");
262
            }
263
        }
264
265
        return $results;
266
    }
267
}
268