IsTelephone::validate()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 8
c 1
b 0
f 0
nc 4
nop 1
dl 0
loc 11
ccs 0
cts 11
cp 0
crap 12
rs 10
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