Completed
Push — master ( 5c13d8...08a414 )
by Tyler
02:01
created

Telephone::getType()   A

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
30
    //Telephone types
31
    protected $typeMap = [
32
        Type::FIXED_LINE => 'FIXED_LINE',
33
        Type::MOBILE => 'MOBILE',
34
        Type::FIXED_LINE_OR_MOBILE => 'FIXED_LINE_OR_MOBILE',
35
        Type::TOLL_FREE => 'TOLL_FREE',
36
        Type::PREMIUM_RATE => 'PREMIUM_RATE',
37
        Type::SHARED_COST => 'SHARED_COST',
38
        Type::VOIP => 'VOIP',
39
        Type::PERSONAL_NUMBER => 'PERSONAL_NUMBER',
40
        Type::PAGER => 'PAGER',
41
        Type::UAN => 'UAN',
42
        Type::UNKNOWN => 'UNKNOWN',
43
        Type::EMERGENCY => 'EMERGENCY',
44
        Type::VOICEMAIL => 'VOICEMAIL',
45
        Type::SHORT_CODE => 'SHORT_CODE',
46
        Type::STANDARD_RATE => 'STANDARD_RATE',
47
    ];
48
49
    /**
50
     * @param string $input  The phone number
51
     * @param string $region The region or country of the phone number
52
     */
53 6
    public function __construct($input,$region = 'US')
54
    {
55 6
        $this->input = $input;
56 6
        $this->region = $region;
57 6
        $this->setup();
58 6
    }
59
60
    /**
61
     * @return void
62
     */
63 6
    protected function setup()
64
    {
65 6
        $util = Util::getInstance();
66 6
        $number = $util->parse($this->input, $this->region);
67
        
68
        //Meta
69 6
        $this->valid  = $util->isValidNumber($number);
0 ignored issues
show
Bug introduced by
The property valid does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
70 6
        $this->type   = $this->typeMap[$util->getNumberType($number)];
0 ignored issues
show
Bug introduced by
The property type does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
71
72
        //Formats
73 6
        $this->format_E164          = $util->format($number, Format::E164);
74 6
        $this->format_international = $util->format($number, Format::INTERNATIONAL);
75 6
        $this->format_national      = $util->format($number, Format::NATIONAL);
76 6
        $this->format_RFC3966       = $util->format($number, Format::RFC3966);
77
        
78
        //Get the parts
79 6
        $this->country_code    = $number->getCountryCode();
80 6
        $this->extension       = $number->getExtension();
81 6
        $this->area_code       = $this->extractAreaCode();
82 6
        $this->subscriber_code = $this->extractSubscriberCode();
83 6
    }
84
85 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...
86
    {
87 6
        if($this->region === 'US' && $this->isValid()){
88 6
            $exp = '/^\+?(1)?\s?\(?(\d{3})\)?(?:\s|\.|\-)?(\d{3})(?:\s|\.|\-)?(\d{4})$/';
89 6
            preg_match($exp,$this->format_E164,$matches);
90 6
            if(count($matches) === 5){
91 6
                return $matches[2];
92
            }
93
        }
94
95
        return null;
96
    }
97
98 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...
99
    {
100 6
        if($this->region === 'US' && $this->isValid()){
101 6
            $exp = '/^\+?(1)?\s?\(?(\d{3})\)?(?:\s|\.|\-)?(\d{3})(?:\s|\.|\-)?(\d{4})$/';
102 6
            preg_match($exp,$this->format_E164,$matches);
103 6
            if(count($matches) === 5){
104 6
                return $matches[3] . $matches[4];
105
            }
106
        }
107
108
        return null;
109
    }
110
111
    /*=====================================
112
    =            Ouput Methods            =
113
    =====================================*/
114
115
    /**
116
     * @return string
117
     */
118 3
    public function getRFC3966()
119
    {
120 3
        return $this->format_RFC3966;
121
    }
122
123
    /**
124
     * @return string
125
     */
126 3
    public function getNational()
127
    {
128 3
        return $this->format_national;
129
    }
130
131
    /**
132
     * @return string
133
     */
134 3
    public function getInternational()
135
    {
136 3
        return $this->format_international;
137
    }
138
139
    /**
140
     * @return string
141
     */
142 3
    public function getE164()
143
    {
144 3
        return $this->format_E164;
145
    }
146
147
148
    /**
149
     * @return string
150
     */
151 3
    public function getOriginal()
152
    {
153 3
        return $this->input;
154
    }
155
156
    /**
157
     * @return string
158
     */
159 3
    public function getAreaCode()
160
    {
161 3
        return $this->area_code;
162
    }
163
164
    /**
165
     * @return string
166
     */
167 3
    public function getCountryCode()
168
    {
169 3
        return $this->country_code;
170
    }
171
172
    /**
173
     * @return string
174
     */
175 3
    public function getSubscriberCode()
176
    {
177 3
        return $this->subscriber_code;
178
    }
179
180
    /**
181
     * @return string
182
     */
183 3
    public function getType()
184
    {
185 3
        return $this->type;
186
    }
187
188
    /**
189
     * @return string
190
     */
191 6
    public function isValid()
192
    {
193 6
        return $this->valid;
194
    }
195
196
    /**
197
     * @return string
198
     */
199 3
    public function __toString()
200
    {
201 3
        return $this->getE164();
202
    }
203
    
204
    /*=====  End of Ouput Methods  ======*/
205
}