Completed
Pull Request — master (#94)
by Joshua
82:48 queued 60:07
created

PhoneNumber::clear()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 12
rs 9.4285
ccs 10
cts 10
cp 1
cc 1
eloc 10
nc 1
nop 0
crap 1
1
<?php
2
3
namespace libphonenumber;
4
5
class PhoneNumber implements \Serializable
6
{
7
    /**
8
     * The country code.
9
     *
10
     * @var int|null
11
     */
12
    private $countryCode = null;
13
14
    /**
15
     * Returns whether this phone number has a country code set.
16
     *
17
     * @return bool True if a country code is set, false otherwise.
18
     */
19 1602
    public function hasCountryCode()
20
    {
21 1602
        return isset($this->countryCode);
22
    }
23
24
    /**
25
     * Returns the country code of this phone number.
26
     *
27
     * @return int|null The country code, or null if not set.
28
     */
29 2448
    public function getCountryCode()
30
    {
31 2448
        return $this->countryCode;
32
    }
33
34
    /**
35
     * Sets the country code of this phone number.
36
     *
37
     * @param int $value The country code.
38
     *
39
     * @return PhoneNumber This PhoneNumber instance, for chaining method calls.
40
     */
41 2475
    public function setCountryCode($value)
42
    {
43 2475
        $this->countryCode = (int) $value;
44 2475
        return $this;
45
    }
46
47
    /**
48
     * Clears the country code of this phone number.
49
     *
50
     * @return PhoneNumber This PhoneNumber instance, for chaining method calls.
51
     */
52 12
    public function clearCountryCode()
53
    {
54 12
        $this->countryCode = null;
55 12
        return $this;
56
    }
57
58
    /**
59
     * The national number.
60
     *
61
     * @var string|null
62
     */
63
    private $nationalNumber = null;
64
65
    /**
66
     * Returns whether this phone number has a national number set.
67
     *
68
     * @return bool True if a national number is set, false otherwise.
69
     */
70 16
    public function hasNationalNumber()
71
    {
72 16
        return isset($this->nationalNumber);
73
    }
74
75
    /**
76
     * Returns the country code of this phone number.
77
     *
78
     * @return string|null The national number, or null if not set.
79
     */
80 2257
    public function getNationalNumber()
81
    {
82 2257
        return $this->nationalNumber;
83
    }
84
85
    /**
86
     * Sets the national number of this phone number.
87
     *
88
     * @param string $value The national number.
89
     *
90
     * @return PhoneNumber This PhoneNumber instance, for chaining method calls.
91
     */
92 2473
    public function setNationalNumber($value)
93
    {
94 2473
        $this->nationalNumber = (string) $value;
95 2473
        return $this;
96
    }
97
98
    /**
99
     * Clears the national number of this phone number.
100
     *
101
     * @return PhoneNumber This PhoneNumber instance, for chaining method calls.
102
     */
103 12
    public function clearNationalNumber()
104
    {
105 12
        $this->nationalNumber = null;
106 12
        return $this;
107
    }
108
109
    /**
110
     * The extension.
111
     *
112
     * @var string|null
113
     */
114
    private $extension = null;
115
116
    /**
117
     * Returns whether this phone number has an extension set.
118
     *
119
     * @return bool True if an extension is set, false otherwise.
120
     */
121 1890
    public function hasExtension()
122
    {
123 1890
        return isset($this->extension);
124
    }
125
126
    /**
127
     * Returns the extension of this phone number.
128
     *
129
     * @return string|null The extension, or null if not set.
130
     */
131 6
    public function getExtension()
132
    {
133 6
        return $this->extension;
134
    }
135
136
    /**
137
     * Sets the extension of this phone number.
138
     *
139
     * @param string $value The extension.
140
     *
141
     * @return PhoneNumber This PhoneNumber instance, for chaining method calls.
142
     */
143 8
    public function setExtension($value)
144
    {
145 8
        $this->extension = (string) $value;
146 8
        return $this;
147
    }
148
149
    /**
150
     * Clears the extension of this phone number.
151
     *
152
     * @return PhoneNumber This PhoneNumber instance, for chaining method calls.
153
     */
154 15
    public function clearExtension()
155
    {
156 15
        $this->extension = null;
157 15
        return $this;
158
    }
159
160
    /**
161
     * Whether this phone number uses an italian leading zero.
162
     *
163
     * @var bool|null
164
     */
165
    private $italianLeadingZero = null;
166
167
    /**
168
     * Returns whether this phone number has the italian leading zero information set.
169
     *
170
     * @return bool
171
     */
172 1602
    public function hasItalianLeadingZero()
173
    {
174 1602
        return isset($this->italianLeadingZero);
175
    }
176
177
    /**
178
     * Returns whether this phone number uses an italian leading zero.
179
     *
180
     * @return bool|null True if it uses an italian leading zero, false it it does not, null if not set.
181
     */
182 2247
    public function isItalianLeadingZero()
183
    {
184 2247
        return $this->italianLeadingZero;
185
    }
186
187
    /**
188
     * Sets whether this phone number uses an italian leading zero.
189
     *
190
     * @param bool $value True to use italian leading zero, false otherwise.
191
     *
192
     * @return PhoneNumber This PhoneNumber instance, for chaining method calls.
193
     */
194 133
    public function setItalianLeadingZero($value)
195
    {
196 133
        $this->italianLeadingZero = (bool) $value;
197 133
        return $this;
198
    }
199
200
    /**
201
     * Clears the italian leading zero information of this phone number.
202
     *
203
     * @return PhoneNumber This PhoneNumber instance, for chaining method calls.
204
     */
205 13
    public function clearItalianLeadingZero()
206
    {
207 13
        $this->italianLeadingZero = null;
208 13
        return $this;
209
    }
210
211
    /**
212
     * The raw input.
213
     *
214
     * @var string|null
215
     */
216
    private $rawInput = null;
217
218
    /**
219
     * Returns whether this phone number has a raw input.
220
     *
221
     * @return bool True if a raw input is set, false otherwise.
222
     */
223 18
    public function hasRawInput()
224
    {
225 18
        return isset($this->rawInput);
226
    }
227
228
    /**
229
     * Returns the raw input of this phone number.
230
     *
231
     * @return string|null The raw input, or null if not set.
232
     */
233 6
    public function getRawInput()
234
    {
235 6
        return $this->rawInput;
236
    }
237
238
    /**
239
     * Sets the raw input of this phone number.
240
     *
241
     * @param string $value The raw input.
242
     *
243
     * @return PhoneNumber This PhoneNumber instance, for chaining method calls.
244
     */
245 95
    public function setRawInput($value)
246
    {
247 95
        $this->rawInput = (string) $value;
248 95
        return $this;
249
    }
250
251
    /**
252
     * Clears the raw input of this phone number.
253
     *
254
     * @return PhoneNumber This PhoneNumber instance, for chaining method calls.
255
     */
256 17
    public function clearRawInput()
257
    {
258 17
        $this->rawInput = null;
259 17
        return $this;
260
    }
261
262
    /**
263
     * The country code source.
264
     *
265
     * This must be one of the CountryCodeSource constants.
266
     *
267
     * @var int|null
268
     */
269
    private $countryCodeSource = null;
270
271
    /**
272
     * Returns whether this phone number has a country code source.
273
     *
274
     * @return bool True if a country code source is set, false otherwise.
275
     */
276 18
    public function hasCountryCodeSource()
277
    {
278 18
        return isset($this->countryCodeSource);
279
    }
280
281
    /**
282
     * Returns the country code source of this phone number.
283
     *
284
     * @return int|null A CountryCodeSource constant, or null if not set.
285
     */
286 5
    public function getCountryCodeSource()
287
    {
288 5
        return $this->countryCodeSource;
289
    }
290
291
    /**
292
     * Sets the country code source of this phone number.
293
     *
294
     * @param int $value A CountryCodeSource constant.
295
     *
296
     * @return PhoneNumber This PhoneNumber instance, for chaining method calls.
297
     */
298 7
    public function setCountryCodeSource($value)
299
    {
300 7
        $this->countryCodeSource = (int) $value;
301 7
        return $this;
302
    }
303
304
    /**
305
     * Clears the country code source of this phone number.
306
     *
307
     * @return PhoneNumber This PhoneNumber instance, for chaining method calls.
308
     */
309 16
    public function clearCountryCodeSource()
310
    {
311 16
        $this->countryCodeSource = null;
312 16
        return $this;
313
    }
314
315
    /**
316
     * The preferred domestic carrier code.
317
     *
318
     * @var string|null
319
     */
320
    private $preferredDomesticCarrierCode = null;
321
322
    /**
323
     * Returns whether this phone number has a preferred domestic carrier code.
324
     *
325
     * @return bool True if a preferred domestic carrier code is set, false otherwise.
326
     */
327 1603
    public function hasPreferredDomesticCarrierCode()
328
    {
329 1603
        return isset($this->preferredDomesticCarrierCode);
330
    }
331
332
    /**
333
     * Returns the preferred domestic carrier code of this phone number.
334
     *
335
     * @return string|null The preferred domestic carrier code, or null if not set.
336
     */
337 3
    public function getPreferredDomesticCarrierCode()
338
    {
339 3
        return $this->preferredDomesticCarrierCode;
340
    }
341
342
    /**
343
     * Sets the preferred domestic carrier code of this phone number.
344
     *
345
     * @param string $value The preferred domestic carrier code.
346
     *
347
     * @return PhoneNumber This PhoneNumber instance, for chaining method calls.
348
     */
349 8
    public function setPreferredDomesticCarrierCode($value)
350
    {
351 8
        $this->preferredDomesticCarrierCode = (string) $value;
352 8
        return $this;
353
    }
354
355
    /**
356
     * Clears the preferred domestic carrier code of this phone number.
357
     *
358
     * @return PhoneNumber This PhoneNumber instance, for chaining method calls.
359
     */
360 16
    public function clearPreferredDomesticCarrierCode()
361
    {
362 16
        $this->preferredDomesticCarrierCode = null;
363 16
        return $this;
364
    }
365
366
    /**
367
     * Whether this phone number has a number of leading zeros set.
368
     *
369
     * @var bool
370
     */
371
    private $hasNumberOfLeadingZeros = false;
372
373
    /**
374
     * The number of leading zeros of this phone number.
375
     *
376
     * @var int
377
     */
378
    private $numberOfLeadingZeros = 1;
379
380
    /**
381
     * Returns whether this phone number has a number of leading zeros set.
382
     *
383
     * @return bool True if a number of leading zeros is set, false otherwise.
384
     */
385 1597
    public function hasNumberOfLeadingZeros()
386
    {
387 1597
        return $this->hasNumberOfLeadingZeros;
388
    }
389
390
    /**
391
     * Returns the number of leading zeros of this phone number.
392
     *
393
     * @return int The number of leading zeros.
394
     */
395 57
    public function getNumberOfLeadingZeros()
396
    {
397 57
        return $this->numberOfLeadingZeros;
398
    }
399
400
    /**
401
     * Sets the number of leading zeros of this phone number.
402
     *
403
     * @param int $value The number of leading zeros.
404
     *
405
     * @return PhoneNumber This PhoneNumber instance, for chaining method calls.
406
     */
407 6
    public function setNumberOfLeadingZeros($value)
408
    {
409 6
        $this->hasNumberOfLeadingZeros = true;
410 6
        $this->numberOfLeadingZeros = (int) $value;
411 6
        return $this;
412
    }
413
414
    /**
415
     * Clears the number of leading zeros of this phone number.
416
     *
417
     * @return PhoneNumber This PhoneNumber instance, for chaining method calls.
418
     */
419 12
    public function clearNumberOfLeadingZeros()
420
    {
421 12
        $this->hasNumberOfLeadingZeros = false;
422 12
        $this->numberOfLeadingZeros = 1;
423 12
        return $this;
424
    }
425
426
    /**
427
     * Clears this phone number.
428
     *
429
     * This effectively resets this phone number to the state of a new instance.
430
     *
431
     * @return PhoneNumber This PhoneNumber instance, for chaining method calls.
432
     */
433 12
    public function clear()
434
    {
435 12
        $this->clearCountryCode();
436 12
        $this->clearNationalNumber();
437 12
        $this->clearExtension();
438 12
        $this->clearItalianLeadingZero();
439 12
        $this->clearNumberOfLeadingZeros();
440 12
        $this->clearRawInput();
441 12
        $this->clearCountryCodeSource();
442 12
        $this->clearPreferredDomesticCarrierCode();
443 12
        return $this;
444
    }
445
446
    /**
447
     * Merges the information from another phone number into this phone number.
448
     *
449
     * @param PhoneNumber $other The phone number to copy.
450
     *
451
     * @return PhoneNumber This PhoneNumber instance, for chaining method calls.
452
     */
453 11
    public function mergeFrom(PhoneNumber $other)
454
    {
455 11
        if ($other->hasCountryCode()) {
456 11
            $this->setCountryCode($other->getCountryCode());
457
        }
458 11
        if ($other->hasNationalNumber()) {
459 11
            $this->setNationalNumber($other->getNationalNumber());
460
        }
461 11
        if ($other->hasExtension()) {
462 4
            $this->setExtension($other->getExtension());
463
        }
464 11
        if ($other->hasItalianLeadingZero()) {
465 4
            $this->setItalianLeadingZero($other->isItalianLeadingZero());
0 ignored issues
show
Bug introduced by
It seems like $other->isItalianLeadingZero() targeting libphonenumber\PhoneNumber::isItalianLeadingZero() can also be of type null; however, libphonenumber\PhoneNumb...setItalianLeadingZero() does only seem to accept boolean, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
466
        }
467 11
        if ($other->hasNumberOfLeadingZeros()) {
468 1
            $this->setNumberOfLeadingZeros($other->getNumberOfLeadingZeros());
469
        }
470 11
        if ($other->hasRawInput()) {
471 1
            $this->setRawInput($other->getRawInput());
472
        }
473 11
        if ($other->hasCountryCodeSource()) {
474 1
            $this->setCountryCodeSource($other->getCountryCodeSource());
475
        }
476 11
        if ($other->hasPreferredDomesticCarrierCode()) {
477 1
            $this->setPreferredDomesticCarrierCode($other->getPreferredDomesticCarrierCode());
478
        }
479 11
        return $this;
480
    }
481
482
    /**
483
     * Returns whether this phone number is equal to another.
484
     *
485
     * @param PhoneNumber $other The phone number to compare.
486
     *
487
     * @return bool True if the phone numbers are equal, false otherwise.
488
     */
489 9
    public function equals(PhoneNumber $other)
490
    {
491 9
        $sameType = get_class($other) == get_class($this);
492 9
        $sameCountry = $this->hasCountryCode() == $other->hasCountryCode() &&
493 9
            (!$this->hasCountryCode() || $this->getCountryCode() == $other->getCountryCode());
494 9
        $sameNational = $this->hasNationalNumber() == $other->hasNationalNumber() &&
495 9
            (!$this->hasNationalNumber() || $this->getNationalNumber() == $other->getNationalNumber());
496 9
        $sameExt = $this->hasExtension() == $other->hasExtension() &&
497 9
            (!$this->hasExtension() || $this->hasExtension() == $other->hasExtension());
498 9
        $sameLead = $this->hasItalianLeadingZero() == $other->hasItalianLeadingZero() &&
499 9
            (!$this->hasItalianLeadingZero() || $this->isItalianLeadingZero() == $other->isItalianLeadingZero());
500 9
        $sameZeros = $this->getNumberOfLeadingZeros() == $other->getNumberOfLeadingZeros();
501 9
        $sameRaw = $this->hasRawInput() == $other->hasRawInput() &&
502 9
            (!$this->hasRawInput() || $this->getRawInput() == $other->getRawInput());
503 9
        $sameCountrySource = $this->hasCountryCodeSource() == $other->hasCountryCodeSource() &&
504 9
            (!$this->hasCountryCodeSource() || $this->getCountryCodeSource() == $other->getCountryCodeSource());
505 9
        $samePrefCar = $this->hasPreferredDomesticCarrierCode() == $other->hasPreferredDomesticCarrierCode() &&
506 8
            (!$this->hasPreferredDomesticCarrierCode() || $this->getPreferredDomesticCarrierCode(
507 9
                ) == $other->getPreferredDomesticCarrierCode());
508 9
        return $sameType && $sameCountry && $sameNational && $sameExt && $sameLead && $sameZeros && $sameRaw && $sameCountrySource && $samePrefCar;
509
    }
510
511
    /**
512
     * Returns a string representation of this phone number.
513
     * @return string
514
     */
515 1587
    public function __toString()
516
    {
517 1587
        $outputString = '';
518
519 1587
        $outputString .= 'Country Code: ' . $this->countryCode;
520 1587
        $outputString .= ' National Number: ' . $this->nationalNumber;
521 1587
        if ($this->hasItalianLeadingZero()) {
522 24
            $outputString .= ' Leading Zero(s): true';
523
        }
524 1587
        if ($this->hasNumberOfLeadingZeros()) {
525
            $outputString .= ' Number of leading zeros: ' . $this->numberOfLeadingZeros;
526
        }
527 1587
        if ($this->hasExtension()) {
528 1
            $outputString .= ' Extension: ' . $this->extension;
529
        }
530 1587
        if ($this->hasCountryCode()) {
531 1587
            $outputString .= ' Country Code Source: ' . $this->countryCodeSource;
532
        }
533 1587
        if ($this->hasPreferredDomesticCarrierCode()) {
534
            $outputString .= ' Preferred Domestic Carrier Code: ' . $this->preferredDomesticCarrierCode;
535
        }
536 1587
        return $outputString;
537
    }
538
539
    /**
540
     * @inheritDoc
541
     */
542 2
    public function serialize()
543
    {
544 2
        return serialize(
545
            array(
546 2
                $this->countryCode,
547 2
                $this->nationalNumber,
548 2
                $this->extension,
549 2
                $this->italianLeadingZero,
550 2
                $this->numberOfLeadingZeros,
551 2
                $this->rawInput,
552 2
                $this->countryCodeSource,
553 2
                $this->preferredDomesticCarrierCode
554
            )
555
        );
556
    }
557
558
    /**
559
     * @inheritDoc
560
     */
561 2
    public function unserialize($serialized)
562
    {
563 2
        $data = unserialize($serialized);
564
565
        list (
566 2
            $this->countryCode,
567 2
            $this->nationalNumber,
568 2
            $this->extension,
569 2
            $this->italianLeadingZero,
570 2
            $this->numberOfLeadingZeros,
571 2
            $this->rawInput,
572 2
            $this->countryCodeSource,
573 2
            $this->preferredDomesticCarrierCode
574 2
        ) = $data;
575
576 2
        $this->hasNumberOfLeadingZeros = true;
577 2
    }
578
}
579