Passed
Push — 1.x ( 3587d8...eb5eae )
by Milwad
01:08 queued 13s
created

CountryPhoneCallback::validateBJ()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
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
18
19
20
    protected function validateBJ(): bool
21
    {
22
        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...
23
    }
24
25
    /**
26
     * Call the phone validator method for each country code and return the results.
27
     *
28
     * @throws \BadMethodCallException if the validator method for a country code does not exist.
29
     *
30
     * @return array An array of validation results, where each key is a country code and the value is either `true` or `false`.
31
     */
32
    public function callPhoneValidator(): array
33
    {
34
        $results = [];
35
36
        $codes = explode(',', $this->code);
37
38
        $codes = array_map('strtoupper', $codes);
39
40
        foreach ($codes as $code) {
41
            $methodName = 'validate' . $code;
42
43
            if (method_exists($this, $methodName)) {
44
                $results[$code] = $this->{$methodName}();
45
            } else {
46
                throw new \BadMethodCallException("Validator method '{$methodName}' does not exist.");
47
            }
48
        }
49
50
        return $results;
51
    }
52
}
53