Completed
Push — master ( 9aae8b...f3147e )
by Joshua
16s
created

ShortNumberInfo::isPossibleShortNumber()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 4.0119

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 19
ccs 10
cts 11
cp 0.9091
rs 9.2
cc 4
eloc 10
nc 4
nop 1
crap 4.0119
1
<?php
2
/**
3
 * Methods for getting information about short phone numbers, such as short codes and emergency
4
 * numbers. Note that most commercial short numbers are not handled here, but by the
5
 * {@link PhoneNumberUtil}.
6
 *
7
 * @author Shaopeng Jia
8
 * @author David Yonge-Mallo
9
 * @since 5.8
10
 */
11
12
namespace libphonenumber;
13
14
class ShortNumberInfo
15
{
16
    const META_DATA_FILE_PREFIX = 'ShortNumberMetadata';
17
    /**
18
     * @var ShortNumberInfo
19
     */
20
    protected static $instance = null;
21
    /**
22
     * @var MatcherAPIInterface
23
     */
24
    protected $matcherAPI;
25
    protected $currentFilePrefix;
26
    protected $regionToMetadataMap = array();
27
    protected $countryCallingCodeToRegionCodeMap = array();
28
    protected $countryCodeToNonGeographicalMetadataMap = array();
29
    protected static $regionsWhereEmergencyNumbersMustBeExact = array(
30
        'BR',
31
        'CL',
32
        'NI',
33
    );
34
35 28
    protected function __construct(MatcherAPIInterface $matcherAPI)
36
    {
37 28
        $this->matcherAPI = $matcherAPI;
38
39
        // TODO: Create ShortNumberInfo for a given map
40 28
        $this->countryCallingCodeToRegionCodeMap = CountryCodeToRegionCodeMap::$countryCodeToRegionCodeMap;
41
42 28
        $this->currentFilePrefix = dirname(__FILE__) . '/data/' . static::META_DATA_FILE_PREFIX;
43
44
        // Initialise PhoneNumberUtil to make sure regex's are setup correctly
45 28
        PhoneNumberUtil::getInstance();
46 28
    }
47
48
    /**
49
     * Returns the singleton instance of ShortNumberInfo
50
     *
51
     * @return \libphonenumber\ShortNumberInfo
52
     */
53 5372
    public static function getInstance()
54
    {
55 5372
        if (null === static::$instance) {
56 28
            static::$instance = new self(RegexBasedMatcher::create());
57 28
        }
58
59 5372
        return static::$instance;
60
    }
61
62 27
    public static function resetInstance()
63
    {
64 27
        static::$instance = null;
65 27
    }
66
67
    /**
68
     * Returns a list with teh region codes that match the specific country calling code. For
69
     * non-geographical country calling codes, the region code 001 is returned. Also, in the case
70
     * of no region code being found, an empty list is returned.
71
     *
72
     * @param int $countryCallingCode
73
     * @return array
74
     */
75 631
    protected function getRegionCodesForCountryCode($countryCallingCode)
76
    {
77 631
        if (!array_key_exists($countryCallingCode, $this->countryCallingCodeToRegionCodeMap)) {
78 1
            $regionCodes = null;
79 1
        } else {
80 631
            $regionCodes = $this->countryCallingCodeToRegionCodeMap[$countryCallingCode];
81
        }
82
83 631
        return ($regionCodes === null) ? array() : $regionCodes;
84
    }
85
86
    /**
87
     * Helper method to check that the country calling code of the number matches the region it's
88
     * being dialed from.
89
     * @param PhoneNumber $number
90
     * @param string $regionDialingFrom
91
     * @return bool
92
     */
93 630
    protected function regionDialingFromMatchesNumber(PhoneNumber $number, $regionDialingFrom)
94
    {
95 630
        $regionCodes = $this->getRegionCodesForCountryCode($number->getCountryCode());
96
97 630
        return in_array($regionDialingFrom, $regionCodes);
98
    }
99
100
    public function getSupportedRegions()
101
    {
102
        return ShortNumbersRegionCodeSet::$shortNumbersRegionCodeSet;
103
    }
104
105
    /**
106
     * Gets a valid short number for the specified region.
107
     *
108
     * @param $regionCode String the region for which an example short number is needed
109
     * @return string a valid short number for the specified region. Returns an empty string when the
110
     *      metadata does not contain such information.
111
     */
112 239
    public function getExampleShortNumber($regionCode)
113
    {
114 239
        $phoneMetadata = $this->getMetadataForRegion($regionCode);
115 239
        if ($phoneMetadata === null) {
116 1
            return "";
117
        }
118
119
        /** @var PhoneNumberDesc $desc */
120 239
        $desc = $phoneMetadata->getShortCode();
121 239
        if ($desc !== null && $desc->hasExampleNumber()) {
122 239
            return $desc->getExampleNumber();
123
        }
124
        return "";
125
    }
126
127
    /**
128
     * @param $regionCode
129
     * @return PhoneMetadata|null
130
     */
131 1933
    public function getMetadataForRegion($regionCode)
132
    {
133 1933
        if (!in_array($regionCode, ShortNumbersRegionCodeSet::$shortNumbersRegionCodeSet)) {
134 1
            return null;
135
        }
136
137 1933
        if (!isset($this->regionToMetadataMap[$regionCode])) {
138
            // The regionCode here will be valid and won't be '001', so we don't need to worry about
139
            // what to pass in for the country calling code.
140 258
            $this->loadMetadataFromFile($this->currentFilePrefix, $regionCode, 0);
141 258
        }
142
143 1933
        return isset($this->regionToMetadataMap[$regionCode]) ? $this->regionToMetadataMap[$regionCode] : null;
144
    }
145
146 258
    protected function loadMetadataFromFile($filePrefix, $regionCode, $countryCallingCode)
147
    {
148 258
        $isNonGeoRegion = PhoneNumberUtil::REGION_CODE_FOR_NON_GEO_ENTITY === $regionCode;
149 258
        $fileName = $filePrefix . '_' . ($isNonGeoRegion ? $countryCallingCode : $regionCode) . '.php';
150 258
        if (!is_readable($fileName)) {
151
            throw new \Exception('missing metadata: ' . $fileName);
152
        } else {
153 258
            $data = include $fileName;
154 258
            $metadata = new PhoneMetadata();
155 258
            $metadata->fromArray($data);
156 258
            if ($isNonGeoRegion) {
157
                $this->countryCodeToNonGeographicalMetadataMap[$countryCallingCode] = $metadata;
158
            } else {
159 258
                $this->regionToMetadataMap[$regionCode] = $metadata;
160
            }
161
        }
162 258
    }
163
164
    /**
165
     *  Gets a valid short number for the specified cost category.
166
     *
167
     * @param string $regionCode the region for which an example short number is needed
168
     * @param int $cost the cost category of number that is needed
169
     * @return string a valid short number for the specified region and cost category. Returns an empty string
170
     * when the metadata does not contain such information, or the cost is UNKNOWN_COST.
171
     */
172 954
    public function getExampleShortNumberForCost($regionCode, $cost)
173
    {
174 954
        $phoneMetadata = $this->getMetadataForRegion($regionCode);
175 954
        if ($phoneMetadata === null) {
176
            return "";
177
        }
178
179
        /** @var PhoneNumberDesc $desc */
180 954
        $desc = null;
181
        switch ($cost) {
182 954
            case ShortNumberCost::TOLL_FREE:
183 240
                $desc = $phoneMetadata->getTollFree();
184 240
                break;
185 716
            case ShortNumberCost::STANDARD_RATE:
186 240
                $desc = $phoneMetadata->getStandardRate();
187 240
                break;
188 478
            case ShortNumberCost::PREMIUM_RATE:
189 240
                $desc = $phoneMetadata->getPremiumRate();
190 240
                break;
191 239
            default:
192
                // UNKNOWN_COST numbers are computed by the process of elimination from the other cost categories
193 239
                break;
194 239
        }
195
196 954
        if ($desc !== null && $desc->hasExampleNumber()) {
197 83
            return $desc->getExampleNumber();
198
        }
199
200 872
        return "";
201
    }
202
203
    /**
204
     * Returns true if the given number, exactly as dialed, might be used to connect to an emergency
205
     * service in the given region.
206
     * <p>
207
     * This method accepts a string, rather than a PhoneNumber, because it needs to distinguish
208
     * cases such as "+1 911" and "911", where the former may not connect to an emergency service in
209
     * all cases but the latter would. This method takes into account cases where the number might
210
     * contain formatting, or might have additional digits appended (when it is okay to do that in
211
     * the specified region).
212
     *
213
     * @param string $number the phone number to test
214
     * @param string $regionCode the region where the phone number if being dialled
215
     * @return boolean whether the number might be used to connect to an emergency service in the given region
216
     */
217 10
    public function connectsToEmergencyNumber($number, $regionCode)
218
    {
219 10
        return $this->matchesEmergencyNumberHelper($number, $regionCode, true /* allows prefix match */);
220
    }
221
222
    /**
223
     * @param string $number
224
     * @param string $regionCode
225
     * @param bool $allowPrefixMatch
226
     * @return bool
227
     */
228 262
    protected function matchesEmergencyNumberHelper($number, $regionCode, $allowPrefixMatch)
229
    {
230 262
        $number = PhoneNumberUtil::extractPossibleNumber($number);
231 262
        $matcher = new Matcher(PhoneNumberUtil::$PLUS_CHARS_PATTERN, $number);
232 262
        if ($matcher->lookingAt()) {
233
            // Returns false if the number starts with a plus sign. We don't believe dialing the country
234
            // code before emergency numbers (e.g. +1911) works, but later, if that proves to work, we can
235
            // add additional logic here to handle it.
236 2
            return false;
237
        }
238
239 260
        $metadata = $this->getMetadataForRegion($regionCode);
240 260
        if ($metadata === null || !$metadata->hasEmergency()) {
241
            return false;
242
        }
243
244 260
        $normalizedNumber = PhoneNumberUtil::normalizeDigitsOnly($number);
245 260
        $emergencyDesc = $metadata->getEmergency();
246
247
        $allowPrefixMatchForRegion = ($allowPrefixMatch
248 260
            && !in_array($regionCode, static::$regionsWhereEmergencyNumbersMustBeExact)
249 260
        );
250
251 260
        return $this->matcherAPI->matchesNationalNumber($normalizedNumber, $emergencyDesc, $allowPrefixMatchForRegion);
252
    }
253
254
    /**
255
     * Given a valid short number, determines whether it is carrier-specific (however, nothing is
256
     * implied about its validity). If it is important that the number is valid, then its validity
257
     * must first be checked using {@link isValidShortNumber} or
258
     * {@link #isValidShortNumberForRegion}.
259
     *
260
     * @param PhoneNumber $number the valid short number to check
261
     * @return boolean whether the short number is carrier-specific (assuming the input was a valid short
262
     *     number).
263
     */
264 66
    public function isCarrierSpecific(PhoneNumber $number)
265
    {
266 66
        $regionCodes = $this->getRegionCodesForCountryCode($number->getCountryCode());
267 66
        $regionCode = $this->getRegionCodeForShortNumberFromRegionList($number, $regionCodes);
268 66
        $nationalNumber = $this->getNationalSignificantNumber($number);
269 66
        $phoneMetadata = $this->getMetadataForRegion($regionCode);
270
271 66
        return ($phoneMetadata !== null) && ($this->matchesPossibleNumberAndNationalNumber(
272 66
            $nationalNumber,
273 66
            $phoneMetadata->getCarrierSpecific()
274 66
        ));
275
    }
276
277
    /**
278
     * Helper method to get the region code for a given phone number, from a list of possible region
279
     * codes. If the list contains more than one region, the first region for which the number is
280
     * valid is returned.
281
     *
282
     * @param PhoneNumber $number
283
     * @param $regionCodes
284
     * @return String|null Region Code (or null if none are found)
285
     */
286 308
    protected function getRegionCodeForShortNumberFromRegionList(PhoneNumber $number, $regionCodes)
287
    {
288 308
        if (count($regionCodes) == 0) {
289
            return null;
290 308
        } elseif (count($regionCodes) == 1) {
291 244
            return $regionCodes[0];
292
        }
293
294 65
        $nationalNumber = $this->getNationalSignificantNumber($number);
295
296 65
        foreach ($regionCodes as $regionCode) {
297 65
            $phoneMetadata = $this->getMetadataForRegion($regionCode);
298
            if ($phoneMetadata !== null
299 65
                && $this->matchesPossibleNumberAndNationalNumber($nationalNumber, $phoneMetadata->getShortCode())
300 65
            ) {
301
                // The number is valid for this region.
302 65
                return $regionCode;
303
            }
304 11
        }
305
        return null;
306
    }
307
308
    /**
309
     * Check whether a short number is a possible number. If a country calling code is shared by
310
     * multiple regions, this returns true if it's possible in any of them. This provides a more
311
     * lenient check than {@link #isValidShortNumber}. See {@link
312
     * #IsPossibleShortNumberForRegion(PhoneNumber, String)} for details.
313
     *
314
     * @param $number PhoneNumber the short number to check
315
     * @return boolean whether the number is a possible short number
316
     */
317 2
    public function isPossibleShortNumber(PhoneNumber $number)
318
    {
319 2
        $regionCodes = $this->getRegionCodesForCountryCode($number->getCountryCode());
320 2
        $shortNumberLength = strlen($this->getNationalSignificantNumber($number));
321
322 2
        foreach ($regionCodes as $region) {
323 2
            $phoneMetadata = $this->getMetadataForRegion($region);
324
325 2
            if ($phoneMetadata === null) {
326
                continue;
327
            }
328
329 2
            if (in_array($shortNumberLength, $phoneMetadata->getGeneralDesc()->getPossibleLength())) {
330 1
                return true;
331
            }
332 2
        }
333
334 2
        return false;
335
    }
336
337
    /**
338
     * Check whether a short number is a possible number when dialled from a region, given the number
339
     * in the form of a string, and the region where the number is dialled from. This provides a more
340
     * lenient check than {@link #isValidShortNumber}.
341
     *
342
     * @param PhoneNumber $shortNumber The short number to check
343
     * @param string $regionDialingFrom Region dialing From
344
     * @return boolean whether the number is a possible short number
345
     */
346 306
    public function isPossibleShortNumberForRegion(PhoneNumber $shortNumber, $regionDialingFrom)
347
    {
348 306
        if (!$this->regionDialingFromMatchesNumber($shortNumber, $regionDialingFrom)) {
349 1
            return false;
350
        }
351
352 305
        $phoneMetadata = $this->getMetadataForRegion($regionDialingFrom);
353
354 305
        if ($phoneMetadata === null) {
355
            return false;
356
        }
357
358 305
        $numberLength = strlen($this->getNationalSignificantNumber($shortNumber));
359 305
        return in_array($numberLength, $phoneMetadata->getGeneralDesc()->getPossibleLength());
360
    }
361
362
    /**
363
     * Tests whether a short number matches a valid pattern. If a country calling code is shared by
364
     * multiple regions, this returns true if it's valid in any of them. Note that this doesn't verify
365
     * the number is actually in use, which is impossible to tell by just looking at the number
366
     * itself. See {@link #isValidShortNumberForRegion(PhoneNumber, String)} for details.
367
     *
368
     * @param $number PhoneNumber the short number for which we want to test the validity
369
     * @return boolean whether the short number matches a valid pattern
370
     */
371 242
    public function isValidShortNumber(PhoneNumber $number)
372
    {
373 242
        $regionCodes = $this->getRegionCodesForCountryCode($number->getCountryCode());
374 242
        $regionCode = $this->getRegionCodeForShortNumberFromRegionList($number, $regionCodes);
375 242
        if (count($regionCodes) > 1 && $regionCode !== null) {
376
            // If a matching region had been found for the phone number from among two or more regions,
377
            // then we have already implicitly verified its validity for that region.
378 53
            return true;
379
        }
380
381 190
        return $this->isValidShortNumberForRegion($number, $regionCode);
382
    }
383
384
    /**
385
     * Tests whether a short number matches a valid pattern in a region. Note that this doesn't verify
386
     * the number is actually in use, which is impossible to tell by just looking at the number
387
     * itself.
388
     *
389
     * @param PhoneNumber $number The Short number for which we want to test the validity
390
     * @param string $regionDialingFrom the region from which the number is dialed
391
     * @return boolean whether the short number matches a valid pattern
392
     */
393 243
    public function isValidShortNumberForRegion(PhoneNumber $number, $regionDialingFrom)
394
    {
395 243
        if (!$this->regionDialingFromMatchesNumber($number, $regionDialingFrom)) {
396 1
            return false;
397
        }
398 242
        $phoneMetadata = $this->getMetadataForRegion($regionDialingFrom);
399
400 242
        if ($phoneMetadata === null) {
401
            return false;
402
        }
403
404 242
        $shortNumber = $this->getNationalSignificantNumber($number);
405
406 242
        $generalDesc = $phoneMetadata->getGeneralDesc();
407
408 242
        if (!$this->matchesPossibleNumberAndNationalNumber($shortNumber, $generalDesc)) {
409 1
            return false;
410
        }
411
412 242
        $shortNumberDesc = $phoneMetadata->getShortCode();
413
414 242
        return $this->matchesPossibleNumberAndNationalNumber($shortNumber, $shortNumberDesc);
415
    }
416
417
    /**
418
     * Gets the expected cost category of a short number  when dialled from a region (however, nothing is
419
     * implied about its validity). If it is important that the number is valid, then its validity
420
     * must first be checked using {@link isValidShortNumberForRegion}. Note that emergency numbers
421
     * are always considered toll-free.
422
     * Example usage:
423
     * <pre>{@code
424
     * $shortInfo = ShortNumberInfo::getInstance();
425
     * $shortNumber = PhoneNumberUtil::parse("110", "US);
426
     * $regionCode = "FR";
427
     * if ($shortInfo->isValidShortNumberForRegion($shortNumber, $regionCode)) {
428
     *     $cost = $shortInfo->getExpectedCostForRegion($shortNumber, $regionCode);
429
     *    // Do something with the cost information here.
430
     * }}</pre>
431
     *
432
     * @param PhoneNumber $number the short number for which we want to know the expected cost category,
433
     *     as a string
434
     * @param string $regionDialingFrom the region from which the number is dialed
435
     * @return int the expected cost category for that region of the short number. Returns UNKNOWN_COST if
436
     *     the number does not match a cost category. Note that an invalid number may match any cost
437
     *     category.
438
     */
439 323
    public function getExpectedCostForRegion(PhoneNumber $number, $regionDialingFrom)
440
    {
441 323
        if (!$this->regionDialingFromMatchesNumber($number, $regionDialingFrom)) {
442 2
            return ShortNumberCost::UNKNOWN_COST;
443
        }
444
        // Note that regionDialingFrom may be null, in which case phoneMetadata will also be null.
445 322
        $phoneMetadata = $this->getMetadataForRegion($regionDialingFrom);
446 322
        if ($phoneMetadata === null) {
447
            return ShortNumberCost::UNKNOWN_COST;
448
        }
449
450 322
        $shortNumber = $this->getNationalSignificantNumber($number);
451
452
        // The possible lengths are not present for a particular sub-type if they match the general
453
        // description; for this reason, we check the possible lengths against the general description
454
        // first to allow an early exit if possible.
455 322
        if (!in_array(strlen($shortNumber), $phoneMetadata->getGeneralDesc()->getPossibleLength())) {
456 1
            return ShortNumberCost::UNKNOWN_COST;
457
        }
458
459
        // The cost categories are tested in order of decreasing expense, since if for some reason the
460
        // patterns overlap the most expensive matching cost category should be returned.
461 322
        if ($this->matchesPossibleNumberAndNationalNumber($shortNumber, $phoneMetadata->getPremiumRate())) {
462 17
            return ShortNumberCost::PREMIUM_RATE;
463
        }
464
465 307
        if ($this->matchesPossibleNumberAndNationalNumber($shortNumber, $phoneMetadata->getStandardRate())) {
466 20
            return ShortNumberCost::STANDARD_RATE;
467
        }
468
469 289
        if ($this->matchesPossibleNumberAndNationalNumber($shortNumber, $phoneMetadata->getTollFree())) {
470 53
            return ShortNumberCost::TOLL_FREE;
471
        }
472
473 239
        if ($this->isEmergencyNumber($shortNumber, $regionDialingFrom)) {
474
            // Emergency numbers are implicitly toll-free.
475 237
            return ShortNumberCost::TOLL_FREE;
476
        }
477
478 3
        return ShortNumberCost::UNKNOWN_COST;
479
    }
480
481
    /**
482
     * Gets the expected cost category of a short number (however, nothing is implied about its
483
     * validity). If the country calling code is unique to a region, this method behaves exactly the
484
     * same as {@link #getExpectedCostForRegion(PhoneNumber, String)}. However, if the country calling
485
     * code is shared by multiple regions, then it returns the highest cost in the sequence
486
     * PREMIUM_RATE, UNKNOWN_COST, STANDARD_RATE, TOLL_FREE. The reason for the position of
487
     * UNKNOWN_COST in this order is that if a number is UNKNOWN_COST in one region but STANDARD_RATE
488
     * or TOLL_FREE in another, its expected cost cannot be estimated as one of the latter since it
489
     * might be a PREMIUM_RATE number.
490
     *
491
     * <p>
492
     * For example, if a number is STANDARD_RATE in the US, but TOLL_FREE in Canada, the expected
493
     * cost returned by this method will be STANDARD_RATE, since the NANPA countries share the same
494
     * country calling code.
495
     * </p>
496
     *
497
     * Note: If the region from which the number is dialed is known, it is highly preferable to call
498
     * {@link #getExpectedCostForRegion(PhoneNumber, String)} instead.
499
     *
500
     * @param PhoneNumber $number the short number for which we want to know the expected cost category
501
     * @return int the highest expected cost category of the short number in the region(s) with the given
502
     *     country calling code
503
     */
504 3
    public function getExpectedCost(PhoneNumber $number)
505
    {
506 3
        $regionCodes = $this->getRegionCodesForCountryCode($number->getCountryCode());
507 3
        if (count($regionCodes) == 0) {
508 1
            return ShortNumberCost::UNKNOWN_COST;
509
        }
510 3
        if (count($regionCodes) == 1) {
511 1
            return $this->getExpectedCostForRegion($number, $regionCodes[0]);
512
        }
513 2
        $cost = ShortNumberCost::TOLL_FREE;
514 2
        foreach ($regionCodes as $regionCode) {
515 2
            $costForRegion = $this->getExpectedCostForRegion($number, $regionCode);
516
            switch ($costForRegion) {
517 2
                case ShortNumberCost::PREMIUM_RATE:
518 1
                    return ShortNumberCost::PREMIUM_RATE;
519
520 2
                case ShortNumberCost::UNKNOWN_COST:
521 1
                    $cost = ShortNumberCost::UNKNOWN_COST;
522 1
                    break;
523
524 2
                case ShortNumberCost::STANDARD_RATE:
525 1
                    if ($cost != ShortNumberCost::UNKNOWN_COST) {
526 1
                        $cost = ShortNumberCost::STANDARD_RATE;
527 1
                    }
528 1
                    break;
529 2
                case ShortNumberCost::TOLL_FREE:
530
                    // Do nothing
531 2
                    break;
532
            }
533 2
        }
534 2
        return $cost;
535
    }
536
537
    /**
538
     * Returns true if the given number exactly matches an emergency service number in the given
539
     * region.
540
     * <p>
541
     * This method takes into account cases where the number might contain formatting, but doesn't
542
     * allow additional digits to be appended. Note that {@code isEmergencyNumber(number, region)}
543
     * implies {@code connectsToEmergencyNumber(number, region)}.
544
     *
545
     * @param string $number the phone number to test
546
     * @param string $regionCode the region where the phone number is being dialled
547
     * @return boolean whether the number exactly matches an emergency services number in the given region
548
     */
549 252
    public function isEmergencyNumber($number, $regionCode)
550
    {
551 252
        return $this->matchesEmergencyNumberHelper($number, $regionCode, false /* doesn't allow prefix match */);
552
    }
553
554
    /**
555
     * Gets the national significant number of the a phone number. Note a national significant number
556
     * doesn't contain a national prefix or any formatting.
557
     * <p>
558
     * This is a temporary duplicate of the {@code getNationalSignificantNumber} method from
559
     * {@code PhoneNumberUtil}. Ultimately a canonical static version should exist in a separate
560
     * utility class (to prevent {@code ShortNumberInfo} needing to depend on PhoneNumberUtil).
561
     *
562
     * @param PhoneNumber $number the phone number for which the national significant number is needed
563
     * @return string the national significant number of the PhoneNumber object passed in
564
     */
565 630
    protected function getNationalSignificantNumber(PhoneNumber $number)
566
    {
567
        // If leading zero(s) have been set, we prefix this now. Note this is not a national prefix.
568 630
        $nationalNumber = '';
569 630
        if ($number->isItalianLeadingZero()) {
570 10
            $zeros = str_repeat('0', $number->getNumberOfLeadingZeros());
571 10
            $nationalNumber .= $zeros;
572 10
        }
573
574 630
        $nationalNumber .= $number->getNationalNumber();
575
576 630
        return $nationalNumber;
577
    }
578
579
    /**
580
     * TODO: Once we have benchmarked ShortnumberInfo, consider if it is worth keeping
581
     * this performance optimization.
582
     * @param string $number
583
     * @param PhoneNumberDesc $numberDesc
584
     * @return bool
585
     */
586 627
    protected function matchesPossibleNumberAndNationalNumber($number, PhoneNumberDesc $numberDesc)
587
    {
588 627
        if (count($numberDesc->getPossibleLength()) > 0 && !in_array(strlen($number), $numberDesc->getPossibleLength())) {
589 294
            return false;
590
        }
591
592 408
        return $this->matcherAPI->matchesNationalNumber($number, $numberDesc, false);
593
    }
594
}
595