Passed
Push — 1.x ( 625e1b...6eea3f )
by Milwad
06:03 queued 02:50
created

CountryPhoneCallback::validateGW()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
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
     * Validate Italy phone number.
122
     *
123
     * @return false|int
124
     */
125
    protected function validateIT()
126
    {
127
        return preg_match('/^\+39\d{8,10}$/', $this->value);
128
    }
129
130
    /**
131
     * Validate Japanese phone number.
132
     *
133
     * @return false|int
134
     */
135
    protected function validateJA()
136
    {
137
        return preg_match('/(\d{2,3})-?(\d{3,4})-?(\d{4})/', $this->value);
138
    }
139
140
    /**
141
     * Validate Korean phone number.
142
     *
143
     * @return false|int
144
     */
145
    protected function validateKO()
146
    {
147
        return preg_match('/^(?:\+82|0)(?:10|1[1-9])-?\d{3,4}-?\d{4}$/', $this->value);
148
    }
149
150
    /**
151
     * Validate Russian phone number.
152
     *
153
     * @return false|int
154
     */
155
    protected function validateRU()
156
    {
157
        return preg_match('/^(?:\+7|8)(?:\s?\(?\d{3}\)?\s?\d{3}(?:-?\d{2}){2}|\s?\d{2}(?:\s?\d{2}){3})$/', $this->value);
158
    }
159
160
    /**
161
     * Validate Sweden phone number.
162
     *
163
     * @return false|int
164
     */
165
    protected function validateSE()
166
    {
167
        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);
168
    }
169
170
    /**
171
     * Validate Turkey phone number.
172
     *
173
     * @return false|int
174
     */
175
    protected function validateTR()
176
    {
177
        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);
178
    }
179
180
    /**
181
     * Validate Chinese phone number.
182
     *
183
     * @return false|int
184
     */
185
    protected function validateZH()
186
    {
187
        return preg_match('/^(?:\+86)?1[3-9]\d{9}$/', $this->value);
188
    }
189
190
    /**
191
     * Call the phone validator method for each country code and return the results.
192
     *
193
     * @return array An array of validation results, where each key is a country code and the value is either `true` or `false`.
194
     *
195
     * @throws \BadMethodCallException if the validator method for a country code does not exist.
196
     */
197
    public function callPhoneValidator(): array
198
    {
199
        $results = [];
200
        $codes = explode(',', $this->code);
201
        $codes = array_map('strtoupper', $codes);
202
203
        foreach ($codes as $code) {
204
            $methodName = 'validate'.$code;
205
206
            if (method_exists($this, $methodName)) {
207
                $results[$code] = $this->{$methodName}();
208
            } else {
209
                throw new \BadMethodCallException("Validator method '{$methodName}' does not exist.");
210
            }
211
        }
212
213
        return $results;
214
    }
215
}
216