Passed
Push — 1.x ( 938550...cada6e )
by Milwad
03:42 queued 14s
created

CountryPhoneCallback::addValidator()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 2
b 0
f 0
nc 2
nop 2
dl 0
loc 7
rs 10
1
<?php
2
3
namespace Milwad\LaravelValidate\Utils;
4
5
use Milwad\LaravelValidate\Utils\CountryPhoneValidator\CountryPhoneValidator;
6
use RuntimeException;
7
8
class CountryPhoneCallback
9
{
10
    /**
11
     * Country Validate classes.
12
     */
13
    protected static array $validators = [];
14
15
    /**
16
     * Add new country validator.
17
     *
18
     * @throws \Throwable
19
     */
20
    public static function addValidator(string $code, string $validator): void
21
    {
22
        if (! new $validator instanceof CountryPhoneValidator) {
23
            throw new RuntimeException('The validator is not instance of CountryPhoneValidator');
24
        }
25
26
        self::$validators[$code] = $validator;
27
    }
28
29
    /**
30
     * Call country validate class.
31
     */
32
    public static function callPhoneValidator(string $code, $value)
33
    {
34
        if (isset(self::$validators[$code])) {
35
            return (new self::$validators[$code])->validate($value);
36
        } else {
37
            throw new \BadMethodCallException("Validator method for '$code' does not exist.");
38
        }
39
    }
40
}
41