Passed
Push — master ( 1fe033...85608f )
by Joshua
08:13
created

NumberFormat::toArray()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 21
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4

Importance

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