IsTelephone   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 15
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
c 1
b 0
f 0
dl 0
loc 15
ccs 0
cts 11
cp 0
rs 10
wmc 3

1 Method

Rating   Name   Duplication   Size   Complexity  
A validate() 0 11 3
1
<?php
2
3
namespace kalanis\kw_rules\Rules\External;
4
5
6
use kalanis\kw_rules\Interfaces\IValidate;
7
use kalanis\kw_rules\Exceptions\RuleException;
8
use kalanis\kw_rules\Rules\ARule;
9
use kalanis\kw_rules\Rules\TCheckString;
10
use libphonenumber\NumberParseException;
11
use libphonenumber\PhoneNumberUtil;
12
13
14
/**
15
 * Class IsTelephone
16
 * @package kalanis\kw_rules\Rules\External
17
 * Check if input is telephone for preset country
18
 * @link https://github.com/giggsey/libphonenumber-for-php
19
 * @codeCoverageIgnore need external libraries
20
 */
21
class IsTelephone extends ARule
22
{
23
    use TCheckString;
24
25
    public function validate(IValidate $entry): void
26
    {
27
        $phoneUtil = PhoneNumberUtil::getInstance();
28
        try {
29
            $number = $phoneUtil->parse($entry->getValue(), $this->againstValue);
30
            if ($phoneUtil->isValidNumber($number)) {
31
                return;
32
            }
33
            throw new RuleException($this->errorText);
34
        } catch (NumberParseException $e) {
35
            throw new RuleException($this->errorText, 0, $e);
36
        }
37
    }
38
}
39