Passed
Push — 1.x ( ced68d...fcb885 )
by Milwad
01:14 queued 14s
created

CountryLandlineCallback::callLandlineValidator()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 6
rs 10
1
<?php
2
3
namespace Milwad\LaravelValidate\Utils;
4
5
use Milwad\LaravelValidate\Utils\CountryLandlineValidator\CountryLandlineValidator;
6
use RuntimeException;
7
8
class CountryLandlineCallback
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 CountryLandlineValidator) {
23
            throw new RuntimeException('The validator is not instance of CountryLandlineValidator');
24
        }
25
26
        self::$validators[$code] = $validator;
27
    }
28
29
    /**
30
     * Call country validate class.
31
     */
32
    public static function callLandlineValidator(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