LibPhoneNumberValidator   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 120
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 120
ccs 24
cts 24
cp 1
rs 10
c 0
b 0
f 0
wmc 11

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getCarrierName() 0 3 1
A make() 0 9 2
A getNumber() 0 3 1
A isValidMobile() 0 3 1
A isValidForCountry() 0 9 3
A getInstance() 0 3 1
A isValidFixedLine() 0 3 1
A __construct() 0 8 1
1
<?php
2
3
namespace App;
4
5
use Exception;
6
use libphonenumber\PhoneNumber;
7
use libphonenumber\PhoneNumberType;
8
use libphonenumber\PhoneNumberUtil;
9
use libphonenumber\PhoneNumberFormat;
10
use libphonenumber\PhoneNumberToCarrierMapper;
11
12
class LibPhoneNumberValidator implements PhoneNumberValidator
13
{
14
    /**
15
     * @var libphonenumber\PhoneNumberUtil
0 ignored issues
show
Bug introduced by
The type App\libphonenumber\PhoneNumberUtil was not found. Did you mean libphonenumber\PhoneNumberUtil? If so, make sure to prefix the type with \.
Loading history...
16
     */
17
    protected $util;
18
19
    /**
20
     * @var libphonenumber\PhoneNumberToCarrierMapper
0 ignored issues
show
Bug introduced by
The type App\libphonenumber\PhoneNumberToCarrierMapper was not found. Did you mean libphonenumber\PhoneNumberToCarrierMapper? If so, make sure to prefix the type with \.
Loading history...
21
     */
22
    protected $carrierMapper;
23
24
    /**
25
     * Number to validate.
26
     * @var libphonenumber\PhoneNumber
0 ignored issues
show
Bug introduced by
The type App\libphonenumber\PhoneNumber was not found. Did you mean libphonenumber\PhoneNumber? If so, make sure to prefix the type with \.
Loading history...
27
     */
28
    protected $number;
29
30
    /**
31
     * @var bool
32
     */
33
    protected $isValid = false;
34
35
    /**
36
     * Instantiate the class and get an instance
37
     * of the libphonenumber util.
38
     */
39 20
    public function __construct(PhoneNumberUtil $util, PhoneNumberToCarrierMapper $carrierMapper)
40
    {
41 20
        $this->util = $util;
0 ignored issues
show
Documentation Bug introduced by
It seems like $util of type libphonenumber\PhoneNumberUtil is incompatible with the declared type App\libphonenumber\PhoneNumberUtil of property $util.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
42 20
        $this->carrierMapper = $carrierMapper;
0 ignored issues
show
Documentation Bug introduced by
It seems like $carrierMapper of type libphonenumber\PhoneNumberToCarrierMapper is incompatible with the declared type App\libphonenumber\PhoneNumberToCarrierMapper of property $carrierMapper.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
43
44
        // Default the phone number to prevent exceptions and allow control flow
45
        // to be handled by the developer with the 'isValid...' methods
46 20
        $this->number = new PhoneNumber;
0 ignored issues
show
Documentation Bug introduced by
It seems like new libphonenumber\PhoneNumber() of type libphonenumber\PhoneNumber is incompatible with the declared type App\libphonenumber\PhoneNumber of property $number.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
47 20
    }
48
49
    /**
50
     * Validate the given number.
51
     *
52
     * @param  mixed        $number
53
     * @param  null|string  $countryCode
54
     * @return App\PhoneNumberValidator
0 ignored issues
show
Bug introduced by
The type App\App\PhoneNumberValidator was not found. Did you mean App\PhoneNumberValidator? If so, make sure to prefix the type with \.
Loading history...
55
     */
56 19
    public function make($number, string $countryCode = null): PhoneNumberValidator
57
    {
58
        try {
59 19
            $this->number = $this->util->parse($number, $countryCode);
60 1
        } catch (Exception $e) {
61
            // Let the 'isValid' methods handle the control-flow.
62
        }
63
64 19
        return $this;
65
    }
66
67
    /**
68
     * Is the number valid for the given country code.
69
     *
70
     * @param  mixed  $countryCode
71
     * @return bool
72
     */
73 7
    public function isValidForCountry($countryCode): bool
74
    {
75 7
        foreach (array_wrap($countryCode) as $code) {
76 7
            if ($this->util->isValidNumberForRegion($this->number, mb_strtoupper($code))) {
77 7
                return true;
78
            }
79
        }
80
81 2
        return false;
82
    }
83
84
    /**
85
     * Is the number a valid mobile.
86
     *
87
     * @return bool
88
     */
89 7
    public function isValidMobile(): bool
90
    {
91 7
        return $this->util->getNumberType($this->number) === PhoneNumberType::MOBILE;
92
    }
93
94
    /**
95
     * Is the number a valid landline/fixed-line.
96
     *
97
     * @return bool
98
     */
99 2
    public function isValidFixedLine(): bool
100
    {
101 2
        return $this->util->getNumberType($this->number) === PhoneNumberType::FIXED_LINE;
102
    }
103
104
    /**
105
     * Return the carrier name.
106
     *
107
     * @return string
108
     */
109 7
    public function getCarrierName(): string
110
    {
111 7
        return $this->carrierMapper->getNameForNumber($this->number, 'en');
112
    }
113
114
    /**
115
     * Return the phone number.
116
     *
117
     * @return string
118
     */
119 6
    public function getNumber(): string
120
    {
121 6
        return $this->util->format($this->number, PhoneNumberFormat::INTERNATIONAL);
122
    }
123
124
    /**
125
     * Get a new instance of the validator.
126
     *
127
     * @param libphonenumber\PhoneNumberToCarrierMapper $mapper
128
     */
129 20
    public static function getInstance()
130
    {
131 20
        return new static(PhoneNumberUtil::getInstance(), PhoneNumberToCarrierMapper::getInstance());
132
    }
133
}
134