Completed
Push — dev-7.7.1 ( 5e6dff )
by Joshua
14:52
created

NumberFormat::getPattern()   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 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
ccs 2
cts 2
cp 1
crap 1
1
<?php
2
3
namespace libphonenumber;
4
5
/**
6
 * Number Format
7
 */
8
class NumberFormat
9
{
10
    protected $pattern = null;
11
    protected $format = null;
12
    protected $leadingDigitsPattern = array();
13
    protected $nationalPrefixFormattingRule = null;
14
    /**
15
     * @var bool
16
     */
17
    protected $nationalPrefixOptionalWhenFormatting = false;
18
    protected $domesticCarrierCodeFormattingRule = null;
19
20 129
    public function __construct()
21
    {
22
        $this->clear();
23 129
    }
24
25
    /**
26
     * @return NumberFormat
27
     */
28 129
    public function clear()
29
    {
30 129
        $this->pattern = "";
31 129
        $this->format = "";
32 129
        $this->leadingDigitsPattern = array();
33 129
        $this->nationalPrefixFormattingRule = "";
34 129
        $this->nationalPrefixOptionalWhenFormatting = false;
35 129
        $this->domesticCarrierCodeFormattingRule = "";
36
37 129
        return $this;
38
    }
39
40
    /**
41
     * @return boolean
42
     */
43 8
    public function hasPattern()
44
    {
45 8
        return isset($this->pattern);
46
    }
47
48
    /**
49
     * @return string
50
     */
51 10
    public function getPattern()
52
    {
53 10
        return $this->pattern;
54
    }
55
56
    /**
57
     * @param string $value
58
     * @return NumberFormat
59
     */
60 133
    public function setPattern($value)
61
    {
62 133
        $this->pattern = $value;
63
64 133
        return $this;
65
    }
66
67
    /**
68
     * @return boolean
69
     */
70 7
    public function isNationalPrefixOptionalWhenFormatting()
71
    {
72 7
        return $this->nationalPrefixOptionalWhenFormatting;
73
    }
74
75
    /**
76
     * @param boolean $nationalPrefixOptionalWhenFormatting
77
     */
78 126
    public function setNationalPrefixOptionalWhenFormatting($nationalPrefixOptionalWhenFormatting)
79
    {
80 126
        $this->nationalPrefixOptionalWhenFormatting = $nationalPrefixOptionalWhenFormatting;
81 126
    }
82
83
    /**
84
     * @return boolean
85
     */
86 3
    public function hasFormat()
87
    {
88 3
        return ($this->format);
89
    }
90
91
    /**
92
     * @return string
93
     */
94 32
    public function getFormat()
95
    {
96 32
        return $this->format;
97
    }
98
99
    /**
100
     * @param string $value
101
     * @return NumberFormat
102
     */
103 128
    public function setFormat($value)
104
    {
105 128
        $this->format = $value;
106
107 128
        return $this;
108
    }
109
110
    /**
111
     * @return string
112
     */
113
    public function leadingDigitPatterns()
114
    {
115
        return $this->leadingDigitsPattern;
116
    }
117
118
    /**
119
     * @return int
120
     */
121
    public function leadingDigitsPatternSize()
122
    {
123
        return count($this->leadingDigitsPattern);
124
    }
125
126
    /**
127
     * @param int $index
128
     * @return string
129
     */
130 11
    public function getLeadingDigitsPattern($index)
131
    {
132 11
        return $this->leadingDigitsPattern[$index];
133
    }
134
135
    /**
136
     * @param string $value
137
     * @return NumberFormat
138
     */
139 16
    public function addLeadingDigitsPattern($value)
140
    {
141 16
        $this->leadingDigitsPattern[] = $value;
142
143 16
        return $this;
144
    }
145
146
    /**
147
     * @return boolean
148
     */
149 8
    public function hasNationalPrefixFormattingRule()
150
    {
151 8
        return isset($this->nationalPrefixFormattingRule);
152
    }
153
154
    /**
155
     * @return string
156
     */
157 27
    public function getNationalPrefixFormattingRule()
158
    {
159 27
        return $this->nationalPrefixFormattingRule;
160
    }
161
162
    /**
163
     * @param string $value
164
     * @return NumberFormat
165
     */
166 127
    public function setNationalPrefixFormattingRule($value)
167
    {
168 127
        $this->nationalPrefixFormattingRule = $value;
169
170 127
        return $this;
171
    }
172
173
    /**
174
     * @return NumberFormat
175
     */
176
    public function clearNationalPrefixFormattingRule()
177
    {
178
        $this->nationalPrefixFormattingRule = null;
179
180
        return $this;
181
    }
182
183
    /**
184
     * @return boolean
185
     */
186 8
    public function hasDomesticCarrierCodeFormattingRule()
187
    {
188 8
        return isset($this->domesticCarrierCodeFormattingRule);
189
    }
190
191
    /**
192
     * @return string
193
     */
194 6
    public function getDomesticCarrierCodeFormattingRule()
195
    {
196 6
        return $this->domesticCarrierCodeFormattingRule;
197
    }
198
199
    /**
200
     * @param string $value
201
     * @return NumberFormat
202
     */
203 127
    public function setDomesticCarrierCodeFormattingRule($value)
204
    {
205 127
        $this->domesticCarrierCodeFormattingRule = $value;
206
207 127
        return $this;
208
    }
209
210
    /**
211
     * @param NumberFormat $other
212
     * @return NumberFormat
213
     */
214 8
    public function mergeFrom(NumberFormat $other)
215
    {
216
        if ($other->hasPattern()) {
217
            $this->setPattern($other->getPattern());
218
        }
219
        if ($other->hasFormat()) {
220
            $this->setFormat($other->getFormat());
221
        }
222
        $leadingDigitsPatternSize = $other->leadingDigitsPatternSize();
223
        for ($i = 0; $i < $leadingDigitsPatternSize; $i++) {
224
            $this->addLeadingDigitsPattern($other->getLeadingDigitsPattern($i));
225 6
        }
226
        if ($other->hasNationalPrefixFormattingRule()) {
227
            $this->setNationalPrefixFormattingRule($other->getNationalPrefixFormattingRule());
228
        }
229
        if ($other->hasDomesticCarrierCodeFormattingRule()) {
230
            $this->setDomesticCarrierCodeFormattingRule($other->getDomesticCarrierCodeFormattingRule());
231
        }
232
        $this->setNationalPrefixOptionalWhenFormatting($other->isNationalPrefixOptionalWhenFormatting());
233
234 8
        return $this;
235 8
    }
236
237
    /**
238
     * @return array
239
     */
240
    public function toArray()
241
    {
242
        $output = array();
243
        $output['pattern'] = $this->getPattern();
244
        $output['format'] = $this->getFormat();
245
246
        $output['leadingDigitsPatterns'] = $this->leadingDigitPatterns();
247
248
        if ($this->hasNationalPrefixFormattingRule()) {
249
            $output['nationalPrefixFormattingRule'] = $this->getNationalPrefixFormattingRule();
250
        }
251
252
        if ($this->hasDomesticCarrierCodeFormattingRule()) {
253
            $output['domesticCarrierCodeFormattingRule'] = $this->getDomesticCarrierCodeFormattingRule();
254
        }
255
256
        $output['nationalPrefixOptionalWhenFormatting'] = $this->isNationalPrefixOptionalWhenFormatting();
257
258
        return $output;
259
    }
260
261
    /**
262
     * @param array $input
263
     */
264 125
    public function fromArray(array $input)
265
    {
266
        $this->setPattern($input['pattern']);
267
        $this->setFormat($input['format']);
268 125
        foreach ($input['leadingDigitsPatterns'] as $leadingDigitsPattern) {
269
            $this->addLeadingDigitsPattern($leadingDigitsPattern);
270 110
        }
271
272 125
        if (isset($input['nationalPrefixFormattingRule'])) {
273
            $this->setNationalPrefixFormattingRule($input['nationalPrefixFormattingRule']);
274
        }
275 125
        if (isset($input['domesticCarrierCodeFormattingRule'])) {
276
            $this->setDomesticCarrierCodeFormattingRule($input['domesticCarrierCodeFormattingRule']);
277
        }
278
279
        $this->setNationalPrefixOptionalWhenFormatting($input['nationalPrefixOptionalWhenFormatting']);
280
    }
281
}
282