Passed
Pull Request — 1.x (#50)
by
unknown
02:55
created

CountryPhoneCallback::validateCM()   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(protected $value, protected string $code, $attribute = null)
15
    {
16
    }
17
    protected function validateTG()
18
    {
19
        return preg_match('/^(\+228|00228|228)?\d{8}$/', $this->value);
20
    }
21
    protected function validateNE()
22
    {
23
        return preg_match('/^(\+227|00227|227)?\d{8}$/', $this->value);
24
    }
25
26
    protected function validateGW()
27
    {
28
        return preg_match('/^(\+245|00245|245)?\d{7,8}$/', $this->value);
29
    }
30
    protected function validateTD()
31
    {
32
        return preg_match('/^(\+235|00235|235)?\d{8}$/', $this->value);
33
    }
34
    protected function validateCM()
35
    {
36
        return preg_match('/^(\+237|00237|237)?\d{8}$/', $this->value);
37
    }
38
    protected function validateBF()
39
    {
40
        return preg_match('/^(\+226|00226|226)?\d{8}$/', $this->value);
41
    }
42
    protected function validateAO(): bool
43
    {
44
        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...
45
    }
46
47
    protected function validateBJ(): bool
48
    {
49
        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...
50
    }
51
52
    /**
53
     * Call the phone validator method for each country code and return the results.
54
     *
55
     * @return array An array of validation results, where each key is a country code and the value is either `true` or `false`.
56
     *
57
     * @throws \BadMethodCallException if the validator method for a country code does not exist.
58
     */
59
    public function callPhoneValidator(): array
60
    {
61
        $results = [];
62
63
        $codes = explode(',', $this->code);
64
65
        $codes = array_map('strtoupper', $codes);
66
67
        foreach ($codes as $code) {
68
            $methodName = 'validate' . $code;
69
70
            if (method_exists($this, $methodName)) {
71
                $results[$code] = $this->{$methodName}();
72
            } else {
73
                throw new \BadMethodCallException("Validator method '{$methodName}' does not exist.");
74
            }
75
        }
76
77
        return $results;
78
    }
79
}
80