Completed
Push — master ( 5117ae...fac286 )
by Joshua
27:26 queued 11:48
created

NumberFormat::setFormat()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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