Completed
Pull Request — master (#164)
by Joshua
09:55
created

getAlternateFormatsForCountry()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 7
nc 3
nop 1
1
<?php
2
3
namespace libphonenumber;
4
5
use libphonenumber\Leniency\AbstractLeniency;
6
7
class PhoneNumberMatcher implements \Iterator
8
{
9
    protected static $initialized = false;
10
11
    /**
12
     * The phone number pattern used by $this->find(), similar to
13
     * PhoneNumberUtil::VALID_PHONE_NUMBER, but with the following differences:
14
     * <ul>
15
     *   <li>All captures are limited in order to place an upper bound to the text matched by the
16
     *       pattern.
17
     * <ul>
18
     *   <li>Leading punctuation / plus signs are limited.
19
     *   <li>Consecutive occurrences of punctuation are limited.
20
     *   <li>Number of digits is limited.
21
     * </ul>
22
     *   <li>No whitespace is allowed at the start or end.
23
     *   <li>No alpha digits (vanity numbers such as 1-800-SIX-FLAGS) are currently supported.
24
     * </ul>
25
     *
26
     * @var string
27
     */
28
    protected static $pattern;
29
30
    /**
31
     * Matches strings that look like publication pages. Example:
32
     * <pre>Computing Complete Answers to Queries in the Presence of Limited Access Patterns.
33
     * Chen Li. VLDB J. 12(3): 211-227 (2003).</pre>
34
     *
35
     * The string "211-227 (2003)" is not a telephone number.
36
     *
37
     * @var string
38
     */
39
    protected static $pubPages = "\\d{1,5}-+\\d{1,5}\\s{0,4}\\(\\d{1,4}";
40
41
    /**
42
     * Matches strings that look like dates using "/" as a separator. Examples 3/10/2011, 31/10/2011 or
43
     * 08/31/95.
44
     *
45
     * @var string
46
     */
47
    protected static $slashSeparatedDates = "(?:(?:[0-3]?\\d/[01]?\\d)|(?:[01]?\\d/[0-3]?\\d))/(?:[12]\\d)?\\d{2}";
48
49
    /**
50
     * Matches timestamps. Examples: "2012-01-02 08:00". Note that the reg-ex does not include the
51
     * trailing ":\d\d" -- that is covered by timeStampsSuffix.
52
     *
53
     * @var string
54
     */
55
    protected static $timeStamps = "[12]\\d{3}[-/]?[01]\\d[-/]?[0-3]\\d +[0-2]\\d$";
56
    protected static $timeStampsSuffix = ":[0-5]\\d";
57
58
    /**
59
     * Pattern to check that brackets match. Opening brackets should be closed within a phone number.
60
     * This also checks that there is something inside the brackets. Having no brackets at all is also
61
     * fine.
62
     *
63
     * @var string
64
     */
65
    protected static $matchingBrackets;
66
67
    /**
68
     * Patterns used to extract phone numbers from a larger phone-number-like pattern. These are
69
     * ordered according to specificity. For example, white-space is last since that is frequently
70
     * used in numbers, not just to separate two numbers. We have separate patterns since we don't
71
     * want to break up the phone-number-like text on more than one different kind of symbol at one
72
     * time, although symbols of the same type (e.g. space) can be safely grouped together.
73
     *
74
     * Note that if there is a match, we will always check any text found up to the first match as
75
     * well.
76
     *
77
     * @var string[]
78
     */
79
    protected static $innerMatches = array();
80
81
    /**
82
     * Punctuation that may be at the start of a phone number - brackets and plus signs.
83
     *
84
     * @var string
85
     */
86
    protected static $leadClass;
87
88
    /**
89
     * Prefix of the files
90
     * @var string
91
     */
92
    protected static $alternateFormatsFilePrefix;
93
    const META_DATA_FILE_PREFIX = 'PhoneNumberAlternateFormats';
94
95
    protected static function init()
96
    {
97
        static::$alternateFormatsFilePrefix = dirname(__FILE__) . '/data/' . static::META_DATA_FILE_PREFIX;
98
99
        static::$innerMatches = array(
100
            // Breaks on the slash - e.g. "651-234-2345/332-445-1234"
101
            "/+(.*)",
102
            // Note that the bracket here is inside the capturing group, since we consider it part of the
103
            // phone number. Will match a pattern like "(650) 223 3345 (754) 223 3321".
104
            "(\\([^(]*)",
105
            // Breaks on a hyphen - e.g. "12345 - 332-445-1234 is my number."
106
            // We require a space on either side of the hyphen for it to be considered a separator.
107
            "(?:\\p{Z}-|-\\p{Z})\\p{Z}*(.+)",
108
            // Various types of wide hyphens. Note we have decided not to enforce a space here, since it's
109
            // possible that it's supposed to be used to break two numbers without spaces, and we haven't
110
            // seen many instances of it used within a number.
111
            "[‒-―-]\\p{Z}*(.+)",
112
            // Breaks on a full stop - e.g. "12345. 332-445-1234 is my number."
113
            "\\.+\\p{Z}*([^.]+)",
114
            // Breaks on space - e.g. "3324451234 8002341234"
115
            "\\p{Z}+(\\P{Z}+)"
116
        );
117
118
        /*
119
         * Builds the matchingBrackets and pattern regular expressions. The building blocks exist
120
         * to make the pattern more easily understood.
121
         */
122
123
        $openingParens = "(\\[\xEF\xBC\x88\xEF\xBC\xBB";
124
        $closingParens = ")\\]\xEF\xBC\x89\xEF\xBC\xBD";
125
        $nonParens = "[^" . $openingParens . $closingParens . "]";
126
127
        // Limit on the number of pairs of brackets in a phone number.
128
        $bracketPairLimit = static::limit(0, 3);
129
130
        /*
131
         * An opening bracket at the beginning may not be closed, but subsequent ones should be.  It's
132
         * also possible that the leading bracket was dropped, so we shouldn't be surprised if we see a
133
         * closing bracket first. We limit the sets of brackets in a phone number to four.
134
         */
135
        static::$matchingBrackets =
136
            "(?:[" . $openingParens . "])?" . "(?:" . $nonParens . "+" . "[" . $closingParens . "])?"
137
            . $nonParens . "+"
138
            . "(?:[" . $openingParens . "]" . $nonParens . "+[" . $closingParens . "])" . $bracketPairLimit
139
            . $nonParens . "*";
140
141
        // Limit on the number of leading (plus) characters.
142
        $leadLimit = static::limit(0, 2);
143
144
        // Limit on the number of consecutive punctuation characters.
145
        $punctuationLimit = static::limit(0, 4);
146
147
        /*
148
         * The maximum number of digits allowed in a digit-separated block. As we allow all digits in a
149
         * single block, set high enough to accommodate the entire national number and the international
150
         * country code
151
         */
152
        $digitBlockLimit = PhoneNumberUtil::MAX_LENGTH_FOR_NSN + PhoneNumberUtil::MAX_LENGTH_COUNTRY_CODE;
153
154
        /*
155
         * Limit on the number of blocks separated by the punctuation. Uses digitBlockLimit since some
156
         * formats use spaces to separate each digit
157
         */
158
        $blockLimit = static::limit(0, $digitBlockLimit);
159
160
        // A punctuation sequence allowing white space
161
        $punctuation = '[' . PhoneNumberUtil::VALID_PUNCTUATION . ']' . $punctuationLimit;
162
163
        // A digits block without punctuation.
164
        $digitSequence = "\\p{Nd}" . static::limit(1, $digitBlockLimit);
165
166
167
        $leadClassChars = $openingParens . PhoneNumberUtil::PLUS_CHARS;
168
        $leadClass = '[' . $leadClassChars . ']';
169
        static::$leadClass = $leadClass;
170
171
        // Init extension patterns from PhoneNumberUtil
172
        PhoneNumberUtil::initCapturingExtnDigits();
173
        PhoneNumberUtil::initExtnPatterns();
174
175
176
        // Phone number pattern allowing optional punctuation.
177
        static::$pattern = "(?:" . $leadClass . $punctuation . ")" . $leadLimit
178
            . $digitSequence . "(?:" . $punctuation . $digitSequence . ")" . $blockLimit
179
            . "(?:" . PhoneNumberUtil::$EXTN_PATTERNS_FOR_MATCHING . ")?";
180
181
        static::$initialized = true;
182
    }
183
184
    /**
185
     * Helper function to generate regular expression with an upper and lower limit.
186
     *
187
     * @param int $lower
188
     * @param int $upper
189
     * @return string
190
     */
191
    protected static function limit($lower, $upper)
192
    {
193
        if (($lower < 0) || ($upper <= 0) || ($upper < $lower)) {
194
            throw new \InvalidArgumentException();
195
        }
196
197
        return '{' . $lower . ',' . $upper . '}';
198
    }
199
200
    /**
201
     * The phone number utility.
202
     * @var PhoneNumberUtil
203
     */
204
    protected $phoneUtil;
205
206
    /**
207
     * The text searched for phone numbers.
208
     * @var string
209
     */
210
    protected $text;
211
212
    /**
213
     * The region (country) to assume for phone numbers without an international prefix, possibly
214
     * null.
215
     * @var string
216
     */
217
    protected $preferredRegion;
218
219
    /**
220
     * The degrees of validation requested.
221
     * @var AbstractLeniency
222
     */
223
    protected $leniency;
224
225
    /**
226
     * The maximum number of retires after matching an invalid number.
227
     * @var int
228
     */
229
    protected $maxTries;
230
231
    /**
232
     * One of:
233
     *  - NOT_READY
234
     *  - READY
235
     *  - DONE
236
     * @var string
237
     */
238
    protected $state = 'NOT_READY';
239
240
    /**
241
     * The last successful match, null unless $this->state = READY
242
     * @var PhoneNumberMatch
243
     */
244
    protected $lastMatch;
245
246
    /**
247
     * The next index to start searching at. Undefined when $this->state = DONE
248
     * @var int
249
     */
250
    protected $searchIndex = 0;
251
252
    /**
253
     * Creates a new instance. See the factory methods in PhoneNumberUtil on how to obtain a new instance.
254
     *
255
     *
256
     * @param PhoneNumberUtil $util The Phone Number Util to use
257
     * @param string|null $text The text that we will search, null for no text
258
     * @param string|null $country The country to assume for phone numbers not written in international format.
259
     *  (with a leading plus, or with the international dialling prefix of the specified region).
260
     *  May be null, or "ZZ" if only numbers with a leading plus should be considered.
261
     * @param AbstractLeniency $leniency The leniency to use when evaluating candidate phone numbers
262
     * @param int $maxTries The maximum number of invalid numbers to try before giving up on the text.
263
     *  This is to cover degenerate cases where the text has a lot of false positives in it. Must be >= 0
264
     * @throws \NullPointerException
265
     * @throws \InvalidArgumentException
266
     */
267
    public function __construct(PhoneNumberUtil $util, $text, $country, AbstractLeniency $leniency, $maxTries)
268
    {
269
        if ($maxTries < 0) {
270
            throw new \InvalidArgumentException();
271
        }
272
273
        $this->phoneUtil = $util;
274
        $this->text = ($text !== null) ? $text : "";
275
        $this->preferredRegion = $country;
276
        $this->leniency = $leniency;
277
        $this->maxTries = $maxTries;
278
279
        if (static::$initialized === false) {
280
            static::init();
281
        }
282
    }
283
284
    /**
285
     * Attempts to find the next subsequence in the searched sequence on or after {@code searchIndex}
286
     * that represents a phone number. Returns the next match, null if none was found.
287
     *
288
     * @param int $index The search index to start searching at
289
     * @return PhoneNumberMatch|null The Phone Number Match found, null if none can be found
290
     */
291
    protected function find($index)
292
    {
293
        $matcher = new Matcher(static::$pattern, $this->text);
294
        while (($this->maxTries > 0) && $matcher->find($index)) {
295
            $start = $matcher->start();
296
            $cutLength = $matcher->end() - $start;
297
            $candidate = mb_substr($this->text, $start, $cutLength);
298
299
            // Check for extra numbers at the end.
300
            // TODO: This is the place to start when trying to support extraction of multiple phone number
301
            // from split notations (+41 49 123 45 67 / 68).
302
            $candidate = static::trimAfterFirstMatch(PhoneNumberUtil::$SECOND_NUMBER_START_PATTERN, $candidate);
303
304
            $match = $this->extractMatch($candidate, $start);
305
            if ($match !== null) {
306
                return $match;
307
            }
308
309
            $index = $start + mb_strlen($candidate);
310
            $this->maxTries--;
311
        }
312
313
        return null;
314
    }
315
316
    /**
317
     * Trims away any characters after the first match of $pattern in $candidate,
318
     * returning the trimmed version.
319
     *
320
     * @param string $pattern
321
     * @param string $candidate
322
     * @return string
323
     */
324
    protected static function trimAfterFirstMatch($pattern, $candidate)
325
    {
326
        $trailingCharsMatcher = new Matcher($pattern, $candidate);
327
        if ($trailingCharsMatcher->find()) {
328
            $startChar = $trailingCharsMatcher->start();
329
            $candidate = mb_substr($candidate, 0, $startChar);
330
        }
331
        return $candidate;
332
    }
333
334
    /**
335
     * Helper method to determine if a character is a Latin-script letter or not. For our purposes,
336
     * combining marks should also return true since we assume they have been added to a preceding
337
     * Latin character.
338
     *
339
     * @param string $letter
340
     * @return bool
341
     * @internal
342
     */
343
    public static function isLatinLetter($letter)
344
    {
345
        // Combining marks are a subset of non-spacing-mark.
346
        if (preg_match('/\p{L}/u', $letter) !== 1 && preg_match('/\p{Mn}/u', $letter) !== 1) {
347
            return false;
348
        }
349
350
        return (preg_match('/\p{Latin}/u', $letter) === 1)
351
        || (preg_match('/\pM+/u', $letter) === 1);
352
    }
353
354
    /**
355
     * @param string $character
356
     * @return bool
357
     */
358
    protected static function isInvalidPunctuationSymbol($character)
359
    {
360
        return $character == '%' || preg_match('/\p{Sc}/u', $character);
361
    }
362
363
    /**
364
     * Attempts to extract a match from a $candidate.
365
     *
366
     * @param string $candidate The candidate text that might contain a phone number
367
     * @param int $offset The offset of $candidate within $this->text
368
     * @return PhoneNumberMatch|null The match found, null if none can be found
369
     */
370
    protected function extractMatch($candidate, $offset)
371
    {
372
        // Skip a match that is more likely to be a date.
373
        $dateMatcher = new Matcher(static::$slashSeparatedDates, $candidate);
374
        if ($dateMatcher->find()) {
375
            return null;
376
        }
377
378
        // Skip potential time-stamps.
379
        $timeStampMatcher = new Matcher(static::$timeStamps, $candidate);
380
        if ($timeStampMatcher->find()) {
381
            $followingText = mb_substr($this->text, $offset + mb_strlen($candidate));
382
            $timeStampSuffixMatcher = new Matcher(static::$timeStampsSuffix, $followingText);
383
            if ($timeStampSuffixMatcher->lookingAt()) {
384
                return null;
385
            }
386
        }
387
388
        // Try to come up with a valid match given the entire candidate.
389
        $rawString = $candidate;
390
        $match = $this->parseAndVerify($rawString, $offset);
391
        if ($match !== null) {
392
            return $match;
393
        }
394
395
        // If that failed, try to find an "inner match" - there might be a phone number within this
396
        // candidate.
397
        return $this->extractInnerMatch($rawString, $offset);
398
    }
399
400
    /**
401
     * Attempts to extract a match from $candidate if the whole candidate does not qualify as a
402
     * match.
403
     *
404
     * @param string $candidate The candidate text that might contact a phone number
405
     * @param int $offset The current offset of $candidate within $this->text
406
     * @return PhoneNumberMatch|null The match found, null if none can be found
407
     */
408
    protected function extractInnerMatch($candidate, $offset)
409
    {
410
        foreach (static::$innerMatches as $possibleInnerMatch) {
411
            $groupMatcher = new Matcher($possibleInnerMatch, $candidate);
412
            $isFirstMatch = true;
413
414
            while ($groupMatcher->find() && $this->maxTries > 0) {
415
                if ($isFirstMatch) {
416
                    // We should handle any group before this one too.
417
                    $group = static::trimAfterFirstMatch(PhoneNumberUtil::$UNWANTED_END_CHAR_PATTERN,
418
                        mb_substr($candidate, 0, $groupMatcher->start()));
419
420
                    $match = $this->parseAndVerify($group, $offset);
421
                    if ($match !== null) {
422
                        return $match;
423
                    }
424
                    $this->maxTries--;
425
                    $isFirstMatch = false;
426
                }
427
                $group = static::trimAfterFirstMatch(PhoneNumberUtil::$UNWANTED_END_CHAR_PATTERN,
428
                    $groupMatcher->group(1));
429
                $match = $this->parseAndVerify($group, $offset + $groupMatcher->start(1));
430
                if ($match !== null) {
431
                    return $match;
432
                }
433
                $this->maxTries--;
434
            }
435
        }
436
        return null;
437
    }
438
439
    /**
440
     * Parses a phone number from the $candidate} using PhoneNumberUtil::parse() and
441
     * verifies it matches the requested leniency. If parsing and verification succeed, a
442
     * corresponding PhoneNumberMatch is returned, otherwise this method returns null.
443
     *
444
     * @param string $candidate The candidate match
445
     * @param int $offset The offset of $candidate within $this->text
446
     * @return PhoneNumberMatch|null The parsed and validated phone number match, or null
447
     */
448
    protected function parseAndVerify($candidate, $offset)
449
    {
450
        try {
451
            // Check the candidate doesn't contain any formatting which would indicate that it really
452
            // isn't a phone number
453
            $matchingBracketsMatcher = new Matcher(static::$matchingBrackets, $candidate);
454
            $pubPagesMatcher = new Matcher(static::$pubPages, $candidate);
455
            if (!$matchingBracketsMatcher->matches() || $pubPagesMatcher->find()) {
456
                return null;
457
            }
458
459
            // If leniency is set to VALID or stricter, we also want to skip numbers that are surrounded
460
            // by Latin alphabetic characters, to skip cases like abc8005001234 or 8005001234def.
461
            if ($this->leniency->compareTo(Leniency::VALID()) >= 0) {
462
                // If the candidate is not at the start of the text, and does not start with phone-number
463
                // punctuation, check the previous character.
464
                $leadClassMatcher = new Matcher(static::$leadClass, $candidate);
465
                if ($offset > 0 && !$leadClassMatcher->lookingAt()) {
466
                    $previousChar = mb_substr($this->text, $offset - 1, 1);
467
                    // We return null if it is a latin letter or an invalid punctuation symbol.
468
                    if (static::isInvalidPunctuationSymbol($previousChar) || static::isLatinLetter($previousChar)) {
469
                        return null;
470
                    }
471
                }
472
                $lastCharIndex = $offset + mb_strlen($candidate);
473
                if ($lastCharIndex < mb_strlen($this->text)) {
474
                    $nextChar = mb_substr($this->text, $lastCharIndex, 1);
475
                    if (static::isInvalidPunctuationSymbol($nextChar) || static::isLatinLetter($nextChar)) {
476
                        return null;
477
                    }
478
                }
479
            }
480
481
            $number = $this->phoneUtil->parseAndKeepRawInput($candidate, $this->preferredRegion);
482
483
            // Check Israel * numbers: these are a special case in that they are four-digit numbers that
484
            // our library supports, but they can only be dialled with a leading *. Since we don't
485
            // actually store or detect the * in our phone number library, this means in practice we
486
            // detect most four digit numbers as being valid for Israel. We are considering moving these
487
            // numbers to ShortNumberInfo instead, in which case this problem would go away, but in the
488
            // meantime we want to restrict the false matches so we only allow these numbers if they are
489
            // preceded by a star. We enforce this for all leniency levels even though these numbers are
490
            // technically accepted by isPossibleNumber and isValidNumber since we consider it to be a
491
            // deficiency in those methods that they accept these numbers without the *.
492
            // TODO: Remove this or make it significantly less hacky once we've decided how to
493
            // handle these short codes going forward in ShortNumberInfo. We could use the formatting
494
            // rules for instance, but that would be slower.
495
            if ($this->phoneUtil->getRegionCodeForCountryCode($number->getCountryCode()) == "IL"
496
                && mb_strlen($this->phoneUtil->getNationalSignificantNumber($number)) === 4
497
                && ($offset === 0 || ($offset > 0 && mb_substr($this->text, $offset - 1, 1) != '*'))
498
            ) {
499
                // No match.
500
                return null;
501
            }
502
503
            if ($this->leniency->verify($number, $candidate, $this->phoneUtil)) {
504
                // We used parseAndKeepRawInput to create this number, but for now we don't return the extra
505
                // values parsed. TODO: stop clearing all values here and switch all users over
506
                // to using rawInput() rather than the rawString() of PhoneNumberMatch
507
                $number->clearCountryCodeSource();
508
                $number->clearRawInput();
509
                $number->clearPreferredDomesticCarrierCode();
510
                return new PhoneNumberMatch($offset, $candidate, $number);
511
            }
512
        } catch (NumberParseException $e) {
513
            // ignore and continue
514
        }
515
        return null;
516
    }
517
518
    /**
519
     * @param PhoneNumberUtil $util
520
     * @param PhoneNumber $number
521
     * @param string $normalizedCandidate
522
     * @param string[] $formattedNumberGroups
523
     * @return bool
524
     */
525
    public static function allNumberGroupsRemainGrouped(
526
        PhoneNumberUtil $util,
527
        PhoneNumber $number,
528
        $normalizedCandidate,
529
        $formattedNumberGroups
530
    ) {
531
        $fromIndex = 0;
532
        if ($number->getCountryCodeSource() !== CountryCodeSource::FROM_DEFAULT_COUNTRY) {
533
            // First skip the country code if the normalized candidate contained it.
534
            $countryCode = $number->getCountryCode();
535
            $fromIndex = mb_strpos($normalizedCandidate, $countryCode) + mb_strlen($countryCode);
536
        }
537
538
        // Check each group of consecutive digits are not broken into separate groupings in the
539
        // $normalizedCandidate string.
540
        $formattedNumberGroupsLength = count($formattedNumberGroups);
541
        for ($i = 0; $i < $formattedNumberGroupsLength; $i++) {
542
            // Fails if the substring of $normalizedCandidate starting from $fromIndex
543
            // doesn't contain the consecutive digits in $formattedNumberGroups[$i].
544
            $fromIndex = mb_strpos($normalizedCandidate, $formattedNumberGroups[$i], $fromIndex);
545
            if ($fromIndex === false) {
546
                return false;
547
            }
548
549
            // Moves $fromIndex forward.
550
            $fromIndex += mb_strlen($formattedNumberGroups[$i]);
551
            if ($i === 0 && $fromIndex < mb_strlen($normalizedCandidate)) {
552
                // We are at the position right after the NDC. We get the region used for formatting
553
                // information based on the country code in the phone number, rather than the number itself,
554
                // as we do not need to distinguish between different countries with the same country
555
                // calling code and this is faster.
556
                $region = $util->getRegionCodeForCountryCode($number->getCountryCode());
557
558
                if ($util->getNddPrefixForRegion($region, true) !== null
559
                    && is_int(mb_substr($normalizedCandidate, $fromIndex, 1))
560
                ) {
561
                    // This means there is no formatting symbol after the NDC. In this case, we only
562
                    // accept the number if there is no formatting symbol at all in the number, except
563
                    // for extensions. This is only important for countries with national prefixes.
564
                    $nationalSignificantNumber = $util->getNationalSignificantNumber($number);
565
                    return mb_substr(
566
                        mb_substr($normalizedCandidate, $fromIndex - mb_strlen($formattedNumberGroups[$i])),
567
                        mb_strlen($nationalSignificantNumber)
568
                    ) === $nationalSignificantNumber;
569
                }
570
            }
571
        }
572
        // The check here makes sure that we haven't mistakenly already used the extension to
573
        // match the last group of the subscriber number. Note the extension cannot have
574
        // formatting in-between digits
575
576
        if ($number->hasExtension()) {
577
            return mb_strpos(mb_substr($normalizedCandidate, $fromIndex), $number->getExtension()) !== false;
578
        }
579
580
        return true;
581
    }
582
583
    /**
584
     * @param PhoneNumberUtil $util
585
     * @param PhoneNumber $number
586
     * @param string $normalizedCandidate
587
     * @param string[] $formattedNumberGroups
588
     * @return bool
589
     */
590
    public static function allNumberGroupsAreExactlyPresent(
591
        PhoneNumberUtil $util,
592
        PhoneNumber $number,
593
        $normalizedCandidate,
594
        $formattedNumberGroups
595
    ) {
596
        $candidateGroups = preg_split(PhoneNumberUtil::NON_DIGITS_PATTERN, $normalizedCandidate);
597
598
        // Set this to the last group, skipping it if the number has an extension.
599
        $candidateNumberGroupIndex = $number->hasExtension() ? count($candidateGroups) - 2 : count($candidateGroups) - 1;
600
601
        // First we check if the national significant number is formatted as a block.
602
        // We use contains and not equals, since the national significant number may be present with
603
        // a prefix such as a national number prefix, or the country code itself.
604
        if (count($candidateGroups) == 1
605
            || mb_strpos($candidateGroups[$candidateNumberGroupIndex],
606
                $util->getNationalSignificantNumber($number)) !== false
607
        ) {
608
            return true;
609
        }
610
611
        // Starting from the end, go through in reverse, excluding the first group, and check the
612
        // candidate and number groups are the same.
613
        for ($formattedNumberGroupIndex = (count($formattedNumberGroups) - 1);
614
             $formattedNumberGroupIndex > 0 && $candidateNumberGroupIndex >= 0;
615
             $formattedNumberGroupIndex--, $candidateNumberGroupIndex--) {
616
            if ($candidateGroups[$candidateNumberGroupIndex] != $formattedNumberGroups[$formattedNumberGroupIndex]) {
617
                return false;
618
            }
619
        }
620
621
        // Now check the first group. There may be a national prefix at the start, so we only check
622
        // that the candidate group ends with the formatted number group.
623
        return ($candidateNumberGroupIndex >= 0
624
            && mb_substr($candidateGroups[$candidateNumberGroupIndex],
625
                -mb_strlen($formattedNumberGroups[0])) == $formattedNumberGroups[0]);
626
    }
627
628
    /**
629
     * Helper method to get the national-number part of a number, formatted without any national
630
     * prefix, and return it as a set of digit blocks that would be formatted together.
631
     *
632
     * @param PhoneNumberUtil $util
633
     * @param PhoneNumber $number
634
     * @param NumberFormat $formattingPattern
635
     * @return string[]
636
     */
637
    protected static function getNationalNumberGroups(
638
        PhoneNumberUtil $util,
639
        PhoneNumber $number,
640
        NumberFormat $formattingPattern = null
641
    ) {
642
        if ($formattingPattern === null) {
643
            // This will be in the format +CC-DG;ext=EXT where DG represents groups of digits.
644
            $rfc3966Format = $util->format($number, PhoneNumberFormat::RFC3966);
645
            // We remove the extension part from the formatted string before splitting it into different
646
            // groups.
647
            $endIndex = mb_strpos($rfc3966Format, ';');
648
            if ($endIndex === false) {
649
                $endIndex = mb_strlen($rfc3966Format);
650
            }
651
652
            // The country-code will have a '-' following it.
653
            $startIndex = mb_strpos($rfc3966Format, '-') + 1;
654
            return explode('-', mb_substr($rfc3966Format, $startIndex, $endIndex - $startIndex));
655
        } else {
656
            // We format the NSN only, and split that according to the separator.
657
            $nationalSignificantNumber = $util->getNationalSignificantNumber($number);
658
            return explode('-', $util->formatNsnUsingPattern($nationalSignificantNumber, $formattingPattern,
659
                PhoneNumberFormat::RFC3966));
660
        }
661
    }
662
663
    /**
664
     * @param PhoneNumber $number
665
     * @param string $candidate
666
     * @param PhoneNumberUtil $util
667
     * @param \Closure $checker
668
     * @return bool
669
     */
670
    public static function checkNumberGroupingIsValid(
671
        PhoneNumber $number,
672
        $candidate,
673
        PhoneNumberUtil $util,
674
        \Closure $checker
675
    ) {
676
        // TODO: Evaluate how this works for other locales (testing has been limited to NANPA regions)
677
        // and optimise if necessary.
678
        $normalizedCandidate = PhoneNumberUtil::normalizeDigits($candidate, true /* keep non-digits */);
679
        $formattedNumberGroups = static::getNationalNumberGroups($util, $number, null);
680
        if ($checker($util, $number, $normalizedCandidate, $formattedNumberGroups)) {
681
            return true;
682
        }
683
684
        // If this didn't pass, see if there are any alternative formats, and try them instead.
685
        $alternateFormats = static::getAlternateFormatsForCountry($number->getCountryCode());
686
687
        if ($alternateFormats !== null) {
688
            foreach ($alternateFormats->numberFormats() as $alternateFormat) {
689
                $formattedNumberGroups = static::getNationalNumberGroups($util, $number, $alternateFormat);
690
                if ($checker($util, $number, $normalizedCandidate, $formattedNumberGroups)) {
691
                    return true;
692
                }
693
            }
694
        }
695
        return false;
696
    }
697
698
    /**
699
     * @param PhoneNumber $number
700
     * @param string $candidate
701
     * @return bool
702
     */
703
    public static function containsMoreThanOneSlashInNationalNumber(PhoneNumber $number, $candidate)
704
    {
705
        $firstSlashInBodyIndex = mb_strpos($candidate, '/');
706
        if ($firstSlashInBodyIndex === false) {
707
            // No slashes, this is okay
708
            return false;
709
        }
710
711
        // Now look for a second one.
712
        $secondSlashInBodyIndex = mb_strpos($candidate, '/', $firstSlashInBodyIndex + 1);
713
        if ($secondSlashInBodyIndex === false) {
714
            // Only one slash, this is okay
715
            return false;
716
        }
717
718
        // If the first slash is after the country calling code, this is permitted
719
        $candidateHasCountryCode = ($number->getCountryCodeSource() === CountryCodeSource::FROM_NUMBER_WITH_PLUS_SIGN
720
            || $number->getCountryCodeSource() === CountryCodeSource::FROM_NUMBER_WITHOUT_PLUS_SIGN);
721
722
        if ($candidateHasCountryCode
723
            && PhoneNumberUtil::normalizeDigitsOnly(
724
                mb_substr($candidate, 0, $firstSlashInBodyIndex)
725
            ) == $number->getCountryCode()
726
        ) {
727
            // Any more slashes and this is illegal
728
            return (mb_strpos(mb_substr($candidate, $secondSlashInBodyIndex + 1), '/') !== false);
729
        }
730
731
        return true;
732
    }
733
734
    /**
735
     * @param PhoneNumber $number
736
     * @param string $candidate
737
     * @param PhoneNumberUtil $util
738
     * @return bool
739
     */
740
    public static function containsOnlyValidXChars(PhoneNumber $number, $candidate, PhoneNumberUtil $util)
741
    {
742
        // The characters 'x' and 'X' can be (1) a carrier code, in which case they always precede the
743
        // national significant number or (2) an extension sign, in which case they always precede the
744
        // extension number. We assume a carrier code is more than 1 digit, so the first case has to
745
        // have more than 1 consecutive 'x' or 'X', whereas the second case can only have exactly 1 'x'
746
        // or 'X'. We ignore the character if it appears as the last character of the string.
747
        $candidateLength = mb_strlen($candidate);
748
749
        for ($index = 0; $index < $candidateLength - 1; $index++) {
750
            $charAtIndex = mb_substr($candidate, $index, 1);
751
            if ($charAtIndex == 'x' || $charAtIndex == 'X') {
752
                $charAtNextIndex = mb_substr($candidate, $index + 1, 1);
753
                if ($charAtNextIndex == 'x' || $charAtNextIndex == 'X') {
754
                    // This is the carrier code case, in which the 'X's always precede the national
755
                    // significant number.
756
                    $index++;
757
758
                    if ($util->isNumberMatch($number, mb_substr($candidate, $index)) != MatchType::NSN_MATCH) {
759
                        return false;
760
                    }
761
                } elseif (!PhoneNumberUtil::normalizeDigitsOnly(mb_substr($candidate,
762
                        $index)) == $number->getExtension()
763
                ) {
764
                    // This is the extension sign case, in which the 'x' or 'X' should always precede the
765
                    // extension number
766
                    return false;
767
                }
768
            }
769
        }
770
        return true;
771
    }
772
773
    /**
774
     * @param PhoneNumber $number
775
     * @param PhoneNumberUtil $util
776
     * @return bool
777
     */
778
    public static function isNationalPrefixPresentIfRequired(PhoneNumber $number, PhoneNumberUtil $util)
779
    {
780
        // First, check how we deduced the country code. If it was written in international format, then
781
        // the national prefix is not required.
782
        if ($number->getCountryCodeSource() !== CountryCodeSource::FROM_DEFAULT_COUNTRY) {
783
            return true;
784
        }
785
786
        $phoneNumberRegion = $util->getRegionCodeForCountryCode($number->getCountryCode());
787
        $metadata = $util->getMetadataForRegion($phoneNumberRegion);
788
        if ($metadata === null) {
789
            return true;
790
        }
791
792
        // Check if a national prefix should be present when formatting this number.
793
        $nationalNumber = $util->getNationalSignificantNumber($number);
794
        $formatRule = $util->chooseFormattingPatternForNumber($metadata->numberFormats(), $nationalNumber);
795
        // To do this, we check that a national prefix formatting rule was present and that it wasn't
796
        // just the first-group symbol ($1) with punctuation.
797
        if (($formatRule !== null) && mb_strlen($formatRule->getNationalPrefixFormattingRule()) > 0) {
798
            if ($formatRule->isNationalPrefixOptionalWhenFormatting()) {
799
                // The national-prefix is optional in these cases, so we don't need to check if it was
800
                // present.
801
                return true;
802
            }
803
804
            if (PhoneNumberUtil::formattingRuleHasFirstGroupOnly($formatRule->getNationalPrefixFormattingRule())) {
805
                // National Prefix not needed for this number.
806
                return true;
807
            }
808
809
            // Normalize the remainder.
810
            $rawInputCopy = PhoneNumberUtil::normalizeDigitsOnly($number->getRawInput());
811
            $rawInput = $rawInputCopy;
812
            // Check if we found a national prefix and/or carrier code at the start of the raw input, and
813
            // return the result.
814
            $carrierCode = null;
815
            return $util->maybeStripNationalPrefixAndCarrierCode($rawInput, $metadata, $carrierCode);
816
        }
817
        return true;
818
    }
819
820
821
    /**
822
     * Storage for Alternate Formats
823
     * @var PhoneMetadata[]
824
     */
825
    protected static $callingCodeToAlternateFormatsMap = array();
826
827
    /**
828
     * @param $countryCallingCode
829
     * @return PhoneMetadata|null
830
     */
831
    protected static function getAlternateFormatsForCountry($countryCallingCode)
832
    {
833
        $countryCodeSet = AlternateFormatsCountryCodeSet::$alternateFormatsCountryCodeSet;
834
835
        if (!in_array($countryCallingCode, $countryCodeSet)) {
836
            return null;
837
        }
838
839
        if (!isset(static::$callingCodeToAlternateFormatsMap[$countryCallingCode])) {
840
            static::loadAlternateFormatsMetadataFromFile($countryCallingCode);
841
        }
842
843
        return static::$callingCodeToAlternateFormatsMap[$countryCallingCode];
844
    }
845
846
    /**
847
     * @param string $countryCallingCode
848
     * @throws \Exception
849
     */
850
    protected static function loadAlternateFormatsMetadataFromFile($countryCallingCode)
851
    {
852
        $fileName = static::$alternateFormatsFilePrefix . '_' . $countryCallingCode . '.php';
853
854
        if (!is_readable($fileName)) {
855
            throw new \Exception('missing metadata: ' . $fileName);
856
        }
857
858
        $metadataLoader = new DefaultMetadataLoader();
859
        $data = $metadataLoader->loadMetadata($fileName);
860
        $metadata = new PhoneMetadata();
861
        $metadata->fromArray($data);
862
        static::$callingCodeToAlternateFormatsMap[$countryCallingCode] = $metadata;
863
    }
864
865
866
    /**
867
     * Return the current element
868
     * @link http://php.net/manual/en/iterator.current.php
869
     * @return PhoneNumberMatch|null
870
     */
871
    public function current()
872
    {
873
        return $this->lastMatch;
874
    }
875
876
    /**
877
     * Move forward to next element
878
     * @link http://php.net/manual/en/iterator.next.php
879
     * @return void Any returned value is ignored.
880
     */
881
    public function next()
882
    {
883
        $this->lastMatch = $this->find($this->searchIndex);
884
885
        if ($this->lastMatch === null) {
886
            $this->state = 'DONE';
887
        } else {
888
            $this->searchIndex = $this->lastMatch->end();
889
            $this->state = 'READY';
890
        }
891
892
        $this->searchIndex++;
893
    }
894
895
    /**
896
     * Return the key of the current element
897
     * @link http://php.net/manual/en/iterator.key.php
898
     * @return mixed scalar on success, or null on failure.
899
     * @since 5.0.0
900
     */
901
    public function key()
902
    {
903
        return $this->searchIndex;
904
    }
905
906
    /**
907
     * Checks if current position is valid
908
     * @link http://php.net/manual/en/iterator.valid.php
909
     * @return boolean The return value will be casted to boolean and then evaluated.
910
     * Returns true on success or false on failure.
911
     * @since 5.0.0
912
     */
913
    public function valid()
914
    {
915
        return $this->state === 'READY';
916
    }
917
918
    /**
919
     * Rewind the Iterator to the first element
920
     * @link http://php.net/manual/en/iterator.rewind.php
921
     * @return void Any returned value is ignored.
922
     * @since 5.0.0
923
     */
924
    public function rewind()
925
    {
926
        $this->searchIndex = 0;
927
        $this->next();
928
    }
929
}
930