Telephone::isValid()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Zenapply\DataTypes;
4
5
use libphonenumber\PhoneNumberUtil as Util;
6
use libphonenumber\PhoneNumberFormat as Format;
7
use libphonenumber\PhoneNumberType as Type;
8
9
/**
10
 * @see https://en.wikipedia.org/wiki/Telephone_numbering_plan#Structure
11
 */
12
class Telephone 
13
{
14
    //Telephone variations
15
    protected $format_E164;
16
    protected $format_international;
17
    protected $format_national;
18
    protected $format_RFC3966;
19
20
    //Telephone structure
21
    protected $country_code;
22
    protected $area_code;
23
    protected $subscriber_code;
24
    protected $extension;
25
26
    //Telephone extras
27
    protected $region;
28
    protected $input;
29
    protected $valid;
30
    protected $type;
31
32
    //Telephone types
33
    protected $typeMap = [
34
        Type::FIXED_LINE => 'FIXED_LINE',
35
        Type::MOBILE => 'MOBILE',
36
        Type::FIXED_LINE_OR_MOBILE => 'FIXED_LINE_OR_MOBILE',
37
        Type::TOLL_FREE => 'TOLL_FREE',
38
        Type::PREMIUM_RATE => 'PREMIUM_RATE',
39
        Type::SHARED_COST => 'SHARED_COST',
40
        Type::VOIP => 'VOIP',
41
        Type::PERSONAL_NUMBER => 'PERSONAL_NUMBER',
42
        Type::PAGER => 'PAGER',
43
        Type::UAN => 'UAN',
44
        Type::UNKNOWN => 'UNKNOWN',
45
        Type::EMERGENCY => 'EMERGENCY',
46
        Type::VOICEMAIL => 'VOICEMAIL',
47
        Type::SHORT_CODE => 'SHORT_CODE',
48
        Type::STANDARD_RATE => 'STANDARD_RATE',
49
    ];
50
51
    /**
52
     * @param string $input  The phone number
53
     * @param string $region The region or country of the phone number
54
     */
55 6
    public function __construct($input,$region = 'US')
56
    {
57 6
        $this->input = $input;
58 6
        $this->region = $region;
59 6
        $this->setup();
60 6
    }
61
62
    /**
63
     * @return void
64
     */
65 6
    protected function setup()
66
    {
67 6
        $util = Util::getInstance();
68 6
        $number = $util->parse($this->input, $this->region);
69
        
70
        //Meta
71 6
        $this->valid  = $util->isValidNumber($number);
72 6
        $this->type   = $this->typeMap[$util->getNumberType($number)];
73
74
        //Formats
75 6
        $this->format_E164          = $util->format($number, Format::E164);
76 6
        $this->format_international = $util->format($number, Format::INTERNATIONAL);
77 6
        $this->format_national      = $util->format($number, Format::NATIONAL);
78 6
        $this->format_RFC3966       = $util->format($number, Format::RFC3966);
79
        
80
        //Get the parts
81 6
        $this->country_code    = $number->getCountryCode();
82 6
        $this->extension       = $number->getExtension();
83 6
        $this->area_code       = $this->extractAreaCode();
84 6
        $this->subscriber_code = $this->extractSubscriberCode();
85 6
    }
86
87 6 View Code Duplication
    protected function extractAreaCode()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
88
    {
89 6
        if($this->region === 'US' && $this->isValid()){
90 6
            $exp = '/^\+?(1)?\s?\(?(\d{3})\)?(?:\s|\.|\-)?(\d{3})(?:\s|\.|\-)?(\d{4})$/';
91 6
            preg_match($exp,$this->format_E164,$matches);
92 6
            if(count($matches) === 5){
93 6
                return $matches[2];
94
            }
95
        }
96
97
        return null;
98
    }
99
100 6 View Code Duplication
    protected function extractSubscriberCode()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
101
    {
102 6
        if($this->region === 'US' && $this->isValid()){
103 6
            $exp = '/^\+?(1)?\s?\(?(\d{3})\)?(?:\s|\.|\-)?(\d{3})(?:\s|\.|\-)?(\d{4})$/';
104 6
            preg_match($exp,$this->format_E164,$matches);
105 6
            if(count($matches) === 5){
106 6
                return $matches[3] . $matches[4];
107
            }
108
        }
109
110
        return null;
111
    }
112
113
    /*=====================================
114
    =            Ouput Methods            =
115
    =====================================*/
116
117
    /**
118
     * @return string
119
     */
120 3
    public function getRFC3966()
121
    {
122 3
        return $this->format_RFC3966;
123
    }
124
125
    /**
126
     * @return string
127
     */
128 3
    public function getNational()
129
    {
130 3
        return $this->format_national;
131
    }
132
133
    /**
134
     * @return string
135
     */
136 3
    public function getInternational()
137
    {
138 3
        return $this->format_international;
139
    }
140
141
    /**
142
     * @return string
143
     */
144 3
    public function getE164()
145
    {
146 3
        return $this->format_E164;
147
    }
148
149
150
    /**
151
     * @return string
152
     */
153 3
    public function getOriginal()
154
    {
155 3
        return $this->input;
156
    }
157
158
    /**
159
     * @return string
160
     */
161 3
    public function getAreaCode()
162
    {
163 3
        return $this->area_code;
164
    }
165
166
    /**
167
     * @return string
168
     */
169 3
    public function getCountryCode()
170
    {
171 3
        return $this->country_code;
172
    }
173
174
    /**
175
     * @return string
176
     */
177 3
    public function getSubscriberCode()
178
    {
179 3
        return $this->subscriber_code;
180
    }
181
182
    /**
183
     * @return string
184
     */
185 3
    public function getType()
186
    {
187 3
        return $this->type;
188
    }
189
190
    /**
191
     * @return string
192
     */
193 6
    public function isValid()
194
    {
195 6
        return $this->valid;
196
    }
197
198
    /**
199
     * @return string
200
     */
201 3
    public function __toString()
202
    {
203 3
        return $this->getE164();
204
    }
205
    
206
    /*=====  End of Ouput Methods  ======*/
207
}