Passed
Push — upstream-8.9.2 ( 6a22e8 )
by Joshua
25:34 queued 09:29
created

NumberFormat::toArray()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 21
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 4

Importance

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