Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1441 ariadna 1
<?php
2
 
3
declare(strict_types=1);
4
 
5
namespace libphonenumber;
6
 
7
class PhoneNumber implements \Serializable, \Stringable
8
{
9
    /**
10
     * The country calling code for this number, as defined by the International Telecommunication Union
11
     * (ITU). For example, this would be 1 for NANPA countries, and 33 for France.
12
     */
13
    protected int $countryCode = 0;
14
    /**
15
     * National (significant) Number is defined in International Telecommunication Union (ITU)
16
     * Recommendation E.164. It is a language/country-neutral representation of a phone number at a
17
     * country level. For countries which have the concept of an "area code" or "national destination
18
     * code", this is included in the National (significant) Number. Although the ITU says the maximum
19
     * length should be 15, we have found longer numbers in some countries e.g. Germany.
20
     *
21
     * Note that the National (significant) Number does not contain the National(trunk) prefix.
22
     */
23
    protected ?string $nationalNumber = null;
24
    /**
25
     * Extension is not standardized in ITU recommendations, except for being defined as a series of
26
     * numbers with a maximum length of 40 digits. It is defined as a string here to accommodate for the
27
     * possible use of a leading zero in the extension (organizations have complete freedom to do so,
28
     * as there is no standard defined). However, only ASCII digits should be stored here.
29
     */
30
    protected ?string $extension = null;
31
    /**
32
     * In some countries, the national (significant) number starts with one or more "0"s without this
33
     * being a national prefix or trunk code of some kind. For example, the leading zero in the national
34
     * (significant) number of an Italian phone number indicates the number is a fixed-line number.
35
     * There have been plans to migrate fixed-line numbers to start with the digit two since December
36
     * 2000, but it has not happened yet. See http://en.wikipedia.org/wiki/%2B39 for more details.
37
     *
38
     * These fields can be safely ignored (there is no need to set them) for most countries. Some
39
     * limited number of countries behave like Italy - for these cases, if the leading zero(s) of a
40
     * number would be retained even when dialling internationally, set this flag to true, and also
41
     * set the number of leading zeros.
42
     *
43
     * Clients who use the parsing functionality of the i18n phone number libraries
44
     * will have these fields set if necessary automatically.
45
     */
46
    protected ?bool $italianLeadingZero = null;
47
    /**
48
     * This field is used to store the raw input string containing phone numbers before it was
49
     * canonicalized by the library. For example, it could be used to store alphanumerical numbers
50
     * such as "1-800-GOOG-411".
51
     */
52
    protected ?string $rawInput = null;
53
    /**
54
     * The source from which the country_code is derived. This is not set in the general parsing method,
55
     * but in the method that parses and keeps raw_input. New fields could be added upon request.
56
     *
57
     * @see CountryCodeSource
58
     *
59
     * This must be one of the CountryCodeSource constants.
60
     */
61
    protected int $countryCodeSource = CountryCodeSource::UNSPECIFIED;
62
    /**
63
     * The carrier selection code that is preferred when calling this phone number domestically. This
64
     * also includes codes that need to be dialed in some countries when calling from landlines to
65
     * mobiles or vice versa. For example, in Columbia, a "3" needs to be dialed before the phone number
66
     * itself when calling from a mobile phone to a domestic landline phone and vice versa.
67
     *
68
     * Note this is the "preferred" code, which means other codes may work as well.
69
     */
70
    protected ?string $preferredDomesticCarrierCode = null;
71
    /**
72
     * Whether this phone number has a number of leading zeros set.
73
     */
74
    protected bool $hasNumberOfLeadingZeros = false;
75
    /**
76
     * The number of leading zeros of this phone number.
77
     */
78
    protected int $numberOfLeadingZeros = 1;
79
    private bool $hasCountryCode = false;
80
 
81
    /**
82
     * Clears this phone number.
83
     *
84
     * This effectively resets this phone number to the state of a new instance.
85
     *
86
     * @return PhoneNumber This PhoneNumber instance, for chaining method calls.
87
     */
88
    public function clear(): PhoneNumber
89
    {
90
        $this->clearCountryCode();
91
        $this->clearNationalNumber();
92
        $this->clearExtension();
93
        $this->clearItalianLeadingZero();
94
        $this->clearNumberOfLeadingZeros();
95
        $this->clearRawInput();
96
        $this->clearCountryCodeSource();
97
        $this->clearPreferredDomesticCarrierCode();
98
        return $this;
99
    }
100
 
101
    /**
102
     * Clears the country code of this phone number.
103
     *
104
     * @return PhoneNumber This PhoneNumber instance, for chaining method calls.
105
     */
106
    public function clearCountryCode(): PhoneNumber
107
    {
108
        $this->countryCode = 0;
109
        $this->hasCountryCode = false;
110
        return $this;
111
    }
112
 
113
    /**
114
     * Clears the national number of this phone number.
115
     *
116
     * @return PhoneNumber This PhoneNumber instance, for chaining method calls.
117
     */
118
    public function clearNationalNumber(): PhoneNumber
119
    {
120
        $this->nationalNumber = null;
121
        return $this;
122
    }
123
 
124
    /**
125
     * Clears the extension of this phone number.
126
     *
127
     * @return PhoneNumber This PhoneNumber instance, for chaining method calls.
128
     */
129
    public function clearExtension(): PhoneNumber
130
    {
131
        $this->extension = null;
132
        return $this;
133
    }
134
 
135
    /**
136
     * Clears the italian leading zero information of this phone number.
137
     *
138
     * @return PhoneNumber This PhoneNumber instance, for chaining method calls.
139
     */
140
    public function clearItalianLeadingZero(): PhoneNumber
141
    {
142
        $this->italianLeadingZero = null;
143
        return $this;
144
    }
145
 
146
    /**
147
     * Clears the number of leading zeros of this phone number.
148
     *
149
     * @return PhoneNumber This PhoneNumber instance, for chaining method calls.
150
     */
151
    public function clearNumberOfLeadingZeros(): PhoneNumber
152
    {
153
        $this->hasNumberOfLeadingZeros = false;
154
        $this->numberOfLeadingZeros = 1;
155
        return $this;
156
    }
157
 
158
    /**
159
     * Clears the raw input of this phone number.
160
     *
161
     * @return PhoneNumber This PhoneNumber instance, for chaining method calls.
162
     */
163
    public function clearRawInput(): PhoneNumber
164
    {
165
        $this->rawInput = null;
166
        return $this;
167
    }
168
 
169
    /**
170
     * Clears the country code source of this phone number.
171
     *
172
     * @return PhoneNumber This PhoneNumber instance, for chaining method calls.
173
     */
174
    public function clearCountryCodeSource(): PhoneNumber
175
    {
176
        $this->countryCodeSource = CountryCodeSource::UNSPECIFIED;
177
        return $this;
178
    }
179
 
180
    /**
181
     * Clears the preferred domestic carrier code of this phone number.
182
     *
183
     * @return PhoneNumber This PhoneNumber instance, for chaining method calls.
184
     */
185
    public function clearPreferredDomesticCarrierCode(): PhoneNumber
186
    {
187
        $this->preferredDomesticCarrierCode = null;
188
        return $this;
189
    }
190
 
191
    /**
192
     * Merges the information from another phone number into this phone number.
193
     *
194
     * @param PhoneNumber $other The phone number to copy.
195
     *
196
     * @return PhoneNumber This PhoneNumber instance, for chaining method calls.
197
     */
198
    public function mergeFrom(PhoneNumber $other): PhoneNumber
199
    {
200
        if ($other->hasCountryCode()) {
201
            $this->setCountryCode($other->getCountryCode());
202
        }
203
        if ($other->hasNationalNumber()) {
204
            $this->setNationalNumber($other->getNationalNumber());
205
        }
206
        if ($other->hasExtension()) {
207
            $this->setExtension($other->getExtension());
208
        }
209
        if ($other->hasItalianLeadingZero()) {
210
            $this->setItalianLeadingZero($other->isItalianLeadingZero());
211
        }
212
        if ($other->hasNumberOfLeadingZeros()) {
213
            $this->setNumberOfLeadingZeros($other->getNumberOfLeadingZeros());
214
        }
215
        if ($other->hasRawInput()) {
216
            $this->setRawInput($other->getRawInput());
217
        }
218
        if ($other->hasCountryCodeSource()) {
219
            $this->setCountryCodeSource($other->getCountryCodeSource());
220
        }
221
        if ($other->hasPreferredDomesticCarrierCode()) {
222
            $this->setPreferredDomesticCarrierCode($other->getPreferredDomesticCarrierCode());
223
        }
224
        return $this;
225
    }
226
 
227
    /**
228
     * Returns whether this phone number has a country code set.
229
     *
230
     * @return bool True if a country code is set, false otherwise.
231
     */
232
    public function hasCountryCode(): bool
233
    {
234
        return $this->hasCountryCode;
235
    }
236
 
237
    public function getCountryCode(): int
238
    {
239
        return $this->countryCode;
240
    }
241
 
242
    /**
243
     * Sets the country code of this phone number.
244
     *
245
     * @param int $value The country code.
246
     *
247
     * @return PhoneNumber This PhoneNumber instance, for chaining method calls.
248
     */
249
    public function setCountryCode(int $value): PhoneNumber
250
    {
251
        $this->hasCountryCode = true;
252
        $this->countryCode = $value;
253
        return $this;
254
    }
255
 
256
    /**
257
     * Returns whether this phone number has a national number set.
258
     *
259
     * @return bool True if a national number is set, false otherwise.
260
     */
261
    public function hasNationalNumber(): bool
262
    {
263
        return $this->nationalNumber !== null;
264
    }
265
 
266
    /**
267
     * Returns the national number of this phone number.
268
     *
269
     * @return string|null The national number, or null if not set.
270
     */
271
    public function getNationalNumber(): ?string
272
    {
273
        return $this->nationalNumber;
274
    }
275
 
276
    /**
277
     * Sets the national number of this phone number.
278
     *
279
     * @param string $value The national number.
280
     * @return PhoneNumber This PhoneNumber instance, for chaining method calls.
281
     */
282
    public function setNationalNumber(string $value): PhoneNumber
283
    {
284
        $this->nationalNumber = $value;
285
        return $this;
286
    }
287
 
288
    /**
289
     * Returns whether this phone number has an extension set.
290
     *
291
     * @return bool True if an extension is set, false otherwise.
292
     */
293
    public function hasExtension(): bool
294
    {
295
        return $this->extension !== null;
296
    }
297
 
298
    /**
299
     * Returns the extension of this phone number.
300
     *
301
     * @return string|null The extension, or null if not set.
302
     */
303
    public function getExtension(): ?string
304
    {
305
        return $this->extension;
306
    }
307
 
308
    /**
309
     * Sets the extension of this phone number.
310
     *
311
     * @param string $value The extension.
312
     * @return PhoneNumber This PhoneNumber instance, for chaining method calls.
313
     */
314
    public function setExtension(string $value): PhoneNumber
315
    {
316
        $this->extension = $value;
317
        return $this;
318
    }
319
 
320
    /**
321
     * Returns whether this phone number has the italian leading zero information set.
322
     */
323
    public function hasItalianLeadingZero(): bool
324
    {
325
        return $this->italianLeadingZero !== null;
326
    }
327
 
328
    /**
329
     * Sets whether this phone number uses an italian leading zero.
330
     *
331
     * @param bool $value True to use italian leading zero, false otherwise.
332
     * @return PhoneNumber This PhoneNumber instance, for chaining method calls.
333
     */
334
    public function setItalianLeadingZero(bool $value): PhoneNumber
335
    {
336
        $this->italianLeadingZero = $value;
337
        return $this;
338
    }
339
 
340
    /**
341
     * Returns whether this phone number uses an italian leading zero.
342
     *
343
     * @return bool|null True if it uses an italian leading zero, false if it does not, null if not set.
344
     */
345
    public function isItalianLeadingZero(): ?bool
346
    {
347
        return $this->italianLeadingZero;
348
    }
349
 
350
    /**
351
     * Returns whether this phone number has a number of leading zeros set.
352
     *
353
     * @return bool True if a number of leading zeros is set, false otherwise.
354
     */
355
    public function hasNumberOfLeadingZeros(): bool
356
    {
357
        return $this->hasNumberOfLeadingZeros;
358
    }
359
 
360
    /**
361
     * Returns the number of leading zeros of this phone number.
362
     *
363
     * @return int The number of leading zeros.
364
     */
365
    public function getNumberOfLeadingZeros(): int
366
    {
367
        return $this->numberOfLeadingZeros;
368
    }
369
 
370
    /**
371
     * Sets the number of leading zeros of this phone number.
372
     *
373
     * @param int $value The number of leading zeros.
374
     * @return PhoneNumber This PhoneNumber instance, for chaining method calls.
375
     */
376
    public function setNumberOfLeadingZeros(int $value): PhoneNumber
377
    {
378
        $this->hasNumberOfLeadingZeros = true;
379
        $this->numberOfLeadingZeros = $value;
380
        return $this;
381
    }
382
 
383
    /**
384
     * Returns whether this phone number has a raw input.
385
     *
386
     * @return bool True if a raw input is set, false otherwise.
387
     */
388
    public function hasRawInput(): bool
389
    {
390
        return $this->rawInput !== null;
391
    }
392
 
393
    /**
394
     * Returns the raw input of this phone number.
395
     *
396
     * @return string|null The raw input, or null if not set.
397
     */
398
    public function getRawInput(): ?string
399
    {
400
        return $this->rawInput;
401
    }
402
 
403
    /**
404
     * Sets the raw input of this phone number.
405
     *
406
     * @param string $value The raw input.
407
     * @return PhoneNumber This PhoneNumber instance, for chaining method calls.
408
     */
409
    public function setRawInput(string $value): PhoneNumber
410
    {
411
        $this->rawInput = $value;
412
        return $this;
413
    }
414
 
415
    /**
416
     * Returns whether this phone number has a country code source.
417
     *
418
     * @return bool True if a country code source is set, false otherwise.
419
     */
420
    public function hasCountryCodeSource(): bool
421
    {
422
        return $this->countryCodeSource !== CountryCodeSource::UNSPECIFIED;
423
    }
424
 
425
    /**
426
     * Returns the country code source of this phone number.
427
     *
428
     * @return int|null A CountryCodeSource constant, or null if not set.
429
     */
430
    public function getCountryCodeSource(): ?int
431
    {
432
        return $this->countryCodeSource;
433
    }
434
 
435
    /**
436
     * Sets the country code source of this phone number.
437
     *
438
     * @param int $value A CountryCodeSource constant.
439
     * @return PhoneNumber This PhoneNumber instance, for chaining method calls.
440
     */
441
    public function setCountryCodeSource(int $value): PhoneNumber
442
    {
443
        $this->countryCodeSource = $value;
444
        return $this;
445
    }
446
 
447
    /**
448
     * Returns whether this phone number has a preferred domestic carrier code.
449
     *
450
     * @return bool True if a preferred domestic carrier code is set, false otherwise.
451
     */
452
    public function hasPreferredDomesticCarrierCode(): bool
453
    {
454
        return $this->preferredDomesticCarrierCode !== null;
455
    }
456
 
457
    /**
458
     * Returns the preferred domestic carrier code of this phone number.
459
     *
460
     * @return string|null The preferred domestic carrier code, or null if not set.
461
     */
462
    public function getPreferredDomesticCarrierCode(): ?string
463
    {
464
        return $this->preferredDomesticCarrierCode;
465
    }
466
 
467
    /**
468
     * Sets the preferred domestic carrier code of this phone number.
469
     *
470
     * @param string $value The preferred domestic carrier code.
471
     * @return PhoneNumber This PhoneNumber instance, for chaining method calls.
472
     */
473
    public function setPreferredDomesticCarrierCode(string $value): PhoneNumber
474
    {
475
        $this->preferredDomesticCarrierCode = $value;
476
        return $this;
477
    }
478
 
479
    /**
480
     * Returns whether this phone number is equal to another.
481
     *
482
     * @param PhoneNumber $other The phone number to compare.
483
     *
484
     * @return bool True if the phone numbers are equal, false otherwise.
485
     */
486
    public function equals(PhoneNumber $other): bool
487
    {
488
        if ($this === $other) {
489
            return true;
490
        }
491
 
492
        return $this->countryCode === $other->countryCode
493
            && $this->nationalNumber === $other->nationalNumber
494
            && $this->extension === $other->extension
495
            && $this->italianLeadingZero === $other->italianLeadingZero
496
            && $this->numberOfLeadingZeros === $other->numberOfLeadingZeros
497
            && $this->rawInput === $other->rawInput
498
            && $this->countryCodeSource === $other->countryCodeSource
499
            && $this->preferredDomesticCarrierCode === $other->preferredDomesticCarrierCode;
500
    }
501
 
502
    /**
503
     * Returns a string representation of this phone number.
504
     */
505
    public function __toString(): string
506
    {
507
        $outputString = 'Country Code: ' . $this->countryCode;
508
        $outputString .= ' National Number: ' . $this->nationalNumber;
509
        if ($this->hasItalianLeadingZero()) {
510
            $outputString .= ' Leading Zero(s): true';
511
        }
512
        if ($this->hasNumberOfLeadingZeros()) {
513
            $outputString .= ' Number of leading zeros: ' . $this->numberOfLeadingZeros;
514
        }
515
        if ($this->hasExtension()) {
516
            $outputString .= ' Extension: ' . $this->extension;
517
        }
518
        if ($this->hasCountryCodeSource()) {
519
            $outputString .= ' Country Code Source: ' . $this->countryCodeSource;
520
        }
521
        if ($this->hasPreferredDomesticCarrierCode()) {
522
            $outputString .= ' Preferred Domestic Carrier Code: ' . $this->preferredDomesticCarrierCode;
523
        }
524
        return $outputString;
525
    }
526
 
527
    /**
528
     *
529
     */
530
    public function serialize(): ?string
531
    {
532
        return serialize($this->__serialize());
533
    }
534
 
535
    /**
536
     * @return array{?int,?string,?string,?bool,int,?string,int,?string}
537
     */
538
    public function __serialize(): array
539
    {
540
        return [
541
            $this->countryCode,
542
            $this->nationalNumber,
543
            $this->extension,
544
            $this->italianLeadingZero,
545
            $this->numberOfLeadingZeros,
546
            $this->rawInput,
547
            $this->countryCodeSource,
548
            $this->preferredDomesticCarrierCode,
549
        ];
550
    }
551
 
552
    /**
553
     *
554
     */
555
    public function unserialize($data)
556
    {
557
        $this->__unserialize(unserialize($data, ['allowed_classes' => [__CLASS__]]));
558
    }
559
 
560
    /**
561
     * @param array{int,?string,?string,?bool,int,?string,int,?string} $data
562
     */
563
    public function __unserialize(array $data): void
564
    {
565
        [
566
            $this->countryCode,
567
            $this->nationalNumber,
568
            $this->extension,
569
            $this->italianLeadingZero,
570
            $this->numberOfLeadingZeros,
571
            $this->rawInput,
572
            $this->countryCodeSource,
573
            $this->preferredDomesticCarrierCode
574
        ] = $data;
575
 
576
        if ($this->numberOfLeadingZeros > 1) {
577
            $this->hasNumberOfLeadingZeros = true;
578
        }
579
    }
580
}